﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[_8DUpdateCorrectiveAction]    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].[_8DUpdateCorrectiveAction] @CANo INT,
	@CATitle VARCHAR(300),
	@RequestorID INT,
	@IssueDate DATETIME,
	@CAType VARCHAR(10),
	@D1AssigneeID INT,
	@CASourceID INT,
	@ModuleID INT,
	@QAID INT,
	@D8DueDate DATETIME,
	@Tool VARCHAR(50),
	@ApprovedDate DATETIME,
	@RelatedMRB VARCHAR(50),
	@RelatedAudit VARCHAR(50),
	@D0Comments VARCHAR(2000),
	@D2ProblemDescription VARCHAR(2000),
	@D3RiskAssessmentNotes VARCHAR(2000),
	@D0Completed BIT,
	@D0Approved BIT,
	@D3Completed BIT,
	@D4RootCause1 VARCHAR(1000),
	@D4RootCause2 VARCHAR(1000),
	@D4RootCause3 VARCHAR(1000),
	@D4RootCause4 VARCHAR(1000),
	@D4Completed BIT,
	@D4Approved BIT,
	@D5Completed BIT,
	@D6Validated BIT,
	@D7Completed BIT,
	@D8Completed BIT,
	@D8TeamRecognition VARCHAR(500),
	@D8LessonsLearned VARCHAR(1000),
	@TeamCaptainID INT,
	@CASponsorID INT,
	@CustomerName VARCHAR(100),
	@CustomerPartNo VARCHAR(50),
	@IFXPartNo VARCHAR(50),
	@PartQty INT,
	@InvoiceNo VARCHAR(50),
	@PurchaseOrderNo VARCHAR(50),
	@SalesOrderNo VARCHAR(50),
	@DollarImpact FLOAT,
	@BackgroundInfo VARCHAR(1000),
	@Analysis VARCHAR(1000),
	@VisualVerification VARCHAR(1000),
	@InterimContainmentAction VARCHAR(1000),
	@ICAVerificationResults VARCHAR(1000),
	@ICAValidationActivities VARCHAR(1000),
	@RootCauseVerification VARCHAR(1000),
	@EscapePoint VARCHAR(1000),
	@FollowUpDate DATETIME,
	@D8Approved BIT,
	@D5Approved BIT,
	@D3Approved BIT,
	@CASubmitted BIT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	DECLARE @Status INT
SET
	@Status = CASE
		WHEN @D8Completed = 1 THEN '8'
		WHEN @D7Completed = 1 THEN '7'
		WHEN @D6Validated = 1 THEN '6'
		WHEN @D5Completed = 1 THEN '5'
		WHEN @D4Completed = 1 THEN '4'
		WHEN @D3Completed = 1 THEN '3'
		WHEN @D0Completed = 1 THEN '10'
		ELSE (
			SELECT
				Status
			FROM
				_8DCorrectiveAction
			WHERE
				CANo = @CANo
		)
	END IF @D1AssigneeID <> 0
	AND (@Status = 0) BEGIN
SET
	@Status = 9 -- D1
END
SET
	NOCOUNT ON;

UPDATE
	_8DCorrectiveAction
SET
	CATitle = @CATitle,
	RequestorID = @RequestorID,
	IssueDate = @IssueDate,
	Status = @Status,
	CAType = @CAType,
	D1AssigneeID = @D1AssigneeID,
	CASourceID = @CASourceID,
	ModuleID = @ModuleID,
	QAID = @QAID,
	D8DueDate = @D8DueDate,
	Tool = @Tool,
	ApprovedDate = @ApprovedDate,
	RelatedMRB = @RelatedMRB,
	RelatedAudit = @RelatedAudit,
	D0Comments = @D0Comments,
	TeamCaptainID = @TeamCaptainID,
	CASponsorID = @CASponsorID,
	CustomerName = @CustomerName,
	CustomerPartNo = @CustomerPartNo,
	IFXPartNo = @IFXPartNo,
	PartQty = @PartQty,
	InvoiceNo = @InvoiceNo,
	PurchaseOrderNo = @PurchaseOrderNo,
	SalesOrderNo = @SalesOrderNo,
	DollarImpact = @DollarImpact,
	BackgroundInfo = @BackgroundInfo,
	Analysis = @Analysis,
	VisualVerification = @VisualVerification,
	InterimContainmentAction = @InterimContainmentAction,
	ICAVerificationResults = @ICAVerificationResults,
	ICAValidationActivities = @ICAValidationActivities,
	RootCauseVerification = @RootCauseVerification,
	EscapePoint = @EscapePoint,
	D2ProblemDescription = @D2ProblemDescription,
	D3RiskAssessmentNotes = @D3RiskAssessmentNotes,
	D3CompleteDate = CASE
		WHEN @D3Completed = 1 THEN GETDATE()
		ELSE NULL
	END,
	D4RootCause1 = @D4RootCause1,
	D4RootCause2 = @D4RootCause2,
	D4RootCause3 = @D4RootCause3,
	D4RootCause4 = @D4RootCause4,
	D0CompleteDate = CASE
		WHEN @D0Completed = 1 THEN GETDATE()
		ELSE NULL
	END,
	D0ApprovedDate = CASE
		WHEN @D0Approved = 1 THEN GETDATE()
		ELSE NULL
	END,
	D4CompleteDate = CASE
		WHEN @D4Completed = 1 THEN GETDATE()
		ELSE NULL
	END,
	D5CompleteDate = CASE
		WHEN @D5Completed = 1 THEN GETDATE()
		ELSE NULL
	END,
	D6ValidatedDate = CASE
		WHEN @D6Validated = 1 THEN GETDATE()
		ELSE NULL
	END,
	D7CompleteDate = CASE
		WHEN @D7Completed = 1 THEN GETDATE()
		ELSE NULL
	END,
	D8CompleteDate = CASE
		WHEN @D8Completed = 1 THEN GETDATE()
		ELSE NULL
	END,
	D3ApprovedDate = CASE
		WHEN @D3Approved = 1 THEN GETDATE()
		ELSE NULL
	END,
	D4ApprovedDate = CASE
		WHEN @D4Approved = 1 THEN GETDATE()
		ELSE NULL
	END,
	D5ApprovedDate = CASE
		WHEN @D5Approved = 1 THEN GETDATE()
		ELSE NULL
	END,
	D8ApprovedDate = CASE
		WHEN @D8Approved = 1 THEN GETDATE()
		ELSE NULL
	END,
	D8TeamRecognition = @D8TeamRecognition,
	D8LessonsLearned = @D8LessonsLearned,
	FollowUpDate = @FollowUpDate,
	CASubmitted = @CASubmitted
WHERE
	CANo = @CANo
END
GO