﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[ECNReSubmitForApproval]    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].[ECNReSubmitForApproval] @ECNNumber INT,
	@UserID INT,
	@DocumentTypeID INT,
	@SubRoleCategoriesClause VARCHAR(1000),
	@AppoverCount INT OUT,
	@DescriptionOfChange VARCHAR(1000),
	@ReasonForChange VARCHAR(1000),
	@NewECNTypeString VARCHAR(50),
	@NewECNNumber INT OUTPUT,
	@CategoryID INT = NULL 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() DECLARE @OutNewECNNumber INT EXEC ECNCopyECN @ECNNumber,
	@NewECNTypeString,
	@TempNewECNNumber = @OutNewECNNumber OUTPUT
SET
	@NewECNNumber = @OutNewECNNumber
UPDATE
	ECN
SET
	Converted = 1,
	ConvertedToType = @NewECNTypeString,
	ConvertedToNumber = @NewECNNumber
WHERE
	ECNNumber = @ECNNumber -- 11/07/2018 - allow user to change category ID when changing between ECN/TECN	
UPDATE
	ECN
SET
	CategoryID = @CategoryID
WHERE
	ECNNumber = @NewECNNumber
	AND @CategoryID IS NOT NULL
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		OperationType,
		OperationLog,
		DocumentTypeID
	)
VALUES
	(
		@NewECNNumber,
		@UserID,
		'Changed',
		'Changed the document type',
		@DocumentTypeID
	) -- Trigger the approval based on the current roles for ECNs and TECNS only. 
	-- For Emergency TECNs the users will pick the approvers manually
	--=============================================================================
	--COMMENTED OUT THE Submission Code
	--IF @DocumentTypeID = 3 
	--BEGIN
	--	INSERT INTO #TempApprovals
	--	(IssueID, RoleName, SubRole, UserID, SubRoleID, ItemStatus, Step,  AssignedDate, NotifyDate,RoleAssignedDate, ApprovalType )
	--	EXEC	GetRoles
	--			@WorkflowStepNumber = 1,
	--			@WhereClause = @SubRoleCategoriesClause,
	--			@CheckForITARCompliant = 0,
	--			@DocumentTypeID = @DocumentTypeID,
	--			@IssueID = @NewECNNumber
	--	SET @AppoverCount = (SELECT COUNT(*) FROM #TempApprovals)
	--	IF @AppoverCount > 0
	--	BEGIN
	--	 -- INSERT 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