﻿USE [FabApprovalSystem]
GO
	/****** Object:  UserDefinedFunction [dbo].[fnGetStep1Roles_PERequired]    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].[fnGetStep1Roles_PERequired] (
		-- Add the parameters for the function here
		@IssueID INT,
		@CurrentDate DATETIME
	) RETURNS TABLE AS RETURN (
		-- Add the SELECT statement with parameter references here
		SELECT
			DISTINCT IssueID,
			R.RoleName,
			SRC.SubRoleCategoryItem AS 'SubRole',
			U.UserID,
			SR.SubRoleID,
			0 AS 'ItemStatus',
			WS.WorkflowStepNumber AS 'Step',
			@CurrentDate AS 'AssignedDate',
			@CurrentDate AS 'NotifyDate',
			@CurrentDate AS 'RoleAssignedDate',
			1 AS ApprovalType
		FROM
			DocumentType D
			INNER JOIN Workflows W ON D.DocumentTypeID = W.DocumentTypeID
			INNER JOIN WorkFlowSteps WS ON W.WorkflowID = WS.WorkflowID
			INNER JOIN Role R ON WS.RoleID = R.RoleID
			INNER JOIN SubRole SR ON R.RoleID = SR.RoleID
			INNER JOIN SubRoleCategory SRC ON SR.SubRoleCategoryID = SRC.SubRoleCategoryID
			INNER JOIN ResponsibilityIssue RI ON LTRIM(RTRIM(SRC.SubRoleCategoryItem)) = LTRIM(RTRIM(RI.Issue))
			INNER JOIN LotDisposition LD ON RI.ResponsibilityIssueID = LD.ResponsibilityIssueID -- Get the em
			INNER JOIN UserSubRole USR ON SR.SubRoleID = USR.SubRoleID
			INNER JOIN Users U ON U.UserID = USR.UserID
		WHERE
			LD.IssueID = @IssueID
			AND WorkflowStepNumber = LD.CurrentStep
			AND DocumentType = 'LotDisposition'
		UNION
		ALL --YE and PE Rquired
		SELECT
			DISTINCT LD.IssueID,
			R.RoleName,
			SRC.SubRoleCategoryItem AS 'SubRole',
			U.UserID,
			SR.SubRoleID,
			0 AS 'ItemStatus',
			WS.WorkflowStepNumber AS 'Step',
			@CurrentDate AS 'AssignedDate',
			@CurrentDate AS 'NotifyDate',
			@CurrentDate AS 'RoleAssignedDate',
			1 AS ApprovalType
		FROM
			LotDisposition LD
			INNER JOIN WorkflowSteps WS ON LD.CurrentStep = WS.WorkflowStepNumber
			INNER JOIN Workflows W ON WS.WorkflowID = W.WorkflowID
			INNER JOIN DocumentType D ON W.DocumentTypeID = D.DocumentTypeID
			AND DocumentType = 'LotDisposition'
			INNER JOIN Role R ON WS.RoleID = R.RoleID
			INNER JOIN SubRole SR ON R.RoleID = SR.RoleID
			INNER JOIN SubRoleCategory SRC ON SR.SubRoleCategoryID = SRC.SubRoleCategoryID
			INNER JOIN Lot L ON LD.IssueID = L.IssueID
			INNER JOIN UserSubRole USR ON SR.SubRoleID = USR.SubRoleID
			INNER JOIN Users U ON U.UserID = USR.UserID
		WHERE
			LD.IssueID = @IssueID
			AND LTRIM(RTRIM(L.ProductFamily)) + '-G' + LTRIM(RTRIM(CONVERT(CHAR(10), L.Gen))) = SRC.SubRoleCategoryItem
		UNION
		ALL -- M_Suffix
		SELECT
			DISTINCT LD.IssueID,
			R.RoleName,
			SRC.SubRoleCategoryItem AS 'SubRole',
			U.UserID,
			SR.SubRoleID,
			0 AS 'ItemStatus',
			WS.WorkflowStepNumber AS 'Step',
			@CurrentDate AS 'AssignedDate',
			@CurrentDate AS 'NotifyDate',
			@CurrentDate AS 'RoleAssignedDate',
			1 AS ApprovalType
		FROM
			LotDisposition LD
			INNER JOIN WorkflowSteps WS ON LD.CurrentStep = WS.WorkflowStepNumber
			INNER JOIN Workflows W ON WS.WorkflowID = W.WorkflowID
			INNER JOIN DocumentType D ON W.DocumentTypeID = D.DocumentTypeID
			AND DocumentType = 'LotDisposition'
			INNER JOIN Role R ON WS.RoleID = R.RoleID
			INNER JOIN SubRole SR ON R.RoleID = SR.RoleID
			INNER JOIN SubRoleCategory SRC ON SR.SubRoleCategoryID = SRC.SubRoleCategoryID
			INNER JOIN Lot L ON LD.IssueID = L.IssueID
			INNER JOIN UserSubRole USR ON SR.SubRoleID = USR.SubRoleID
			INNER JOIN Users U ON U.UserID = USR.UserID
			INNER JOIN LotStatusOption LSO ON L.LotStatusOptionID = LSO.LotStatusOptionID
		WHERE
			LD.IssueID = @IssueID
			AND LSO.LotStatusOption = SRC.SubRoleCategoryItem
	)
GO