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

SELECT
	212 AS UserID,
	LTLotID AS IssueID,
	ID AS SysDocumentID,
	'LotTraveler' 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 #LTTempLockedRecords
FROM
	LTLotTravelerRevisionAttrib LT
	INNER JOIN Users U ON LT.RecordLockedBy = U.UserID
WHERE
	DATEDIFF(MI, RecordLockedDate, GETDATE()) > 30
	AND DATEDIFF(MI, TraverlerLastUpdatedDatetime, GETDATE()) > 30
INSERT INTO
	EventLog (
		UserID,
		IssueID,
		SysDocumentID,
		DocumentType,
		OperationType,
		Comments,
		InsertTimeStamp
	)
SELECT
	*
FROM
	#LTTempLockedRecords
UPDATE
	LTLotTravelerRevisionAttrib
SET
	RecordLockedBy = NULL,
	RecordLockIndicator = 0,
	RecordLockedDate = NULl
WHERE
	ID IN (
		SELECT
			SysDocumentID
		FROM
			#LTTempLockedRecords)
			DROP TABLE #LTTempLockedRecords
	END
GO