﻿USE [FabApprovalSystem]
GO
	/****** Object:  UserDefinedFunction [dbo].[fn_GetLotDispositionsByUser]    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_GetLotDispositionsByUser] (
		-- 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 'LotDisposition' AS DocumentType,
			'LotDisposition' AS SubDoc,
			*
		FROM
			(
				SELECT
					DISTINCT LD.IssueID,
					LD.IssueID AS DocID,
					IssueDate,
					ISNULL(SQ.LotNo, '') AS LotNos,
					ISNULL(pdg.Names, '') AS 'PendingApprovers',
					Title,
					IssueDescription,
					U2.FirstName + ' ' + U2.LastName AS Originator,
					SubmitedDate,
					CloseDate,
					A.ItemStatus,
					NULL AS NextDueDate
				FROM
					LotDisposition LD
					INNER JOIN Approval A ON A.IssueID = LD.IssueID
					AND DocumentTypeID = 1
					LEFT JOIN Users U2 ON LD.OriginatorID = U2.UserID
					INNER JOIN Users U1 ON A.UserID = U1.UserID
					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 LD.IssueID,
							STUFF (
								(
									SELECT
										'/' + CAST(U.FirstName AS varchar(50)) + ' ' + CAST(U.LastName AS varchar(50)) + '(' + A.RoleName + ')'
									FROM
										Approval A
										INNER JOIN Users U ON A.UserID = U.UserID
									WHERE
										DocumentTypeID = 1
										AND A.IssueID = LD.IssueID
										AND ItemStatus = 0 FOR XML PATH('')
								),
								1,
								1,
								''
							) AS Names
						FROM
							LotDisposition LD
					) AS pdg ON LD.IssueID = pdg.IssueID
				WHERE
					A.UserID = @UserID
					AND (
						ItemStatus = 0
						OR (
							ItemStatus = 2
							AND DisplayDeniedDocument = 1
						)
					)
				UNION
				ALL
				SELECT
					DISTINCT LD.IssueID,
					LD.IssueID AS DocID,
					IssueDate,
					ISNULL(SQ.LotNo, '') AS LotNos,
					ISNULL(pdg.Names, '') AS 'PendingApprovers',
					Title,
					IssueDescription,
					U1.FirstName + ' ' + U1.LastName AS Originator,
					SubmitedDate,
					CloseDate,
					AP.ItemStatus,
					NULL AS NextDueDate
				FROM
					LotDisposition LD
					INNER JOIN Users U1 ON LD.OriginatorID = U1.UserID
					LEFT JOIN Approval AP ON LD.IssueID = 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 LD.IssueID,
							STUFF (
								(
									SELECT
										'/' + CAST(U.FirstName AS varchar(50)) + ' ' + CAST(U.LastName AS varchar(50)) + '(' + A.RoleName + ')'
									FROM
										Approval A
										INNER JOIN Users U ON A.UserID = U.UserID
									WHERE
										DocumentTypeID = 1
										AND A.IssueID = LD.IssueID
										AND ItemStatus = 0 FOR XML PATH('')
								),
								1,
								1,
								''
							) AS Names
						FROM
							LotDisposition LD
					) AS pdg ON LD.IssueID = pdg.IssueID
				WHERE
					U1.UserID = @UserID
					AND LD.IssueID NOT IN (
						SELECT
							IssueID
						FROM
							Approval
						WHERE
							IssueID = LD.IssueID
							AND ItemStatus <> 2
							AND DocumentTypeID = 1
					)
			) AS A
	)
GO