﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[InsertLotDispositionLot]    Script Date: 11/21/2024 11:29:04 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[InsertLotDispositionLot] @LotID INT OUTPUT,
	@LotNumber VARCHAR(50),
	@DieLotNumber VARCHAR(50),
	@IssueID INT,
	@Description VARCHAR(100),
	@NewPartNo VARCHAR(50),
	@WipPartNo VARCHAR(50),
	@DiePartNo VARCHAR(50),
	@ProductFamily CHAR(10),
	@Gen VARCHAR(10),
	@Channel VARCHAR(50),
	@HexSize VARCHAR(10),
	@Voltage FLOAT,
	@WaferCount INT,
	@DieCount INT,
	@Location VARCHAR(50),
	@TotalCost FLOAT,
	@LotStatusOptionID INT,
	@QualityCode NCHAR(10),
	@OpenIssueWithExistingLot INT OUTPUT,
	@ExistingRowUpdated bit = NULL OUTPUT AS BEGIN -- 2019-05-10 TR: added code to update existing Lot and fix defect with ScrapLot code, #35349
SET
	NOCOUNT ON;

DECLARE @ReleaseCount INT DECLARE @ScrapCount INT
SET
	@OpenIssueWithExistingLot = 0 IF EXISTS (
		SELECT
			LD.*
		FROM
			Lot L
			INNER JOIN LotDisposition LD ON L.IssueID = LD.IssueID
		WHERE
			L.LotNumber = @LotNumber
			AND CloseDate IS NULL
	) BEGIN
SELECT
	@OpenIssueWithExistingLot = LD.IssueID
FROM
	Lot L
	INNER JOIN LotDisposition LD ON L.IssueID = LD.IssueID
WHERE
	L.LotNumber = @LotNumber
	AND CloseDate IS NULL RETURN
END IF EXISTS (
	SELECT
		1
	FROM
		Lot
	WHERE
		LotNumber = @LotNumber
		AND IssueID = @IssueID
) BEGIN
UPDATE
	Lot
SET
	DieLotNumber = @DieLotNumber,
	Description = @Description,
	NewPartNo = @NewPartNo,
	WipPartNo = @WipPartNo,
	DiePartNo = @DiePartNo,
	ProductFamily = @ProductFamily,
	Gen = @Gen,
	Channel = @Channel,
	HexSize = @HexSize,
	Voltage = @Voltage,
	WaferCount = @WaferCount,
	DieCount = @DieCount,
	Location = @Location,
	TotalCost = @TotalCost,
	QualityCode = @QualityCode,
	LotStatusOptionID = @LotStatusOptionID
WHERE
	LotNumber = @LotNumber
	AND IssueID = @IssueID
SET
	@ExistingRowUpdated = 1
END
ELSE BEGIN
INSERT INTO
	Lot (
		LotNumber,
		DieLotNumber,
		IssueID,
		Description,
		NewPartNo,
		WipPartNo,
		DiePartNo,
		ProductFamily,
		Gen,
		Channel,
		HexSize,
		Voltage,
		WaferCount,
		DieCount,
		Location,
		TotalCost,
		QualityCode,
		LotStatusOptionID
	)
VALUES
	(
		@LotNumber,
		@DieLotNumber,
		@IssueID,
		@Description,
		@NewPartNo,
		@WipPartNo,
		@DiePartNo,
		@ProductFamily,
		@Gen,
		@Channel,
		@HexSize,
		@Voltage,
		@WaferCount,
		@DieCount,
		@Location,
		@TotalCost,
		@QualityCode,
		@LotStatusOptionID
	)
SET
	@ExistingRowUpdated = 0
END
SET
	@LotID = CAST(SCOPE_IDENTITY() AS INT) DECLARE @TempCount INT IF @DieCount > 0
SET
	@TempCount = @DieCount IF @WaferCount > 0
SET
	@TempCount = @WaferCount IF @LotStatusOptionID = 1 -- Release
INSERT INTO
	ScrapLot(
		LotNo,
		IssueID,
		ReleaseCount,
		Lot1State,
		Lot2State,
		Lot3State,
		Lot4State,
		Lot5State,
		Lot6State,
		Lot7State,
		Lot8State,
		Lot9State,
		Lot10State,
		Lot11State,
		Lot12State,
		Lot13State,
		Lot14State,
		Lot15State,
		Lot16State,
		Lot17State,
		Lot18State,
		Lot19State,
		Lot20State,
		Lot21State,
		Lot22State,
		Lot23State,
		Lot24State,
		Lot25State,
		Lot26State,
		Lot27State,
		Lot28State,
		Lot29State,
		Lot30State,
		Lot31State,
		Lot32State,
		Lot33State,
		Lot34State,
		Lot35State,
		Lot36State,
		Lot37State,
		Lot38State,
		Lot39State,
		Lot40State,
		Lot41State,
		Lot42State,
		Lot43State,
		Lot44State,
		Lot45State,
		Lot46State,
		Lot47State,
		Lot48State,
		Lot49State,
		Lot50State
	)
VALUES
	(
		@LotNumber,
		@IssueID,
		@TempCount,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1,
		1
	)
	ELSE IF @LotStatusOptionID = 2 -- Scrap
INSERT INTO
	ScrapLot(
		LotNo,
		IssueID,
		ScrapCount,
		Lot1State,
		Lot2State,
		Lot3State,
		Lot4State,
		Lot5State,
		Lot6State,
		Lot7State,
		Lot8State,
		Lot9State,
		Lot10State,
		Lot11State,
		Lot12State,
		Lot13State,
		Lot14State,
		Lot15State,
		Lot16State,
		Lot17State,
		Lot18State,
		Lot19State,
		Lot20State,
		Lot21State,
		Lot22State,
		Lot23State,
		Lot24State,
		Lot25State,
		Lot26State,
		Lot27State,
		Lot28State,
		Lot29State,
		Lot30State,
		Lot31State,
		Lot32State,
		Lot33State,
		Lot34State,
		Lot35State,
		Lot36State,
		Lot37State,
		Lot38State,
		Lot39State,
		Lot40State,
		Lot41State,
		Lot42State,
		Lot43State,
		Lot44State,
		Lot45State,
		Lot46State,
		Lot47State,
		Lot48State,
		Lot49State,
		Lot50State
	)
VALUES
	(
		@LotNumber,
		@IssueID,
		@TempCount,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2
	)
	ELSE IF @LotStatusOptionID = 4 -- M_Suffix
INSERT INTO
	ScrapLot(
		LotNo,
		IssueID,
		ReleaseCount,
		Lot1State,
		Lot2State,
		Lot3State,
		Lot4State,
		Lot5State,
		Lot6State,
		Lot7State,
		Lot8State,
		Lot9State,
		Lot10State,
		Lot11State,
		Lot12State,
		Lot13State,
		Lot14State,
		Lot15State,
		Lot16State,
		Lot17State,
		Lot18State,
		Lot19State,
		Lot20State,
		Lot21State,
		Lot22State,
		Lot23State,
		Lot24State,
		Lot25State,
		Lot26State,
		Lot27State,
		Lot28State,
		Lot29State,
		Lot30State,
		Lot31State,
		Lot32State,
		Lot33State,
		Lot34State,
		Lot35State,
		Lot36State,
		Lot37State,
		Lot38State,
		Lot39State,
		Lot40State,
		Lot41State,
		Lot42State,
		Lot43State,
		Lot44State,
		Lot45State,
		Lot46State,
		Lot47State,
		Lot48State,
		Lot49State,
		Lot50State
	)
VALUES
	(
		@LotNumber,
		@IssueID,
		@TempCount,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4,
		4
	)
	ELSE IF @LotStatusOptionID = 6 -- Close T0 QDB
INSERT INTO
	ScrapLot(
		LotNo,
		IssueID,
		ReleaseCount,
		Lot1State,
		Lot2State,
		Lot3State,
		Lot4State,
		Lot5State,
		Lot6State,
		Lot7State,
		Lot8State,
		Lot9State,
		Lot10State,
		Lot11State,
		Lot12State,
		Lot13State,
		Lot14State,
		Lot15State,
		Lot16State,
		Lot17State,
		Lot18State,
		Lot19State,
		Lot20State,
		Lot21State,
		Lot22State,
		Lot23State,
		Lot24State,
		Lot25State,
		Lot26State,
		Lot27State,
		Lot28State,
		Lot29State,
		Lot30State,
		Lot31State,
		Lot32State,
		Lot33State,
		Lot34State,
		Lot35State,
		Lot36State,
		Lot37State,
		Lot38State,
		Lot39State,
		Lot40State,
		Lot41State,
		Lot42State,
		Lot43State,
		Lot44State,
		Lot45State,
		Lot46State,
		Lot47State,
		Lot48State,
		Lot49State,
		Lot50State
	)
VALUES
	(
		@LotNumber,
		@IssueID,
		@TempCount,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6,
		6
	)
	ELSE
INSERT INTO
	ScrapLot(LotNo, IssueID)
VALUES
	(@LotNumber, @IssueID)
END
GO