﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[PartsRequestGetListByUser]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[PartsRequestGetListByUser] @UserID int AS BEGIN
SELECT
	PR.PRNumber,
	Title,
	[Description],
	CreateDate,
	SubmitDate,
	CloseDate,
	U2.FirstName + ' ' + U2.LastName AS Originator,
	U1.FirstName + ' ' + U1.LastName AS Requestor,
	U3.FirstName + ' ' + U3.LastName AS TechLead,
	CASE
		WHEN EXISTS (
			SELECT
				1
			FROM
				Approval a
			WHERE
				a.IssueID = PR.PRNumber
				AND a.DocumentTypeID = 10
				AND ItemStatus = 2
		) THEN 'Denied'
		ELSE [Status]
	END AS [Status],
	dbo.fnGetApprovalFullNames(PR.PRNumber, 10, ' / ', 0, 1, 1, 0, 0) AS 'PendingApprovers',
	ws.WorkFlowStepName
FROM
	PartsRequest PR
	INNER JOIN Workflows w ON w.DocumentTypeID = 10
	AND w.WorkFlowNumber = 1
	LEFT OUTER JOIN WorkflowSteps ws ON ws.WorkflowID = w.WorkflowID
	AND ws.WorkflowStepNumber = pr.CurrentStep
	LEFT OUTER JOIN Users U3 ON PR.TechLeadID = U3.UserID
	INNER JOIN Users U2 ON PR.OriginatorID = U2.UserID
	LEFT OUTER JOIN Users U1 ON PR.RequestorID = U1.UserID
WHERE
	CloseDate IS NULL
	AND CurrentStep >= 0
END
GO