﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[ECN_TECNExtensionSubmitForApproval_05012017]    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].[ECN_TECNExtensionSubmitForApproval_05012017] @ECNNumber INT,
	@UserID INT,
	@DocumentTypeID INT,
	@TECNExtensionDate DATETIME,
	@SubRoleCategoriesClause VARCHAR(1000),
	@AppoverCount INT OUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

SET
	@AppoverCount = 0;

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
SET
	@CurrentDate = GETDATE() EXEC ECN_TECNExtensionCreateHistory @ECNNumber
UPDATE
	ECN
SET
	CurrentStep = 1,
	SubmitedDate = GETDATE(),
	ExtensionDate = @TECNExtensionDate,
	CloseDate = NULL,
	TECNExtensionState = 1
WHERE
	ECNNumber = @ECNNumber
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		OperationType,
		OperationLog,
		DocumentTypeID
	)
VALUES
	(
		@ECNNumber,
		@UserID,
		'Submitted For Extension',
		'Re Submitted the document due to extension',
		@DocumentTypeID
	) -- UPDATE THE APPROVAL LOG for a system initiated transaction
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		OperationType,
		OperationLog,
		DocumentTypeID
	)
VALUES
	(
		@ECNNumber,
		@UserID,
		'Delete',
		'Insert New Approvals after extension',
		@DocumentTypeID
	) -- Trigger the approval based on the current roles for ECNs and TECNS only. 
	-- For Emergency TECNs the users will pick the approvers manually
	IF @DocumentTypeID = 3 BEGIN DECLARE @RHCount INT DECLARE @CheckForITARCompliant INT
SET
	@RHCount = (
		SELECT
			COUNT(*)
		FROM
			ECN E
		WHERE
			(E.IsRH = 1)
			AND E.ECNNumber = @ECNNumber
	) IF @RHCount > 0
SET
	@CheckForITARCompliant = 1
	ELSE
SET
	@CheckForITARCompliant = 0
SET
	@CheckForITARCompliant = 0
INSERT INTO
	#TempApprovals
	(
		IssueID,
		RoleName,
		SubRole,
		UserID,
		SubRoleID,
		ItemStatus,
		Step,
		AssignedDate,
		NotifyDate,
		RoleAssignedDate,
		ApprovalType
	) EXEC GetRoles @WorkflowStepNumber = 1,
	@WhereClause = @SubRoleCategoriesClause,
	@CheckForITARCompliant = @CheckForITARCompliant,
	@DocumentTypeID = @DocumentTypeID,
	@IssueID = @ECNNumber
SET
	@AppoverCount = (
		SELECT
			COUNT(*)
		FROM
			#TempApprovals)
			IF @AppoverCount > 0 BEGIN -- INSEERT INTO THE ACTUAL TABLE
		INSERT INTO
			Approval (
				IssueID,
				RoleName,
				SubRole,
				UserID,
				SubRoleID,
				ItemStatus,
				Step,
				AssignedDate,
				NotifyDate,
				RoleAssignedDate,
				ApprovalType,
				DocumentTypeID
			) (
				SELECT
					DISTINCT *,
					@DocumentTypeID AS DocumentTypeID
				FROM
					#TempApprovals)
			END DROP TABLE #TempApprovals
	END
END
GO