﻿USE [FabApprovalSystem]
GO
      /****** Object:  StoredProcedure [dbo].[UpdateScrapLotAll]    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].[UpdateScrapLotAll] @IssueID INT,
      @LotStatus INT,
      @ScrapCount INT,
      @Lot1State TINYINT,
      @Lot2State TINYINT,
      @Lot3State TINYINT,
      @Lot4State TINYINT,
      @Lot5State TINYINT,
      @Lot6State TINYINT,
      @Lot7State TINYINT,
      @Lot8State TINYINT,
      @Lot9State TINYINT,
      @Lot10State TINYINT,
      @Lot11State TINYINT,
      @Lot12State TINYINT,
      @Lot13State TINYINT,
      @Lot14State TINYINT,
      @Lot15State TINYINT,
      @Lot16State TINYINT,
      @Lot17State TINYINT,
      @Lot18State TINYINT,
      @Lot19State TINYINT,
      @Lot20State TINYINT,
      @Lot21State TINYINT,
      @Lot22State TINYINT,
      @Lot23State TINYINT,
      @Lot24State TINYINT,
      @Lot25State TINYINT,
      @Lot26State TINYINT,
      @Lot27State TINYINT,
      @Lot28State TINYINT,
      @Lot29State TINYINT,
      @Lot30State TINYINT,
      @Lot31State TINYINT,
      @Lot32State TINYINT,
      @Lot33State TINYINT,
      @Lot34State TINYINT,
      @Lot35State TINYINT,
      @Lot36State TINYINT,
      @Lot37State TINYINT,
      @Lot38State TINYINT,
      @Lot39State TINYINT,
      @Lot40State TINYINT,
      @Lot41State TINYINT,
      @Lot42State TINYINT,
      @Lot43State TINYINT,
      @Lot44State TINYINT,
      @Lot45State TINYINT,
      @Lot46State TINYINT,
      @Lot47State TINYINT,
      @Lot48State TINYINT,
      @Lot49State TINYINT,
      @Lot50State TINYINT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
SET
      NOCOUNT ON;

UPDATE
      Lot
SET
      LotStatusOptionID = @LotStatus
WHERE
      IssueID = @IssueID IF @LotStatus = 1 -- Release
UPDATE
      S
SET
      ReleaseCount = CASE
            WHEN L.WaferCount > 0 THEN L.WaferCount
            ELSE L.DieCount
      END,
      ScrapCount = 0
FROM
      ScrapLot S
      INNER JOIN Lot L ON S.IssueID = L.IssueID
      AND S.LotNo = L.LotNumber
WHERE
      S.IssueID = @IssueID
      ELSE IF @LotStatus = 2 -- Scrap
      BEGIN
UPDATE
      S
SET
      ScrapCount = CASE
            WHEN L.WaferCount > 0 THEN L.WaferCount
            ELSE L.DieCount
      END,
      ReleaseCount = 0
FROM
      ScrapLot S
      INNER JOIN Lot L ON S.IssueID = L.IssueID
      AND S.LotNo = L.LotNumber
WHERE
      S.IssueID = @IssueID
END
ELSE IF @LotStatus = 3
OR @LotStatus = 5
OR @LotStatus = 7 -- Not Avaialble OR M_SUFFIX OR SPlit Off Hold
BEGIN
UPDATE
      S
SET
      ScrapCount = 0,
      ReleaseCount = 0
FROM
      ScrapLot S
      INNER JOIN Lot L ON S.IssueID = L.IssueID
      AND S.LotNo = L.LotNumber
WHERE
      S.IssueID = @IssueID
END
UPDATE
      ScrapLot
SET
      Lot1State = @Lot1State,
      Lot2State = @Lot2State,
      Lot3State = @Lot3State,
      Lot4State = @Lot4State,
      Lot5State = @Lot5State,
      Lot6State = @Lot6State,
      Lot7State = @Lot7State,
      Lot8State = @Lot8State,
      Lot9State = @Lot9State,
      Lot10State = @Lot10State,
      Lot11State = @Lot11State,
      Lot12State = @Lot12State,
      Lot13State = @Lot13State,
      Lot14State = @Lot14State,
      Lot15State = @Lot15State,
      Lot16State = @Lot16State,
      Lot17State = @Lot17State,
      Lot18State = @Lot18State,
      Lot19State = @Lot19State,
      Lot20State = @Lot20State,
      Lot21State = @Lot21State,
      Lot22State = @Lot22State,
      Lot23State = @Lot23State,
      Lot24State = @Lot24State,
      Lot25State = @Lot25State,
      Lot26State = @Lot26State,
      Lot27State = @Lot27State,
      Lot28State = @Lot28State,
      Lot29State = @Lot29State,
      Lot30State = @Lot30State,
      Lot31State = @Lot31State,
      Lot32State = @Lot32State,
      Lot33State = @Lot33State,
      Lot34State = @Lot34State,
      Lot35State = @Lot35State,
      Lot36State = @Lot36State,
      Lot37State = @Lot37State,
      Lot38State = @Lot38State,
      Lot39State = @Lot39State,
      Lot40State = @Lot40State,
      Lot41State = @Lot41State,
      Lot42State = @Lot42State,
      Lot43State = @Lot43State,
      Lot44State = @Lot44State,
      Lot45State = @Lot45State,
      Lot46State = @Lot46State,
      Lot47State = @Lot47State,
      Lot48State = @Lot48State,
      Lot49State = @Lot49State,
      Lot50State = @Lot50State
WHERE
      IssueID = @IssueID
UPDATE
      LotDisposition
SET
      LastUpdateDate = GETDATE()
WHERE
      IssueID = @IssueID
END
GO