﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[LTCloseTraveler]    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].[LTCloseTraveler] @LTLOTID INT,
	@UserID INT,
	@Reason VARCHAR(50),
	@Result INT OUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @LotStatus NCHAR(10) -- Insert statements for procedure here
IF @Reason = 'Complete' BEGIN -- check SPN if the lot has been closed
SET
	@LotStatus = (
		SELECT
			LTRIM(RTRIM(WP.WP_STATUS))
		FROM
			vFAB2SPN_WP_RECORD WP
			INNER JOIN LTLot L ON WP.WP_LOT_NO = L.LotNumber
		WHERE
			L.ID = @LTLOTID
	) IF @LotStatus = 'C' BEGIN
UPDATE
	LTLot
SET
	TravelerClosed = 1,
	TravelerClosedDate = GETDATE(),
	TravelerClosedBy = @UserID,
	TravelerClosedReason = @Reason
WHERE
	ID = @LTLOTID
SET
	@Result = 1
END
END
ELSE BEGIN
UPDATE
	LTLot
SET
	TravelerClosed = 1,
	TravelerClosedDate = GETDATE(),
	TravelerClosedBy = @UserID,
	TravelerClosedReason = @Reason
WHERE
	ID = @LTLOTID
SET
	@Result = 1
END
END
GO