﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[PartsRequestDelete]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[PartsRequestDelete] @UserID INT,
	@PRNumber INT AS BEGIN
SET
	NOCOUNT ON;

IF EXISTS (
	SELECT
		1
	FROM
		PartsRequest
	WHERE
		PRNumber = @PRNumber
		AND CloseDate IS NOT NULL
) BEGIN RAISERROR('Cannot delete closed parts request', 16, 1) RETURN
END
UPDATE
	PartsRequest
SET
	CurrentStep = -1
WHERE
	PRNumber = @PRNumber
	AND CloseDate IS NULL IF @ @ROWCOUNT = 0 BEGIN RAISERROR('Error deleting parts request', 16, 1) RETURN
END
INSERT INTO
	EventLog (
		UserID,
		IssueID,
		DocumentType,
		OperationType,
		Comments
	)
VALUES
	(
		@UserID,
		@PRNumber,
		'PR',
		'Delete',
		'Deleted the document'
	)
END
GO