﻿USE [FabApprovalSystem]
GO
	/****** Object:  UserDefinedFunction [dbo].[fn_GetLTWorkRequestsByUser]    Script Date: 11/21/2024 11:31:55 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE FUNCTION [dbo].[fn_GetLTWorkRequestsByUser] (
		-- Add the parameters for the function here
		@UserID INT
	) RETURNS TABLE AS RETURN (
		-- Add the SELECT statement with parameter references here
		-- M_Suffix
		SELECT
			DISTINCT 'LotTraveler' AS DocumentType,
			'LotTraveler' AS SubDoc,
			*
		FROM
			(
				SELECT
					DISTINCT W.SWRNumber AS IssueID,
					W.ID AS DocID,
					W.OriginatedDate AS IssueDate,
					W.LotNumber AS LotNos,
					pdg.Names AS 'PendingApprovers',
					Title,
					PurposeOfRequest,
					U2.FirstName + ' ' + U2.LastName AS Originator,
					SubmitedDate,
					CloseDate,
					A.ItemStatus,
					NULL AS NextDueDate
				FROM
					LTWorkRequest W WITH(NOLOCK)
					INNER JOIN Approval A WITH(NOLOCK) ON A.IssueID = W.ID
					AND DocumentTypeID = 6
					INNER JOIN Users U2 WITH(NOLOCK) ON W.OriginatorID = U2.UserID
					INNER JOIN Users U1 WITH(NOLOCK) ON A.UserID = U1.UserID --LEFT JOIN 
					--(SELECT   DISTINCT  M.MRBNumber, STUFF
					--							   ((SELECT '/' + CAST(L.LotNumber AS varchar(512))
					--								   FROM         MRBLot L
					--								   WHERE     L.MRBNumber = M.MRBNumber FOR XML PATH('')), 1, 1, '') AS LotNo
					--							FROM MRB M) AS SQ
					--ON M.MRBNumber = SQ.MRBNumber
					LEFT JOIN (
						SELECT
							DISTINCT W.ID,
							STUFF (
								(
									SELECT
										'/' + CAST(U.FirstName AS varchar(50)) + ' ' + CAST(U.LastName AS varchar(50)) + '(' + A.RoleName + ')'
									FROM
										Approval A WITH(NOLOCK)
										INNER JOIN Users U WITH(NOLOCK) ON A.UserID = U.UserID
									WHERE
										DocumentTypeID = 6
										AND A.IssueID = W.ID
										AND ItemStatus = 0 FOR XML PATH('')
								),
								1,
								1,
								''
							) AS Names
						FROM
							LTWorkRequest W WITH(NOLOCK)
					) AS pdg ON W.ID = pdg.ID
				WHERE
					A.UserID = @UserID
					AND (ItemStatus = 0)
					AND IsCurrentRevision = 1
				UNION
				ALL
				SELECT
					DISTINCT W.SWRNumber AS IssueID,
					W.ID AS DocID,
					W.OriginatedDate AS IssueDate,
					W.LotNumber AS LotNos,
					ISNULL(pdg.Names, '') AS 'PendingApprovers',
					Title,
					PurposeOfRequest,
					U1.FirstName + ' ' + U1.LastName AS Originator,
					SubmitedDate,
					CloseDate,
					AP.ItemStatus,
					NULL AS NextDueDate
				FROM
					LTWorkRequest W WITH(NOLOCK)
					INNER JOIN Users U1 WITH(NOLOCK) ON W.OriginatorID = U1.UserID
					LEFT JOIN Approval AP WITH(NOLOCK) ON W.ID = AP.IssueID --LEFT JOIN 
					--(SELECT   DISTINCT  LD.IssueID, STUFF
					--							   ((SELECT '/' + CAST(L.LotNumber AS varchar(512))
					--								   FROM         Lot L
					--								   WHERE     L.IssueID = LD.IssueID FOR XML PATH('')), 1, 1, '') AS LotNo
					--							FROM LotDisposition LD) AS SQ
					--ON LD.IssueID = SQ.IssueID 
					LEFT JOIN (
						SELECT
							DISTINCT W.ID,
							STUFF (
								(
									SELECT
										'/' + CAST(U.FirstName AS varchar(50)) + ' ' + CAST(U.LastName AS varchar(50)) + '(' + A.RoleName + ')'
									FROM
										Approval A WITH(NOLOCK)
										INNER JOIN Users U WITH(NOLOCK) ON A.UserID = U.UserID
									WHERE
										DocumentTypeID = 6
										AND A.IssueID = W.ID
										AND ItemStatus = 0 FOR XML PATH('')
								),
								1,
								1,
								''
							) AS Names
						FROM
							LTWorkRequest W WITH(NOLOCK)
					) AS pdg ON W.ID = pdg.ID
				WHERE
					U1.UserID = @UserID
					AND W.ID NOT IN (
						SELECT
							IssueID
						FROM
							Approval WITH(NOLOCK)
						WHERE
							IssueID = W.ID
							AND ItemStatus <> 2
							AND DocumentTypeID = 6
					)
					AND IsCurrentRevision = 1
			) AS A
	)
GO