﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[MRBDeleteLot]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[MRBDeleteLot] @LotID INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

-- Only set the Flags "R' if the record was previously sent to SPN
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
	MS
SET
	AddRemoveChangeMRBFlag = 'R',
	IsDirty = 1 --, DispoType = NULL
FROM
	MRBLotsToBeSentToSPN MS
	INNER JOIN MRBLot M ON MS.LotNumber = M.LotNumber
	AND MS.MRBNumber = M.MRBNumber
WHERE
	M.LotID = @LotID
END
ELSE -- -- If DELETED Before sending to SPN, Delete the record
BEGIN DELETE MS
FROM
	MRBLotsToBeSentToSPN MS
	INNER JOIN MRBLot M ON MS.LotNumber = M.LotNumber
	AND MS.MRBNumber = M.MRBNumber
WHERE
	M.LotID = @LotID
END
DELETE FROM
	MRBLot
WHERE
	LotID = @LotID
END
GO