﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[ECNUnlockECN]    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].[ECNUnlockECN] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

SELECT
	212 AS UserID,
	E.ECNNumber AS IssueID,
	E.ECNNumber AS SysDocumentID,
	'ECN' AS DocumentType,
	'Record UnLock' AS OperationType,
	'Record locked by ' + U.FirstName + ' ' + U.LastName + ' on ' + CONVERT(VARCHAR(20), RecordLockedDate) + ' was Unlocked on ' + CONVERT(VARCHAR(20), GETDATE()) AS Comments,
	GETDATE() AS InsertTimeStamp INTO #TempLockedRecords
FROM
	ECN E
	INNER JOIN Users U ON E.RecordLockedBy = U.UserID
WHERE
	DATEDIFF(MI, RecordLockedDate, GETDATE()) > 30
	AND DATEDIFF(MI, LastUpdateDate, GETDATE()) > 30
INSERT INTO
	EventLog (
		UserID,
		IssueID,
		SysDocumentID,
		DocumentType,
		OperationType,
		Comments,
		InsertTimeStamp
	)
SELECT
	*
FROM
	#TempLockedRecords
UPDATE
	ECN
SET
	RecordLockindicator = 0,
	RecordLockedBy = NULL,
	RecordLockedDate = NULL
WHERE
	ECNNumber IN (
		SELECT
			SysDocumentID
		FROM
			#TempLockedRecords)
			DROP TABLE #TempLockedRecords
	END
GO