﻿USE [FabApprovalSystem]
GO
	/****** Object:  UserDefinedFunction [dbo].[fnGetStep2Roles_RhMaQdbPeMRB]    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].[fnGetStep2Roles_RhMaQdbPeMRB] (@IssueID INT, @CurrentDate DATETIME) RETURNS TABLE AS RETURN (
		-- Add the SELECT statement with parameter references here
		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
			AND (
				SRC.SubRoleCategoryItem <> 'Facilities'
				AND SRC.SubRoleCategoryItem <> 'Plant Manager'
				AND SRC.SubRoleCategoryItem <> 'M_Suffix'
			)
			INNER JOIN LotDispoDepartment LDD ON LD.IssueID = LDD.IssueID
			INNER JOIN UserSubRole USR ON SR.SubRoleID = USR.SubRoleID
			INNER JOIN Users U ON U.UserID = USR.UserID
		WHERE
			LD.IssueID = @IssueID
	)
GO