﻿USE [FabApprovalSystem]
GO
	/****** Object:  UserDefinedFunction [dbo].[fn_GetChangeControl]    Script Date: 11/21/2024 11:31:55 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE FUNCTION [dbo].[fn_GetChangeControl] () RETURNS TABLE AS RETURN (
		-- Add the SELECT statement with parameter references here
		-- M_Suffix
		SELECT
			DISTINCT 'ChangeControl' AS DocumentType,
			'ChangeControl' AS SubDoc,
			CC.PlanNumber AS IssueID,
			CC.PlanNumber AS DocID,
			CC.InsertTimeStamp AS IssueDate,
			'' AS LotNos,
			'' AS 'PendingApprovers',
			Title,
			ChangeDescription AS IssueDescription,
			U.FirstName + ' ' + U.LastName AS Originator,
			Dates.StartDate AS SubmitedDate,
			ClosedDate AS CloseDate
		FROM
			CCChangeControl CC
			LEFT JOIN Users U ON CC.OwnerID = U.UserID
			LEFT JOIN (
				SELECT
					PlanNumber,
					MIN(MeetingDate) AS StartDate,
					MAX(MeetingDate) AS StatusDate
				FROM
					CCMeeting
				GROUP BY
					PlanNumber
			) AS Dates ON CC.PlanNumber = Dates.PlanNumber
	)
GO