﻿USE [FabApprovalSystem]
GO
  /****** Object:  StoredProcedure [dbo].[_8DGetDueD3D5D7]    Script Date: 11/21/2024 11:29:04 AM ******/
SET
  ANSI_NULLS ON
GO
SET
  QUOTED_IDENTIFIER ON
GO
  -- =============================================
  -- Author:		<Author,,Name>
  -- Create date: <Create Date,,>
  -- Description:	<Description,,>
  -- =============================================
  CREATE PROCEDURE [dbo].[_8DGetDueD3D5D7] -- Add the parameters for the stored procedure here
  AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
SET
  NOCOUNT ON;

-- Insert statements for procedure here
SELECT
  CANo,
  D3DueDate AS DueDate,
  'D3' AS ItemDue,
  CASE
    WHEN (DATEDIFF(HOUR, GETDATE(), D3DueDate) > 0) THEN 'COMING DUE'
    WHEN (DATEDIFF(HOUR, GETDATE(), D3DueDate) <= 0) THEN 'PAST DUE'
  END AS ExpiryType
FROM
  _8DCorrectiveAction
WHERE
  (
    D3DueDate < GETDATE()
    OR DATEDIFF(DAY, GETDATE(), D3DueDate) < 1
  )
  AND D3CompleteDate IS NULL
  AND (
    D3NotificationSent IS NULL
    OR DATEDIFF(DAY, D3NotificationSent, GETDATE()) >= 1
  )
  AND Deleted = 0
UNION
SELECT
  CANo,
  D5D7DueDate AS DueDate,
  'D5D7' AS ItemDue,
  CASE
    WHEN (DATEDIFF(HOUR, GETDATE(), D5D7DueDate) > 0) THEN 'COMING DUE'
    WHEN (DATEDIFF(HOUR, GETDATE(), D5D7DueDate) <= 0) THEN 'PAST DUE'
  END AS ExpiryType
FROM
  _8DCorrectiveAction
WHERE
  (
    D5D7DueDate < GETDATE()
    OR DATEDIFF(DAY, GETDATE(), D5D7DueDate) < 1
  )
  AND D5CompleteDate IS NULL
  AND (
    D5D7NotificationSent IS NULL
    OR DATEDIFF(DAY, D5D7NotificationSent, GETDATE()) >= 1
  )
  AND Deleted = 0
END
GO