﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[UpdateApproval]    Script Date: 12/11/2024 12:42:33 PM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	ALTER PROCEDURE [dbo].[UpdateApproval] @IssueID INT,
	@CurrentStep INT,
	@ItemStatus INT,
	@UserID INT,
	@Comments VARCHAR(1000),
	@SubRoleCategoriesClause VARCHAR(500),
	@DocumentTypeID INT,
	@LastStep BIT OUTPUT,
	@LastApproverInCurrentStep BIT OUTPUT,
	@WorkFlowNumber INT = 1 AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @ApprovalType INT DECLARE @MaxStep INT DECLARE @RemainingApprovers INT DECLARE @NewStep INT DECLARE @ApproverCountForThisStep INT DECLARE @SubRoleID INT DECLARE @ConvertedFromNumber INT DECLARE @ErrorMessage NVARCHAR(4000);

DECLARE @ErrorSeverity INT;

DECLARE @ErrorState INT;

DECLARE @RHCount INT
SET
	@RHCount = 0 IF @DocumentTypeID = 1 BEGIN
SET
	@RHCount = (
		SELECT
			COUNT(*)
		FROM
			dbo.fnGetLot_RH(@IssueID)
	)
END
ELSE IF @DocumentTypeID = 2 BEGIN
SET
	@RHCount = (
		SELECT
			COUNT(*)
		FROM
			MRB M WITH(NOLOCK)
		WHERE
			M.MRBNumber = @IssueID
	)
END
SET
	@ApprovalType = (
		SELECT
			DISTINCT TOP 1 ApprovalType
		FROM
			Approval
		WHERE
			IssueID = @IssueID
			AND UserID = @UserID
			AND Step = @CurrentStep
			AND DocumentTypeID = @DocumentTypeID
			AND ItemStatus = 0
	)
SET
	@LastStep = 0
SET
	@NewStep = @CurrentStep + 1 -- GET THE LAST STEP IN THE WORKFLOW
SELECT
	@MaxStep = (
		SELECT
			MAX(WS.WorkflowStepNumber)
		FROM
			DocumentType D
			INNER JOIN Workflows W ON D.DocumentTypeID = W.DocumentTypeID
			AND WorkFlowNumber = @WorkFlowNumber
			INNER JOIN WorkflowSteps WS ON WS.WorkflowID = W.WorkflowID
			AND D.DocumentTypeID = @DocumentTypeID
	) -- Get the SubRole ID of the Person Approving/Denying it							
SET
	@SubRoleID = (
		SELECT
			TOP 1 SubRoleID
		FROM
			Approval
		WHERE
			IssueID = @IssueID
			AND UserID = @UserID
			AND Step = @CurrentStep
			AND DocumentTypeID = @DocumentTypeID
	) DECLARE @OperationTypeString VARCHAR(50) IF EXISTS (
		SELECT
			*
		FROM
			ECN
		WHERE
			CancellationInProgress = 1
			AND ECNNumber = @IssueID
	)
SET
	@OperationTypeString = 'Cancellation Approved'
	ELSE IF EXISTS (
		SELECT
			*
		FROM
			ECN
		WHERE
			ExpirationInProgress = 1
			AND ECNNumber = @IssueID
	)
SET
	@OperationTypeString = 'Expiration Approved'
	ELSE
SET
	@OperationTypeString = 'Approved' BEGIN TRY BEGIN TRAN IF @ItemStatus = 1 -- Approve
	BEGIN
UPDATE
	Approval
SET
	ItemStatus = 1,
	CompletedDate = GETDATE(),
	Comments = @Comments
WHERE
	IssueID = @IssueID
	AND UserID = @UserID
	AND Step = @CurrentStep
	AND DocumentTypeID = @DocumentTypeID
	AND CompletedDate IS NULL IF @ @ROWCOUNT > 0 BEGIN -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		@OperationTypeString,
		'Line 113 - Approved at step ' + CONVERT(NCHAR(10), @CurrentStep),
		@DocumentTypeID,
		@Comments
	)
END IF @ApprovalType = 2 -- ONLY ONE APPROVER PER SUBROLE IS REQUIRED FOR THIS STEP
BEGIN ---- Get the SubRole ID of the Person Approving it
-- Because Only approver is required to approve per SubRole
-- Selete rest the approvers for the SubRoles bellonging to the this approver
DELETE FROM
	Approval
WHERE
	IssueID = @IssueID
	AND UserID <> @UserID
	AND Step = @CurrentStep
	AND DocumentTypeID = @DocumentTypeID
	AND SubRoleID = @SubRoleID -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Delete',
		'Line 133 - Delete approvers for step ' + CONVERT(NCHAR(10), @CurrentStep) + ' for SubRoleID ' + CONVERT(NCHAR(10), @SubRoleID),
		@DocumentTypeID,
		@Comments
	) --  Check if there are any pending approvals
SET
	@RemainingApprovers = (
		SELECT
			COUNT(*)
		FROM
			Approval
		WHERE
			IssueID = @IssueID
			AND Step = @CurrentStep
			AND ItemStatus = 0
			AND DocumentTypeID = @DocumentTypeID
	) -- if there are no more approvers in this step then proceed to next step
	IF @RemainingApprovers = 0 BEGIN IF @MaxStep > @CurrentStep -- there are some steps remaining
	BEGIN
SET
	@LastStep = 0 IF @DocumentTypeID = 3
	OR @DocumentTypeID = 4
	OR @DocumentTypeID = 5 -- ECN
	BEGIN
UPDATE
	ECN
SET
	CurrentStep = @NewStep
WHERE
	ECNNumber = @IssueID
END
ELSE IF @DocumentTypeID = 9 -- Corrective Action
BEGIN
UPDATE
	_8DCorrectiveAction
SET
	CurrentStep = @NewStep
WHERE
	CANo = @IssueID
END -- proceed to the next
EXEC InsertApprovers @IssueID,
@NewStep,
@DocumentTypeID,
@SubRoleCategoriesClause,
@RHCount,
@ApproverCountForThisStep -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Insert',
		'Line 195 - Insert approvers for step ' + CONVERT(NCHAR(10), @NewStep),
		@DocumentTypeID,
		@Comments
	) IF @ApproverCountForThisStep = 0 BEGIN
SET
	@LastApproverInCurrentStep = 1 RETURN;

END
END
ELSE BEGIN -- It was the last step, close the document
SET
	@LastStep = 1 IF @DocumentTypeID = 3
	OR @DocumentTypeID = 4
	OR @DocumentTypeID = 5 BEGIN IF EXISTS (
		SELECT
			*
		FROM
			ECN
		WHERE
			CancellationInProgress = 1
			AND ECNNumber = @IssueID
	) BEGIN
UPDATE
	ECN
SET
	CancellationInProgress = 0,
	CancellationApproved = 1,
	CancellationApprovalDate = GETDATE()
WHERE
	ECNNumber = @IssueID
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Cancelled',
		'Line - 232 Document closed',
		@DocumentTypeID,
		@Comments
	)
END
ELSE IF EXISTS (
	SELECT
		*
	FROM
		ECN
	WHERE
		ExpirationInProgress = 1
		AND ECNNumber = @IssueID
) BEGIN
UPDATE
	ECN
SET
	ExpirationInProgress = 0,
	ExpirationProcessed = 1,
	ExpirationProcessedlDate = GETDATE()
WHERE
	ECNNumber = @IssueID
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Expired',
		'Line 234 - Document closed',
		@DocumentTypeID,
		@Comments
	)
END
ELSE IF EXISTS (
	SELECT
		*
	FROM
		ECN
	WHERE
		TECNExtensionState = 1
		AND ECNNumber = @IssueID
) BEGIN DECLARE @TECNExtensionDate DATETIME = (
	SELECT
		ExtensionDate
	FROM
		ECN
	WHERE
		ECNNumber = @IssueID
)
UPDATE
	ECN
SET
	TECNExtensionState = 0,
	ExpirationDate = @TECNExtensionDate,
	ExtensionDate = null,
	CloseDate = GETDATE()
WHERE
	ECNNumber = @IssueID
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Extended',
		'Line 256 - Document closed',
		@DocumentTypeID,
		@Comments
	)
END
ELSE BEGIN
UPDATE
	ECN
SET
	CloseDate = GETDATE(),
	TECNExtensionState = 0,
	ConversionApprovalInProgress = 0
WHERE
	ECNNumber = @IssueID
SET
	@ConvertedFromNumber = (
		SELECT
			ConvertedFromNumber
		FROM
			ECN
		WHERE
			ECNNumber = @IssueID
	) -- Set the flag for the TECN, which this ECN was derived from
	IF (@ConvertedFromNumber IS NOT NULL) BEGIN
UPDATE
	ECN
SET
	ConversionApprovalInProgress = 0
WHERE
	ECNNumber = @ConvertedFromNumber
END
END
END
ELSE IF @DocumentTypeID = 9 BEGIN IF @CurrentStep = 3 BEGIN
UPDATE
	_8DCorrectiveAction
SET
	ClosedDate = GETDATE(),
	Status = 1,
	ApprovalStatus = 2,
	ApprovedDate = GETDATE()
WHERE
	CANo = @IssueID
END
ELSE IF @CurrentStep = 1 BEGIN
UPDATE
	_8DCorrectiveAction
SET
	Status = 11,
	ApprovalStatus = 2,
	ApprovedDate = GETDATE()
WHERE
	CANo = @IssueID
END
END -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Last Step',
		' line 314 - Document closed',
		@DocumentTypeID,
		@Comments
	)
END
SET
	@LastApproverInCurrentStep = 1
END
ELSE BEGIN
SET
	@LastApproverInCurrentStep = 0
END
END
ELSE -- ALL THE APPROVERS ARE REQUIRED TO APPROVE FOR THIS STEP
BEGIN --  Check if there are any pending approvals
SET
	@RemainingApprovers = (
		SELECT
			COUNT(*)
		FROM
			Approval
		WHERE
			IssueID = @IssueID
			AND Step = @CurrentStep
			AND ItemStatus = 0
			AND DocumentTypeID = @DocumentTypeID
	) -- if there are no more approvers in this step then proceed to next step
	IF @RemainingApprovers = 0 BEGIN IF @MaxStep > @CurrentStep -- there are some steps remaining
	BEGIN
SET
	@LastStep = 0 IF @DocumentTypeID = 3
	OR @DocumentTypeID = 4
	OR @DocumentTypeID = 5 BEGIN
UPDATE
	ECN
SET
	CurrentStep = @NewStep
WHERE
	ECNNumber = @IssueID
END
ELSE IF @DocumentTypeID = 9 BEGIN
UPDATE
	_8DCorrectiveAction
SET
	CurrentStep = @NewStep
WHERE
	CANo = @IssueID
END -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Update',
		'Update Document with the new step ' + CONVERT(NCHAR(10), @NewStep),
		@DocumentTypeID,
		@Comments
	) -- proceed to the next step
SET
	@LastApproverInCurrentStep = 1 EXEC InsertApprovers @IssueID,
	@NewStep,
	@DocumentTypeID,
	@SubRoleCategoriesClause,
	@RHCount,
	@ApproverCountForThisStep -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Insert',
		'Line 391 - Insert approvers for step ' + CONVERT(NCHAR(10), @NewStep),
		@DocumentTypeID,
		@Comments
	) IF @ApproverCountForThisStep = 0 BEGIN
SET
	@LastApproverInCurrentStep = 1 RETURN;

END
END
ELSE BEGIN -- It was the last step, close the document
SET
	@LastStep = 1
SET
	@LastApproverInCurrentStep = 1 IF @DocumentTypeID = 3
	OR @DocumentTypeID = 4
	OR @DocumentTypeID = 5 BEGIN IF EXISTS (
		SELECT
			*
		FROM
			ECN
		WHERE
			CancellationInProgress = 1
			AND ECNNumber = @IssueID
	) BEGIN
UPDATE
	ECN
SET
	CancellationInProgress = 0,
	CancellationApproved = 1,
	CancellationApprovalDate = GETDATE()
WHERE
	ECNNumber = @IssueID
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Cancelled',
		'Line 427 - Document closed',
		@DocumentTypeID,
		@Comments
	)
END
ELSE IF EXISTS (
	SELECT
		*
	FROM
		ECN
	WHERE
		ExpirationInProgress = 1
		AND ECNNumber = @IssueID
) BEGIN
UPDATE
	ECN
SET
	ExpirationInProgress = 0,
	ExpirationProcessed = 1,
	ExpirationProcessedlDate = GETDATE()
WHERE
	ECNNumber = @IssueID
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Expired',
		'Line 438 - Document closed',
		@DocumentTypeID,
		@Comments
	)
END
ELSE BEGIN
UPDATE
	ECN
SET
	CloseDate = GETDATE(),
	TECNExtensionState = 0,
	ConversionApprovalInProgress = 0
WHERE
	ECNNumber = @IssueID -- Set the flag for the TECN, which this ECN was derived from
SET
	@ConvertedFromNumber = (
		SELECT
			ConvertedFromNumber
		FROM
			ECN
		WHERE
			ECNNumber = @IssueID
	) -- Set the flag for the TECN, which this ECN was derived from
	IF (@ConvertedFromNumber IS NOT NULL) BEGIN
UPDATE
	ECN
SET
	ConversionApprovalInProgress = 0
WHERE
	ECNNumber = @ConvertedFromNumber
END
END
END
ELSE IF @DocumentTypeID = 9 BEGIN
UPDATE
	_8DCorrectiveAction
SET
	ClosedDate = GETDATE(),
	Status = 1,
	ApprovalStatus = 2,
	ApprovedDate = GETDATE()
WHERE
	CANo = @IssueID
END -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Last Step',
		'Document closed linw 485',
		@DocumentTypeID,
		@Comments
	)
END
END
ELSE BEGIN
SET
	@LastApproverInCurrentStep = 0
END
END
END
ELSE IF @ItemStatus = 2 -- denied
BEGIN
SET
	@LastStep = 0
SET
	@LastApproverInCurrentStep = 0 -- begin from step 1
UPDATE
	Approval
SET
	ItemStatus = 2,
	CompletedDate = NULL,
	Comments = @Comments
WHERE
	IssueID = @IssueID
	AND UserID = @UserID
	AND Step = @CurrentStep
	AND DocumentTypeID = @DocumentTypeID -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Denied',
		'Line 514 Denied at step ' + CONVERT(NCHAR(10), @CurrentStep),
		@DocumentTypeID,
		@Comments
	) -- get the approvalid of the rejected record, 
	-- as this is the only record which needs to be retained for this issue
	-- this retained record will be deleted when the issue is re-submitted
	DECLARE @ApprovalID INT
SELECT
	@ApprovalID = (
		SELECT
			MAX(ApprovalID)
		FROM
			Approval
		WHERE
			IssueID = @IssueID
			AND UserID = @UserID
			AND Step = @CurrentStep
			AND DocumentTypeID = @DocumentTypeID
	) IF @ApprovalID IS NOT NULL -- do not process, if an invalid approver has processed it
	BEGIN -- delete all the other approvers record for the issue for the current step
DELETE FROM
	Approval
WHERE
	(
		IssueID = @IssueID
		AND ApprovalID <> @ApprovalID
		AND DocumentTypeID = @DocumentTypeID
	) -- If the document is denied and the originator of the document belongs to "Probe" Subrole at the "Execution" step
	-- make sure the that the document appears in the task list of the other users under the "Probe" Subrole
	-- so that the other users under the "Probe" subrole know that the document needs to be re-submitted
	-- this feature is needed becaue the users under the "Probe" subrole work on shifts.
	DECLARE @OriginatorUserID INT -- get the originator ID	
	IF @DocumentTypeID = 1 BEGIN
SELECT
	@OriginatorUserID = USR.UserID
FROM
	LotDisposition LD
	INNER JOIN UserSubRole USR ON LD.OriginatorID = USR.UserID
	INNER JOIN SubRole SR ON USR.SubRoleID = SR.SubRoleID
	INNER JOIN SubRoleCategory SRC ON SR.SubRoleCategoryID = SRC.SubRoleCategoryID
WHERE
	(
		SRC.SubRoleCategoryItem = 'Probe'
		OR SRC.SubRoleCategoryItem = 'Fab'
	)
	AND LD.IssueID = @IssueID
END IF NOT @OriginatorUserID IS NULL BEGIN -- Add the remaiming users from the "Probe" into the "Approval", 
-- so that it appears in the Task List of the Users in the "Probe" Subrole
INSERT INTO
	Approval (
		A.IssueID,
		A.RoleName,
		A.SubRole,
		B.UserID,
		A.SubRoleID,
		A.ItemStatus,
		A.Step,
		A.NotifyDate,
		A.AssignedDate,
		A.RoleAssignedDate,
		A.CompletedDate,
		Comments,
		ApprovalType,
		A.DocumentTypeID,
		DisplayDeniedDocument
	)
SELECT
	A.IssueID,
	B.RoleName,
	B.SubRoleCategoryItem,
	B.UserID,
	A.SubRoleID,
	A.ItemStatus,
	A.Step,
	A.NotifyDate,
	A.AssignedDate,
	A.RoleAssignedDate,
	A.CompletedDate,
	'Denied' AS 'Comments',
	ApprovalType,
	A.DocumentTypeID,
	1
FROM
	(
		SELECT
			*
		FROM
			Approval
		WHERE
			ApprovalID = @ApprovalID
	) AS A
	INNER JOIN (
		SELECT
			@ApprovalID AS ApprovalID,
			USR.UserID,
			SRC.SubRoleCategoryItem,
			R.RoleName
		FROM
			UserSubRole USR
			INNER JOIN SubRole SR ON USR.SubRoleID = SR.SubRoleID
			INNER JOIN SubRoleCategory SRC ON SR.SubRoleCategoryID = SRC.SubRoleCategoryID
			INNER JOIN Role R ON SR.RoleID = R.RoleID
		WHERE
			SRC.SubRoleCategoryItem = 'Probe'
			AND USR.UserID <> @OriginatorUserID
	) AS B ON A.ApprovalID = B.ApprovalID
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Delete',
		'Delete all the other approvals add other users from the Probe "SubRole" the denied one - line 580',
		@DocumentTypeID,
		@Comments
	)
END
ELSE BEGIN -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Delete',
		'Delete all the other approvals except the denied one - line 592',
		@DocumentTypeID,
		@Comments
	)
END -- Update LotDisposition Item to begining of the workflow loop
IF @DocumentTypeID = 3
OR @DocumentTypeID = 4
OR @DocumentTypeID = 5 BEGIN
UPDATE
	ECN
SET
	CurrentStep = 0,
	SubmitedDate = NULL,
	ConversionApprovalInProgress = 0
WHERE
	ECNNumber = @IssueID -- Set the flag for the TECN, which this ECN was derived from
SET
	@ConvertedFromNumber = (
		SELECT
			ConvertedFromNumber
		FROM
			ECN
		WHERE
			ECNNumber = @IssueID
	) -- Set the flag for the TECN, which this ECN was derived from
	IF NOT @ConvertedFromNumber IS NULL BEGIN
UPDATE
	ECN
SET
	ConversionApprovalInProgress = 0
WHERE
	ECNNumber = @ConvertedFromNumber
END
END
ELSE IF @DocumentTypeID = 9 BEGIN DECLARE @CAType VARCHAR(10)
SET
	@CAType = (
		SELECT
			CAType
		FROM
			_8DCorrectiveAction
		WHERE
			CANo = @IssueID
	) IF @CAType = 'D8' BEGIN
UPDATE
	_8DCorrectiveAction
SET
	CurrentStep = 0,
	D8CompleteDate = NULL,
	ApprovalStatus = 0,
	D8ApprovedDate = NULL,
	D6ValidatedDate = NULL,
	D5ApprovedDate = NULL,
	D5CompleteDate = NULL
WHERE
	CANo = @IssueID
END IF @CAType = 'D6' BEGIN
UPDATE
	_8DCorrectiveAction
SET
	CurrentStep = 0,
	D6ValidatedDate = NULL,
	ApprovalStatus = 0
WHERE
	CANo = @IssueID
END IF @CAType = 'D3' BEGIN
UPDATE
	_8DCorrectiveAction
SET
	CurrentStep = 0,
	D3CompleteDate = NULL,
	ApprovalStatus = 0,
	D3ApprovedDate = NULL
WHERE
	CANo = @IssueID
END IF @CAType = 'D0' BEGIN
UPDATE
	_8DCorrectiveAction
SET
	CurrentStep = 0,
	D0CompleteDate = NULL,
	ApprovalStatus = 0,
	D0ApprovedDate = NULL
WHERE
	CANo = @IssueID
END
END -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Update',
		'Update the document to step 0 - line 667',
		@DocumentTypeID,
		@Comments
	)
END
end
ELSE IF @ItemStatus = 8 -- recalled
begin
SET
	@LastStep = 0
SET
	@LastApproverInCurrentStep = 0 -- begin from step 1
UPDATE
	Approval
SET
	ItemStatus = 8,
	CompletedDate = NULL,
	Comments = @Comments,
	UserID = @UserID
WHERE
	IssueID = @IssueID
	AND Step = @CurrentStep
	AND DocumentTypeID = @DocumentTypeID -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Recalled',
		'Line 691 - Recalled at step ' + CONVERT(NCHAR(10), @CurrentStep),
		@DocumentTypeID,
		@Comments
	) -- get the approvalid of the rejected record, 
	-- as this is the only record which needs to be retained for this issue
	-- this retained record will be deleted when the issue is re-submitted
	DECLARE @ApprovalIDRecall INT
SELECT
	@ApprovalIDRecall = (
		SELECT
			MAX(ApprovalID)
		FROM
			Approval
		WHERE
			IssueID = @IssueID
			AND Step = @CurrentStep
			AND DocumentTypeID = @DocumentTypeID
	) IF @ApprovalIDRecall IS NOT NULL -- do not process, if an invalid approver has processed it
	BEGIN -- delete all the other approvers record for the issue for the current step
DELETE FROM
	Approval
WHERE
	(
		IssueID = @IssueID
		AND ApprovalID <> @ApprovalIDRecall
		AND DocumentTypeID = @DocumentTypeID
	) -- If the document is denied and the originator of the document belongs to "Probe" Subrole at the "Execution" step
	-- make sure the that the document appears in the task list of the other users under the "Probe" Subrole
	-- so that the other users under the "Probe" subrole know that the document needs to be re-submitted
	-- this feature is needed becaue the users under the "Probe" subrole work on shifts.
	DECLARE @OriginatorUserIDRecall INT -- get the originator ID	
	IF @DocumentTypeID = 1 BEGIN
SELECT
	@OriginatorUserIDRecall = USR.UserID
FROM
	LotDisposition LD
	INNER JOIN UserSubRole USR ON LD.OriginatorID = USR.UserID
	INNER JOIN SubRole SR ON USR.SubRoleID = SR.SubRoleID
	INNER JOIN SubRoleCategory SRC ON SR.SubRoleCategoryID = SRC.SubRoleCategoryID
WHERE
	(
		SRC.SubRoleCategoryItem = 'Probe'
		OR SRC.SubRoleCategoryItem = 'Fab'
	)
	AND LD.IssueID = @IssueID
END IF NOT @OriginatorUserIDRecall IS NULL BEGIN -- Add the remaiming users from the "Probe" into the "Approval", 
-- so that it appears in the Task List of the Users in the "Probe" Subrole
INSERT INTO
	Approval (
		A.IssueID,
		A.RoleName,
		A.SubRole,
		B.UserID,
		A.SubRoleID,
		A.ItemStatus,
		A.Step,
		A.NotifyDate,
		A.AssignedDate,
		A.RoleAssignedDate,
		A.CompletedDate,
		Comments,
		ApprovalType,
		A.DocumentTypeID,
		DisplayDeniedDocument
	)
SELECT
	A.IssueID,
	B.RoleName,
	B.SubRoleCategoryItem,
	B.UserID,
	A.SubRoleID,
	A.ItemStatus,
	A.Step,
	A.NotifyDate,
	A.AssignedDate,
	A.RoleAssignedDate,
	A.CompletedDate,
	'Denied' AS 'Comments',
	ApprovalType,
	A.DocumentTypeID,
	1
FROM
	(
		SELECT
			*
		FROM
			Approval
		WHERE
			ApprovalID = @ApprovalIDRecall
	) AS A
	INNER JOIN (
		SELECT
			@ApprovalIDRecall AS ApprovalID,
			USR.UserID,
			SRC.SubRoleCategoryItem,
			R.RoleName
		FROM
			UserSubRole USR
			INNER JOIN SubRole SR ON USR.SubRoleID = SR.SubRoleID
			INNER JOIN SubRoleCategory SRC ON SR.SubRoleCategoryID = SRC.SubRoleCategoryID
			INNER JOIN Role R ON SR.RoleID = R.RoleID
		WHERE
			SRC.SubRoleCategoryItem = 'Probe'
			AND USR.UserID <> @OriginatorUserIDRecall
	) AS B ON A.ApprovalID = B.ApprovalID
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Delete',
		'Delete all the other approvals add other users from the Probe "SubRole" the denied one - line 757',
		@DocumentTypeID,
		@Comments
	)
END
ELSE BEGIN -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Delete',
		'Delete all the other approvals except the denied one - line 769',
		@DocumentTypeID,
		@Comments
	)
END IF @DocumentTypeID = 3
OR @DocumentTypeID = 4
OR @DocumentTypeID = 5 BEGIN
UPDATE
	ECN
SET
	CurrentStep = 0,
	SubmitedDate = NULL,
	ConversionApprovalInProgress = 0
WHERE
	ECNNumber = @IssueID -- Set the flag for the TECN, which this ECN was derived from
SET
	@ConvertedFromNumber = (
		SELECT
			ConvertedFromNumber
		FROM
			ECN
		WHERE
			ECNNumber = @IssueID
	) -- Set the flag for the TECN, which this ECN was derived from
	IF NOT @ConvertedFromNumber IS NULL BEGIN
UPDATE
	ECN
SET
	ConversionApprovalInProgress = 0
WHERE
	ECNNumber = @ConvertedFromNumber
END
END -- UPDATE THE APPROVAL LOG
INSERT INTO
	ApprovalLog (
		IssueID,
		UserID,
		SubRoleID,
		OperationType,
		OperationLog,
		DocumentTypeID,
		Comments
	)
VALUES
	(
		@IssueID,
		@UserID,
		@SubRoleID,
		'Update',
		'Update the document to step 0 line 793',
		@DocumentTypeID,
		@Comments
	)
END
END COMMIT
END TRY BEGIN CATCH ROLLBACK
INSERT INTO
	EventLog (
		UserID,
		DocumentType,
		IssueID,
		OperationType,
		InsertTimeStamp,
		Comments,
		SysDocumentID
	)
VALUES
	(
		@UserID,
		'Doc',
		@IssueID,
		'Error',
		GETDATE(),
		'Error while Approving the Doc ' + CONVERT(VARCHAR(10), @IssueID) + ' Doc# ' + CONVERT(VARCHAR(10), @IssueID),
		@IssueID
	)
SELECT
	@ErrorMessage = ERROR_MESSAGE() + CONVERT(VARCHAR(10), @IssueID) + ' Doc# ' + CONVERT(VARCHAR(10), @IssueID),
	@ErrorSeverity = ERROR_SEVERITY(),
	@ErrorState = ERROR_STATE();

RAISERROR (
	@ErrorMessage,
	-- Message text.
	@ErrorSeverity,
	-- Severity.
	@ErrorState -- State.
);

END CATCH
END