﻿USE [FabApprovalSystem]
GO
      /****** Object:  StoredProcedure [dbo].[MRBInsertLot_prev]    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].[MRBInsertLot_prev] @LotID INT OUTPUT,
      @LotNumber VARCHAR(50),
      @DispoType CHAR(1),
      @Status CHAR(1),
      @DieLotNumber VARCHAR(50),
      @MRBNumber 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 AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
SET
      NOCOUNT ON;

DECLARE @TotalLotCount INT DECLARE @TotalWaferCount INT DECLARE @TotalDieCount INT
DELETE FROM
      MRBLot
WHERE
      LotNumber = @LotNumber
      AND MRBNumber = @MRBNumber
INSERT INTO
      MRBLot (
            LotNumber,
            DieLotNumber,
            Status,
            DispoType,
            MRBNumber,
            Description,
            NewPartNo,
            WipPartNo,
            DiePartNo,
            ProductFamily,
            Gen,
            Channel,
            HexSize,
            Voltage,
            WaferCount,
            DieCount,
            Location,
            TotalCost
      )
VALUES
      (
            @LotNumber,
            @DieLotNumber,
            @Status,
            @DispoType,
            @MRBNumber,
            @Description,
            @NewPartNo,
            @WipPartNo,
            @DiePartNo,
            @ProductFamily,
            @Gen,
            @Channel,
            @HexSize,
            @Voltage,
            @WaferCount,
            @DieCount,
            @Location,
            @TotalCost
      )
END
SET
      @LotID = CAST(SCOPE_IDENTITY() AS INT)
SELECT
      @TotalLotCount = COUNT(*),
      @TotalWaferCount = SUM(ISNULL(WaferCount, 0)),
      @TotalDieCount = SUM(ISNULL(DieCount, 0))
FROM
      [MRBLot]
WHERE
      MRBNumber = @MRBNumber
UPDATE
      MRB
SET
      NumberOfLotsAffected = ISNULL(@TotalLotCount, 0),
      NumberOfWafersAffected = ISNULL(@TotalWaferCount, 0),
      NumberOfDiesAffected = ISNULL(@TotalDieCOunt, 0)
WHERE
      MRBNumber = @MRBNumber
GO