﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[LTUpdateRevisedLotTravelerHoldStep]    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].[LTUpdateRevisedLotTravelerHoldStep] -- Add the parameters for the stored procedure here
	@LotTravHoldStepID INT,
	@NewLocation VARCHAR(50),
	@NewOperSequence CHAR(10) = NULL,
	@NewOperation VARCHAR(50),
	@NewOperationDescription VARCHAR(100),
	@NewChangeInstructions VARCHAR(1000),
	@NewUpdatedBy INT,
	@Result INT = 0 OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @SPNCurrentSeqForTheLot NCHAR(10) DECLARE @ProcessFlow NCHAR(10) DECLARE @LotNumber VARCHAR(50) DECLARE @PrevLocation VARCHAR(50) DECLARE @PrevOperSequence NCHAR(10) DECLARE @PrevOperation VARCHAR(50) -- Get values of the existing record
SELECT
	@PrevLocation = Location,
	@PrevOperSequence = OperSequence,
	@PrevOperation = Operation
FROM
	LTLotTravelerHoldSteps
WHERE
	ID = @LotTravHoldStepID IF @PrevLocation = @NewLocation
	AND @PrevOperSequence = @NewOperSequence
	AND @PrevOperation = @NewOperation BEGIN
UPDATE
	LTLotTravelerHoldSteps
SET
	Location = @NewLocation,
	OperSequence = @NewOperSequence,
	Operation = @NewOperation,
	OperationDescription = @NewOperationDescription,
	ChangeInstructions = @NewChangeInstructions
WHERE
	PrevLotTravHoldStepID = @LotTravHoldStepID
END
ELSE -- else if the Location/Sequence/Operation is changed update based on the below check
BEGIN
SET
	@LotNumber = (
		SELECT
			TOP 1 L.LotNumber
		FROM
			LTLotTravelerHoldSteps LH
			INNER JOIN LTLotTravelerRevisionAttrib LTA ON LH.LotTravelerRevisionID = LTA.ID
			INNER JOIN LTLot L ON LTA.LTLotID = L.ID
		WHERE
			LH.ID = @LotTravHoldStepID
	)
SET
	@ProcessFlow = (
		SELECT
			TOP 1 L.Process
		FROM
			LTLotTravelerHoldSteps LH
			INNER JOIN LTLotTravelerRevisionAttrib LTA ON LH.LotTravelerRevisionID = LTA.ID
			INNER JOIN LTLot L ON LTA.LTLotID = L.ID
		WHERE
			LH.ID = @LotTravHoldStepID
	) -- Get the Max Sequence# for this lot for the given location
SET
	@SPNCurrentSeqForTheLot = (
		SELECT
			MAX(SEQ)
		FROM
			vFAB2SPN_WO_RECORD WO
			INNER JOIN vSPNPDB_FLOWLOCS F ON WO.WO_PROCESS = F.PROCESS
			AND WO.WO_LOCATION = F.LOC
			AND WO.WO_OPER_NO = F.OPER
		WHERE
			WO_LOT_NO = @LotNumber
			AND WO_LOCATION = @NewLocation
			AND WO_PROCESS = @ProcessFlow
	) IF (@NewOperSequence > @SPNCurrentSeqForTheLot)
	OR (@SPNCurrentSeqForTheLot IS NULL) BEGIN -- Update the values on the new revised Hold Step
UPDATE
	LTLotTravelerHoldSteps
SET
	Location = @NewLocation,
	OperSequence = @NewOperSequence,
	Operation = @NewOperation,
	OperationDescription = @NewOperationDescription,
	ChangeInstructions = @NewChangeInstructions
WHERE
	PrevLotTravHoldStepID = @LotTravHoldStepID
END
ELSE
SET
	@Result = -1
END
END
GO