﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[_8DGetCAItem]    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].[_8DGetCAItem] @CANo INT,
	@UserID INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

IF NOT EXISTS (
	SELECT
		*
	FROM
		_8DCorrectiveAction
	WHERE
		RecordLockIndicator = 1
		AND CANo = @CANo
) BEGIN
UPDATE
	_8DCorrectiveAction
SET
	RecordLockIndicator = 1,
	RecordLockedBy = @UserID,
	RecordLockedDate = GETDATE(),
	LastUpdateDate = GETDATE()
WHERE
	CANo = @CANo
END -- Insert statements for procedure here
SELECT
	CANo,
	dbo.fn8DConvertCANoToCADisplayFormat(CANo) AS CANoDisp,
	AuditNo,
	RequestorID,
	CATitle,
	CAType,
	CurrentStep,
	WorkFlowNumber,
	IssueDate,
	D1AssigneeID,
	D1AssigneeID AS CurrentD1AssigneeID,
	CASourceID,
	ModuleID,
	Status,
	QAID,
	Tool as Tools,
	RelatedMRB,
	(
		SELECT
			TOP 1 AuditNo
		FROM
			_8DAuditFinding
		WHERE
			CANo = @CANo
	) RelatedAudit,
	D0Comments,
	D2ProblemDescription,
	TeamCaptainID,
	CASponsorID,
	CustomerName,
	CustomerPartNo,
	IFXPartNo,
	PartQty,
	InvoiceNo,
	PurchaseOrderNo,
	SalesOrderNo,
	DollarImpact,
	BackgroundInfo,
	Analysis,
	VisualVerification,
	InterimContainmentAction,
	ICAVerificationResults,
	ICAValidationActivities,
	RootCauseVerification,
	EscapePoint,
	CASubmitted,
	U.FirstName + ' ' + U.LastName AS 'RequestorName',
	U1.FirstName + ' ' + U1.LastName AS RecordLockByName,
	CASE
		WHEN D0CompleteDate IS NOT NULL THEN 1
		ELSE 0
	END AS D0Completed,
	CASE
		WHEN D3CompleteDate IS NOT NULL THEN 1
		ELSE 0
	END AS D3Completed,
	D3CompleteDate,
	D3RiskAssessmentNotes,
	D4RootCause1,
	D4RootCause2,
	D4RootCause3,
	D4RootCause4,
	CASE
		WHEN D4CompleteDate IS NOT NULL THEN 1
		ELSE 0
	END AS D4Completed,
	D4CompleteDate,
	CASE
		WHEN D5CompleteDate IS NOT NULL THEN 1
		ELSE 0
	END AS D5Completed,
	D5CompleteDate,
	CASE
		WHEN D6ValidatedDate IS NOT NULL THEN 1
		ELSE 0
	END AS D6Validated,
	D6ValidatedDate,
	CASE
		WHEN D7CompleteDate IS NOT NULL THEN 1
		ELSE 0
	END AS D7Completed,
	D7CompleteDate,
	CASE
		WHEN D8CompleteDate IS NOT NULL THEN 1
		ELSE 0
	END AS D8Completed,
	D8CompleteDate,
	CASE
		WHEN D0ApprovedDate IS NOT NULL THEN 1
		ELSE 0
	END AS D0Approved,
	D0ApprovedDate,
	CASE
		WHEN D3ApprovedDate IS NOT NULL THEN 1
		ELSE 0
	END AS D3Approved,
	D3ApprovedDate,
	CASE
		WHEN D4ApprovedDate IS NOT NULL THEN 1
		ELSE 0
	END AS D4Approved,
	D4ApprovedDate,
	CASE
		WHEN D5ApprovedDate IS NOT NULL THEN 1
		ELSE 0
	END AS D5Approved,
	D5ApprovedDate,
	CASE
		WHEN D8ApprovedDate IS NOT NULL THEN 1
		ELSE 0
	END AS D8Approved,
	D8ApprovedDate,
	CASE
		WHEN ClosedDate IS NOT NULL THEN 'Closed'
		WHEN Status = 0 THEN 'Open'
		WHEN Status = 1 THEN 'Closed'
		WHEN Status = 2 THEN 'Cancelled'
		WHEN Status = 3 THEN 'D3'
		WHEN Status = 4 THEN 'D4'
		WHEN Status = 5 THEN 'D5'
		WHEN Status = 6 THEN 'D6'
		WHEN Status = 7 THEN 'D7'
		WHEN Status = 8 THEN 'D8'
		WHEN Status = 9 THEN 'D1'
		WHEN Status = 10 THEN 'D0'
		WHEN Status = 11 THEN 'Completed'
	END AS StatusName,
	D8DueDate,
	D8TeamRecognition,
	D8LessonsLearned,
	ApprovalStatus,
	CACompleteDate,
	CASE
		WHEN CACompleteDate IS NULL THEN 0
		ELSE 1
	END AS IsCACompleted,
	ClosedDate,
	TeamMembers,
	ApprovedDate,
	RecordLockedBy,
	RecordLockIndicator,
	RecordLockedDate,
	LastUpdateDate,
	InsertTimeStamp,
	FollowUpDate,
	D3DueDate,
	D5D7DueDate
FROM
	_8DCorrectiveAction C WITH(NOLOCK)
	INNER JOIN Users U ON C.RequestorID = U.UserID
	LEFT JOIN Users U1 ON C.RecordLockedBy = U1.UserID
WHERE
	CANo = @CANo
SELECT
	ModuleID
FROM
	_8DCAModuleByCA
WHERE
	CANo = @CANo
SELECT
	TeamMemberID
FROM
	_8DCATeamMember
WHERE
	CANo = @CANo
SELECT
	RiskAssessmentAreaID
FROM
	_8DCA_RiskAssessmentAreaByCANo
WHERE
	CANo = @CANo
END
GO