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

-- Insert statements for procedure here
SELECT
	AI.ID,
	AI.MeetingID,
	ActionItemname,
	CCResponsibles.Responsibles ResponsiblePerson,
	CCResponsiblesID.ResponsiblesID ResponsibleID,
	CASE
		WHEN Gating = 1 THEN 'Yes'
		ELSE 'No'
	END AS Gating,
	DueDate
FROM
	CCMeetingActionItem AI
	LEFT JOIN (
		SELECT
			DISTINCT AI.ID,
			STUFF (
				(
					SELECT
						',' + LTRIM(
							RTRIM(
								CAST(U.FirstName + ' ' + U.LastName AS VARCHAR(350))
							)
						)
					FROM
						CCMeetingActionItemResponsible AIR
						INNER JOIN Users U ON AIR.ActionItemResponsible = U.UserID
					WHERE
						AI.ID = AIR.MeetingActionItemID FOR XML PATH('')
				),
				1,
				1,
				''
			) AS Responsibles
		FROM
			CCMeetingActionItem AI
	) AS CCResponsibles ON AI.ID = CCResponsibles.ID
	LEFT JOIN (
		SELECT
			DISTINCT AI.ID,
			STUFF (
				(
					SELECT
						',' + LTRIM(
							RTRIM(CAST(AIR.ActionItemResponsible AS VARCHAR(350)))
						)
					FROM
						CCMeetingActionItemResponsible AIR
					WHERE
						AI.ID = AIR.MeetingActionItemID FOR XML PATH('')
				),
				1,
				1,
				''
			) AS ResponsiblesID
		FROM
			CCMeetingActionItem AI
	) AS CCResponsiblesID ON AI.ID = CCResponsiblesID.ID
WHERE
	AI.MeetingID = @MeetingID
END
GO