﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[InsertdAdditionalApproval]    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].[InsertdAdditionalApproval] @IssueID INT,
	@UserID INT,
	@Step INT,
	@DocumentTypeID INT,
	@Email VARCHAR(100) OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @AddtionalRoleID INT DECLARE @AddtionalRoleName VARCHAR(50) DECLARE @AdditionalSubRoleID INT DECLARE @AdditionalSubRoleName VARCHAR(50) DECLARE @ApprovalType TINYINT -- Get the approval type of the current step
SET
	@ApprovalType = (
		SELECT
			TOP 1 ApprovalType
		FROM
			Approval
		WHERE
			IssueID = @IssueID
			AND Step = @Step
			AND DocumentTypeID = @DocumentTypeID
	)
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 = 'Additional Approver'
INSERT INTO
	Approval (
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType,
		DocumentTypeID
	)
VALUES
	(
		@IssueID,
		@AddtionalRoleName,
		@AdditionalSubRoleName,
		@UserID,
		@AdditionalSubRoleID,
		0,
		@Step,
		GETDATE(),
		GETDATE(),
		GETDATE(),
		@ApprovalType,
		@DocumentTypeID
	)
SELECT
	@Email = Email
FROM
	Users
WHERE
	UserID = @UserID
END
GO