﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[CCGetMeeting]    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].[CCGetMeeting] -- Add the parameters for the stored procedure here	
	@MeetingID INT,
	@UserID INT,
	@CanViewITAR INT OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

DECLARE @ITARRowCount INT IF NOT EXISTS (
	SELECT
		*
	FROM
		CCMeeting
	WHERE
		RecordLockIndicator = 1
		AND MeetingDate = @MeetingID
) BEGIN
UPDATE
	CCMeeting
SET
	RecordLockIndicator = 1,
	RecordLockedBy = @UserID,
	RecordLockedDate = GETDATE(),
	LastUpdateDate = GETDATE()
WHERE
	MeetingID = @MeetingID
END
SET
	@CanViewITAR = 1 IF EXISTS (
		SELECT
			*
		FROM
			CCMeeting M
			INNER JOIN CCChangeControl C ON M.PlanNumber = C.PlanNumber
		WHERE
			MeetingID = @MeetingID
			AND C.IsITAR = 1
	) BEGIN
SELECT
	@ITARRowCount = COUNT(*)
FROM
	dbo.fnIsUserITARCompliant(@UserID) IF (@ITARRowCount > 0)
SET
	@CanViewITAR = 1
	ELSE
SET
	@CanViewITAR = 0
END -- Insert statements for procedure here
SELECT
	M.MeetingID,
	ISNULL(LTRIM(RTRIM(M.PCRB)), 'Select') PCRB,
	M.MeetingDate,
	M.Decision,
	M.Notes,
	C.ChangeLevel,
	C.Title,
	C.PlanNumber,
	C.CurrentStep,
	M.RecordLockedBy,
	M.RecordLockIndicator,
	U1.FirstName + ' ' + U1.LastName AS RecordLockByName,
	CASE
		WHEN C.ClosedDate IS NULL THEN 0
		ELSE 1
	END PCRBClosed
FROM
	CCMeeting M
	INNER JOIN CCChangeControl C ON M.PlanNumber = C.PlanNumber
	LEFT JOIN Users U1 ON M.RecordLockedBy = U1.UserID
WHERE
	MeetingID = @MeetingID
SELECT
	PCRValue AS PCRValueID
FROM
	CCMeetingPCRValue
WHERE
	MeetingID = @MeetingID
END
GO