﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[InsertApprovers_04212018]    Script Date: 11/21/2024 11:29:04 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE PROCEDURE [dbo].[InsertApprovers_04212018] @IssueID INT,
	@Step INT,
	@DocumentTypeID INT,
	@SubRoleCategoriesClause VARCHAR(500),
	@ApproverCountForThisStep INT OUTPUT,
	@RHCount INT,
	@WorkFlowNumber INT = 1 AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @RowCount INT CREATE TABLE #TempApprovals (
[IssueID] [int] NULL,
[RoleName] [nvarchar](50) NULL,
[SubRole] [nvarchar](50) NOT NULL,
[UserID] [int] NOT NULL,
[SubRoleID] [int] NOT NULL,
[ItemStatus] [int] NULL,
[Step] [int] NULL,
[NotifyDate] [datetime] NULL,
[AssignedDate] [datetime] NULL,
[RoleAssignedDate] [datetime] NULL,
[ApprovalType] [tinyint] NULL
) DECLARE @CurrentDate DATETIME DECLARE @WorkflowStepName VARCHAR(50)
SET
	@CurrentDate = GETDATE() --DECLARE @RHCount INT
	DECLARE @CheckForITARCompliant INT
SET
	@CheckForITARCompliant = 0 IF @RHCount > 0
SET
	@CheckForITARCompliant = 1
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) EXEC GetRoles @WorkflowStepNumber = @Step,
	@WhereClause = @SubRoleCategoriesClause,
	@CheckForITARCompliant = @CheckForITARCompliant,
	@DocumentTypeID = @DocumentTypeID,
	@IssueID = @IssueID DECLARE @RoleExceptionName VARCHAR(50) DECLARE @RoleExceptionWorkFlowStepNumber VARCHAR(50) DECLARE @ApprovalType TINYINT DECLARE @AddtionalRoleID INT DECLARE @AddtionalRoleName VARCHAR(50) DECLARE @AdditionalSubRoleID INT DECLARE @AdditionalSubRoleName VARCHAR(50) DECLARE @UserID INT
SELECT
	@RoleExceptionName = R.RoleName,
	@RoleExceptionWorkFlowStepNumber = WorkflowStepNumber
FROM
	WorkflowSteps W
	INNER JOIN Role R ON W.RoleID = R.RoleID
	AND RulesApply = 1
	INNER JOIN Workflows Wf On Wf.WorkflowID = W.WorkflowID
	AND WorkFlowNumber = @WorkFlowNumber
WHERE
	DocumentTypeID = @DocumentTypeID -- Exception Roles WHERE the Role does not have fixed approvers. 
	-- The approver/s will be determined based on the issue
	IF @RoleExceptionWorkFlowStepNumber = @Step BEGIN IF @RoleExceptionName = 'Originator' BEGIN IF @DocumentTypeID = 6 --  Lot Traveler
	BEGIN
SELECT
	@UserID = OriginatorID
FROM
	LTWorkRequest
WHERE
	ID = @IssueID
END
SET
	@ApprovalType = 1
SELECT
	@AddtionalRoleID = R.RoleID,
	@AddtionalRoleName = R.RoleName,
	@AdditionalSubRoleID = SubRoleID,
	@AdditionalSubRoleName = SubRole
FROM
	[Role] R
	INNER JOIN SubRole SR ON R.RoleID = SR.RoleID
WHERE
	R.RoleName = @RoleExceptionName
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	)
VALUES
	(
		@IssueID,
		@AddtionalRoleName,
		@AdditionalSubRoleName,
		@UserID,
		@AdditionalSubRoleID,
		0,
		@Step,
		GETDATE(),
		GETDATE(),
		GETDATE(),
		@ApprovalType
	)
END
END -- INSERT INTO THE ACTUAL TABLE
SET
	@ApproverCountForThisStep = (
		SELECT
			COUNT(*)
		FROM
			#TempApprovals)
			IF @ApproverCountForThisStep > 0 BEGIN
		INSERT INTO
			Approval (
				IssueID,
				RoleName,
				SubRole,
				UserID,
				SubRoleID,
				ItemStatus,
				Step,
				AssignedDate,
				NotifyDate,
				RoleAssignedDate,
				ApprovalType,
				DocumentTypeID
			) (
				SELECT
					DISTINCT *,
					@DocumentTypeID AS DocumentTypeID
				FROM
					#TempApprovals)			
			END -- THE CALLING PROCEDURE WILL CHECK THE @ApproversCount VARIABLE TO DETERMINE IF THE DCUMENT SHOULD PROGRESS AUTOMATICALLY TO THE NEXT STEP
			DROP TABLE #TempApprovals
	END
GO