﻿USE [FabApprovalSystem]
GO
	/****** Object:  UserDefinedFunction [dbo].[fn_GetRole_MSuffix]    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_GetRole_MSuffix] (
		-- Add the parameters for the function here
		@IssueID INT,
		@CurrentDate DATETIME
	) RETURNS TABLE AS RETURN (
		-- Add the SELECT statement with parameter references here
		-- 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 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