﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[MRBInsertChildLot_NotInTheMRB]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[MRBInsertChildLot_NotInTheMRB] -- Add the parameters for the stored procedure here
	@MRBNumber INT,
	@LotNumber VARCHAR(10),
	@DispoType CHAR(1) = NULL,
	@ParentIsDirty BIT,
	@ParentLotNumber VARCHAR(10) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @ParentAddRemoveChangeMRBFlag CHAR(1) DECLARE @ChildAddRemoveChangeMRBFlag CHAR(1)
SELECT
	@ParentIsDirty = IsDirty,
	@DispoType = DispoType,
	@ParentAddRemoveChangeMRBFlag = AddRemoveChangeMRBFlag
FROM
	MRBLotsToBeSentToSPN
WHERE
	LotNumber = @ParentLotNumber
	AND MRBNumber = @MRBNumber -- Insert into table so that Lot in the Lot Disposition Table can be associated to the MRB
DELETE FROM
	MRBChildLotNotInMRB
WHERE
	LotNumber = @LotNumber
	AND MRBNumber = @MRBNumber -- Do not insert the lot into the MRBChildLotNotInMRB table if the Parent was a DELETE
	IF @ParentAddRemoveChangeMRBFlag <> 'R' BEGIN
INSERT INTO
	MRBChildLotNotInMRB (LotNumber, ParentLotNumber, MRBNumber, DispoType)
VALUES
	(
		@LotNumber,
		@ParentLotNumber,
		@MRBNumber,
		@DispoType
	)
END -- Insert Lot into a table that is used for sending to SPN
IF EXISTS(
	SELECT
		*
	FROM
		MRBLotsToBeSentToSPN
	WHERE
		LotNumber = @LotNumber
		AND MRBNumber = @MRBNumber
) BEGIN -- Do not need to have the if/else. could have set the IsDirty = @ParentIsDirty
IF @ParentIsDirty = 1 BEGIN
UPDATE
	MRBLotsToBeSentToSPN
SET
	DispoType = @DispoType,
	IsDirty = 1 -- keep the AddRemoveChangeMRBFlag same
WHERE
	LotNumber = @LotNumber
	AND MRBNumber = @MRBNumber
END
ELSE BEGIN
UPDATE
	MRBLotsToBeSentToSPN
SET
	DispoType = @DispoType,
	IsDirty = 0 -- keep the AddRemoveChangeMRBFlag same
WHERE
	LotNumber = @LotNumber
	AND MRBNumber = @MRBNumber
END
END
ELSE BEGIN --Even though its an Insert, The AddRemoveChangeMRBFlag needs to be set to "C" because this 
-- lot  already has a Dispo Type in SPN which is, carried over from the parent lot
IF @ParentIsDirty = 1 BEGIN -- If the Parent has been deleted and ther is a split in SPN
-- then set the flag to 'R' to delete the child lot in SPN
IF @ParentAddRemoveChangeMRBFlag = 'R'
SET
	@ChildAddRemoveChangeMRBFlag = 'R'
	ELSE
SET
	@ChildAddRemoveChangeMRBFlag = 'C'
INSERT INTO
	MRBLotsToBeSentToSPN (
		LotNumber,
		ParentLotNumber,
		MRBNumber,
		DispoType,
		AddRemoveChangeMRBFlag,
		IsDirty
	)
VALUES
	(
		@LotNumber,
		@ParentLotNumber,
		@MRBNumber,
		@DispoType,
		@ChildAddRemoveChangeMRBFlag,
		1
	)
END
ELSE -- Even though this lot will not be sent to SPN, 
-- need to keep a record of this lot in this tble to reference it with the MRBChildLotNotInMRB table
BEGIN
INSERT INTO
	MRBLotsToBeSentToSPN (
		LotNumber,
		ParentLotNumber,
		MRBNumber,
		DispoType,
		AddRemoveChangeMRBFlag,
		IsDirty
	)
VALUES
	(
		@LotNumber,
		@ParentLotNumber,
		@MRBNumber,
		@DispoType,
		'C',
		0
	)
END
END
END
GO