﻿USE [FabApprovalSystem]
GO
  /****** Object:  StoredProcedure [dbo].[MRBUpdateLot]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
  ANSI_NULLS ON
GO
SET
  QUOTED_IDENTIFIER ON
GO
  -- All the new routines/scripts below added by RJK =========================================
  CREATE PROCEDURE [dbo].[MRBUpdateLot] @LotID INT,
  @LotNumber VARCHAR(50),
  @DieLotNumber VARCHAR(50),
  @DispoType CHAR(1),
  @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 AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
SET
  NOCOUNT ON;

-- Insert statements for procedure here
UPDATE
  MRBLot
SET
  LotNumber = @LotNumber,
  DieLotNumber = @DieLotNumber,
  Description = @Description,
  DispoType = @DispoType,
  NewPartNo = @NewPartNo,
  WipPartNo = @WipPartNo,
  DiePartNo = @DiePartNo,
  ProductFamily = @ProductFamily,
  Gen = @Gen,
  Channel = @Channel,
  HexSize = @HexSize,
  Voltage = @Voltage,
  WaferCount = @WaferCount,
  DieCount = @DieCount,
  Location = @Location,
  TotalCost = @TotalCost
WHERE
  LotID = @LotID DECLARE @ParentLotNumber VARCHAR(50)
SELECT
  @ParentLotNumber = WP_LOT_FROM
FROM
  vFAB2SPN_WP_RECORD
WHERE
  WP_LOT_NO = @LotNumber IF EXISTS(
    SELECT
      MS.*
    FROM
      MRBLotsToBeSentToSPN MS
      INNER JOIN MRBLot M ON MS.LotNumber = M.LotNumber
      AND MS.MRBNumber = M.MRBNumber
    WHERE
      M.LotID = @LotID
      AND SentToSPN = 1
  ) BEGIN
UPDATE
  MRBLotsToBeSentToSPN
SET
  DispoType = @DispoType,
  IsDirty = 1,
  AddRemoveChangeMRBFlag = 'C',
  ParentLotNumber = @ParentLotNumber
WHERE
  LotNumber = @LotNumber
  AND MRBNumber = (
    SELECT
      MRBNumber
    FROM
      MRBLot
    WHERE
      LotID = @LotID
  )
END
ELSE BEGIN
UPDATE
  MRBLotsToBeSentToSPN
SET
  DispoType = @DispoType,
  IsDirty = 1,
  ParentLotNumber = @ParentLotNumber
WHERE
  LotNumber = @LotNumber
  AND MRBNumber = (
    SELECT
      MRBNumber
    FROM
      MRBLot
    WHERE
      LotID = @LotID
  )
END
END
GO