﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[PartsRequestGetList]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[PartsRequestGetList] AS BEGIN
SELECT
	[PRNumber],
	[Title],
	[CreateDate],
	[SubmitDate],
	[CloseDate],
	[OriginatorID],
	[RequestorID],
	[TechLeadID],
	[Description],
	[LastUpdateDate],
	[CurrentStep],
	CASE
		WHEN EXISTS (
			SELECT
				1
			FROM
				Approval a
			WHERE
				a.IssueID = PRNumber
				AND a.DocumentTypeID = 10
				AND ItemStatus = 2
		) THEN 'Denied'
		ELSE [Status]
	END AS [Status],
	Originator.FirstName + ' ' + Originator.LastName AS [Originator],
	Requestor.FirstName + ' ' + Requestor.LastName AS [Requestor],
	TechLead.FirstName + ' ' + TechLead.LastName AS [TechLead],
	dbo.fnGetApprovalFullNames(PRNumber, 10, ' / ', 0, 1, 1, 0, 0) AS 'PendingApprovers'
FROM
	PartsRequest
	LEFT OUTER JOIN Users AS Originator ON Originator.UserID = PartsRequest.OriginatorID
	LEFT OUTER JOIN Users AS Requestor ON Requestor.UserID = PartsRequest.RequestorID
	LEFT OUTER JOIN Users AS TechLead ON TechLead.UserID = PartsRequest.TechLeadID
WHERE
	CurrentStep >= 0
END
GO