﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[GetOpenActionItemsByUser]    Script Date: 11/21/2024 11:29:04 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	CREATE PROCEDURE [dbo].[GetOpenActionItemsByUser] @UserID INT AS BEGIN -- 2019/01/28 - add overdue CA (ticket #35315)
SET
	NOCOUNT ON;

SELECT
	CNT.CANo AS IssueID,
	CNT.CANo AS DocID,
	'8D' AS DocumentTypeDisplay,
	'CorrectiveAction' AS DocumentType,
	U1.FirstName + ' ' + U1.LastName AS Originator,
	CNT.AssignedDate,
	ECD AS DueDate,
	'' AS pcrMesaID
FROM
	_8D_D3ContainmentAction CNT
	INNER JOIN _8DCorrectiveAction CA On CNT.CANo = CA.CANo
	LEFT JOIN Users U1 ON CA.RequestorID = U1.UserID
WHERE
	ImplementedDate IS NULL
	AND ResponsibilityOwnerID = @UserID
	AND CA.Deleted = 0
UNION
ALL
SELECT
	CNT.CANo AS IssueID,
	CNT.CANo AS DocID,
	'8D' AS DocumentTypeDisplay,
	'CorrectiveAction' AS DocumentType,
	U1.FirstName + ' ' + U1.LastName AS Originator,
	CNT.AssignedDate,
	ECD AS DueDate,
	'' AS pcrMesaID
FROM
	_8D_D5D6CorrectiveAction CNT
	INNER JOIN _8DCorrectiveAction CA On CNT.CANo = CA.CANo
	LEFT JOIN Users U1 ON CA.RequestorID = U1.UserID
WHERE
	ImplementedDate IS NULL
	AND ResponsibilityOwnerID = @UserID
	AND Approved = 1
	AND CA.Deleted = 0
UNION
ALL
SELECT
	CNT.CANo AS IssueID,
	CNT.CANo AS DocID,
	'8D' AS DocumentTypeDisplay,
	'CorrectiveAction' AS DocumentType,
	U1.FirstName + ' ' + U1.LastName AS Originator,
	CNT.AssignedDate,
	ECD AS DueDate,
	'' AS pcrMesaID
FROM
	_8D_D7PreventiveAction CNT
	INNER JOIN _8DCorrectiveAction CA On CNT.CANo = CA.CANo
	LEFT JOIN Users U1 ON CA.RequestorID = U1.UserID
WHERE
	ImplementedDate IS NULL
	AND ResponsibilityOwnerID = @UserID
	AND CA.Deleted = 0
UNION
ALL
SELECT
	A.AuditNo AS IssueID,
	A.AuditNo AS DocID,
	'Audit' AS DocumentType,
	'Audit' AS DocumentType,
	U1.FirstName + ' ' + U1.LastName AS Originator,
	Af.AssignedDate,
	ECD AS DueDate,
	'' AS pcrMesaID
FROM
	_8DCAFindings AF
	INNER JOIN _8DAudit A On AF.AuditNo = A.AuditNo
	LEFT JOIN Users U1 ON A.OriginatorID = U1.UserID
WHERE
	ImplementedDate IS NULL
	AND ResponsibilityOwnerID = @UserID
END
GO