﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[LTUpdateWorkRequest]    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].[LTUpdateWorkRequest] @LTWorkRequestID INT,
	@Title VARCHAR(100),
	@LotType CHAR(1),
	@EngLotStartRequired INT,
	@LotTypeConversion INT,
	@QualLot INT,
	@PurposeOfRequest VARCHAR(1000),
	@PlannedLotStartDate DATETIME,
	@PlannedScheduledCloseDate DATETIME,
	@ChargeDepartment VARCHAR(50),
	@AllocationToUse VARCHAR(100),
	@PredictedCyleTime INT,
	@DeptChargedForRawWafers VARCHAR(50),
	@EstimatedBinCLoseDate DATETIME = NULL,
	@TotalQty INT = NULL,
	@WIPArea NCHAR(10),
	@LotStartDate DATETIME,
	@LotNumber VARCHAR(50),
	@WIPPartNumber VARCHAR(50),
	@PartDescription VARCHAR(100),
	@DefaultEPISiliconPartNumber VARCHAR(50),
	@ProcessFlow NCHAR(10),
	@EmployeeID NCHAR(10),
	@VerbComment VARCHAR(1000),
	@RecordSiliconLotInSPNTapeTagTotrav INT,
	@SiliconLotNoCassette1 VARCHAR(500),
	@SiliconOnHand BIT,
	@SiliconExpectedDueDate DATETIME,
	@SiliconComments VARCHAR(500),
	@ReticleOnHand BIT,
	@ReticleExpectedDueDate DATETIME,
	@ReticleComments VARCHAR(1000),
	@SPNTransferOnHand BIT,
	@SPNTransferExpectedDueDate DATETIME,
	@SPNTransferComments VARCHAR(500),
	@ProbeCardOnHand BIT,
	@ProbeCardExpectedDueDate DATETIME,
	@ProbeCardComments VARCHAR(500),
	@ProbeRecipeOnHand BIT,
	@ProbeRecipeExpectedDueDate DATETIME,
	@ProbeRecipeComments VARCHAR(500),
	@ProcessChangeDetailsOnHand BIT,
	@ProcessChangeDetailsDueDate DATETIME,
	@LotStartCheckListComments VARCHAR(1000),
	@PCRBNumber VARCHAR(50),
	@LastUpdateBy 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 @LoginID VARCHAR(50)
SELECT
	@LoginID = LoginID
FROM
	Users
WHERE
	UserID = @LastUpdateBy IF (
		SELECT
			COUNT(*)
		FROM
			LTWorkRequest
		WHERE
			ID = @LTWorkRequestID
			AND RecordLockedBy = @LastUpdateBy
	) > 0 BEGIN -- 2019/03/18 ticket #35320 delete lot material rows if part # has changed
	IF EXISTS (
		SELECT
			1
		FROM
			LTWorkRequest
		WHERE
			ID = @LTWorkRequestID
			AND WIPPartNumber <> @WIPPartNumber
	) BEGIN
DELETE FROM
	LTMaterial
WHERE
	LTWorkRequestID = @LTWorkRequestID IF @ @ROWCOUNT > 0 BEGIN
INSERT INTO
	EventLog (
		UserID,
		DocumentType,
		IssueID,
		OperationType,
		InsertTimeStamp,
		Comments,
		SysDocumentID
	)
SELECT
	@LoginID,
	'WorkRequest',
	LTWorkRequest.SWRNumber,
	'Logging',
	GETDATE(),
	'Removed lot material rows due to part # change',
	ID
FROM
	LTWorkRequest
WHERE
	ID = @LTWorkRequestID
END
END
UPDATE
	LTWorkRequest
SET
	Title = @Title,
	LotType = @LotType,
	EngLotStartRequired = @EngLotStartRequired,
	LotTypeConversion = @LotTypeConversion,
	QualLot = @QualLot,
	PurposeOfRequest = @PurposeOfRequest,
	PlannedLotStartDate = @PlannedLotStartDate,
	PlannedScheduledCloseDate = @PlannedScheduledCloseDate,
	ChargeDepartment = @ChargeDepartment,
	AllocationToUse = @AllocationToUse,
	PredictedCyleTime = @PredictedCyleTime,
	EstimatedBinCLoseDate = @EstimatedBinCLoseDate,
	--WIPArea = @WIPArea ,
	TotalQty = @TotalQty,
	LotStartDate = @LotStartDate,
	LotNumber = @LotNumber,
	WIPPartNumber = @WIPPartNumber,
	PartDescription = @PartDescription,
	DefaultEPISiliconPartNumber = @DefaultEPISiliconPartNumber,
	ProcessFlow = @ProcessFlow,
	EmployeeID = @EmployeeID,
	VerbComment = @VerbComment,
	RecordSiliconLotInSPNTapeTagTotrav = @RecordSiliconLotInSPNTapeTagTotrav,
	SiliconLotNoCassette1 = @SiliconLotNoCassette1,
	SiliconOnHand = @SiliconOnHand,
	SiliconExpectedDueDate = @SiliconExpectedDueDate,
	SiliconComments = @SiliconComments,
	ReticleOnHand = @ReticleOnHand,
	ReticleExpectedDueDate = @ReticleExpectedDueDate,
	ReticleComments = @ReticleComments,
	SPNTransferOnHand = @SPNTransferOnHand,
	SPNTransferExpectedDueDate = @SPNTransferExpectedDueDate,
	SPNTransferComments = @SPNTransferComments,
	ProbeCardOnHand = @ProbeCardOnHand,
	ProbeCardExpectedDueDate = @ProbeCardExpectedDueDate,
	ProbeCardComments = @ProbeCardComments,
	ProbeRecipeOnHand = @ProbeRecipeOnHand,
	ProbeRecipeExpectedDueDate = @ProbeRecipeExpectedDueDate,
	ProbeRecipeComments = @ProbeRecipeComments,
	ProcessChangeDetailsOnHand = @ProcessChangeDetailsOnHand,
	ProcessChangeDetailsDueDate = @ProcessChangeDetailsDueDate,
	LastUpdateBy = @LastUpdateBy,
	LastUpdateDate = GETDATE(),
	LotStartCheckListComments = @LotStartCheckListComments,
	PCRBNUmber = @PCRBNumber
WHERE
	ID = @LTWorkRequestID
END
ELSE BEGIN -- Cannot update because it was not locked by the user
INSERT INTO
	EventLog (
		IssueID,
		UserID,
		DocumentType,
		OperationType,
		Comments
	)
VALUES
	(
		@LTWorkRequestID,
		@LoginID,
		'WorkRequest',
		'Update',
		'Not able to update'
	)
SET
	@Result = -1
END
END
GO