﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[ECNGetExpiredDocuments_05012017]    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].[ECNGetExpiredDocuments_05012017] 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
	E.ECNNumber,
	CASE
		WHEN E.ExtensionDate IS NOT NULL THEN E.ExtensionDate
		ELSE ExpirationDate
	END AS ExpirationDate,
	U.Email,
	CurrentStep
FROM
	ECN E
	INNER JOIN Users U ON E.OriginatorID = U.UserID
WHERE
	IsTECN = 1
	AND CancellationApproved = 0
	AND CancellationInProgress = 0
	AND ExpirationProcessed = 0
	AND ExpirationInProgress = 0
	AND (
		(
			CAST(ExtensionDate AS DATE) <= CAST(GETDATE() AS DATE)
		)
		OR (
			ExtensionDate IS NULL
			AND CAST(ExpirationDate AS DATE) <= CAST(GETDATE() AS DATE)
		)
	)
	AND CloseDate IS NOT NULL
END
GO