USE [DD_ECM]
GO

/****** Object:  UserDefinedFunction [dbo].[FNCUST_GET_PM_OVERVIEW]    Script Date: 21.03.2024 17:20:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- Stand: MK // 28.10.2021
-- 28.10.2021 Mandant hinzugefügt, Dateiname verschoben
-- 20.09.2021 Initial

CREATE FUNCTION [dbo].[FNCUST_GET_PM_OVERVIEW] (	
		@USER_ID		 BIGINT
)
RETURNS TABLE  
AS
RETURN (  

	/*## Following Columns need to be part of sql ##  
	TL_STATE  Returns the TrafficLight-State for each doc (1=Red;2=Orange;3=Green)  
	PROFILE_ID  TITLE   Profile-Title used for default-grouping  
	DOC_ID  
	FULL_FILE_PATH  
	LAST USE
	LAST EDITED   */
	--DECLARE @USER_ID INTEGER = 20;
	 SELECT
		T.GUID,
		DATEDIFF(DAY, [dbo].[FNPM_LAST_EDITED_DOC] (T.PROFIL_ID,T.DOC_ID),GETDATE())				AS [Zuletzt Bearbeitet],
		Case DATEDIFF(DAY, [dbo].[FNPM_LAST_EDITED_DOC] (T.PROFIL_ID,T.DOC_ID),GETDATE()) 
			WHEN 0 THEN 3
			WHEN 1 THEN 3
			WHEN 2 THEN 3
			WHEN 3 THEN 2 
			WHEN 4 THEN 2
			WHEN 5 THEN 2
			WHEN 6 THEN 2
			WHEN 7 THEN 2
			ELSE 1 		-- Wenn drei tage = gelb | wenn mehr als 7 tage = rot
		END 																						AS TL_STATE,   
		T.PROFIL_ID 																				AS PROFILE_ID,   
		T.DOC_ID 																					AS DocID,    
		T.FILE_PATH 																				AS FULL_FILE_PATH,       
		TWM_BA.szText37 																			AS Dokumentart,   
		TWM_BA.szText36 																			AS 'NAV-Nr.',    
		--TWM_BA.lfFloat12 																			AS 'Rechnungsbetrag',    
		TWM_BA.szText19 																			AS 'ex-RechnungsNummer', 
		TWM_BA.szText38 																			AS 'Kreditor-Nummer',   
		TWM_BA.szText08 																			AS 'Kreditor-Name',   
		T.DMS_ERSTELLT_DATE 																		AS 'Erhalten wann',  
		TWM_BA.szText22 																			AS 'Prüfer',  
		[dbo].[FNPM_LAST_WORKUSER_DOC] (T.PROFIL_ID,T.DOC_ID) 										AS 'Letzter Bearbeiter', 
		FORMAT(CONVERT(date, convert(varchar(8),[TWM_BA].[dwChangeDate]), 120), N'dd.MM.yyyy')		AS 'Letzte Bearbeitung',   --Default: [dbo].[FNPM_LAST_EDITED_DOC] (T.PROFIL_ID,T.DOC_ID) AS 'Letzte Bearbeitung', 
		--FORMAT(CONVERT(date, convert(varchar(8),[TWM_BA].[dwDate06]), 120), N'dd.MM.yyyy')			AS 'Fällig am',   --Default: [dbo].[FNPM_LAST_EDITED_DOC] (T.PROFIL_ID,T.DOC_ID) AS 'Letzte Bearbeitung', 
		TWM_BA.szText31 																			AS 'Mandant',  
		TWM_BA.szLongName 																			AS Dateiname, 
		[dbo].[FNPM_PROFILE_GROUP_COLOR] (T.PROFIL_ID) 												AS 'GROUP_COLOR',   
		[dbo].[FNPM_PROFILE_GROUP_TEXT] (T.PROFIL_ID) 												AS 'GROUP_TEXT'  
		,0 CONV_YN
		FROM 
			[FNPM_CUST_GET_DOC_IDS] (@USER_ID) TDOC 
			INNER JOIN TBPM_PROFILE_FILES T					ON TDOC.DOC_GUID	= T.GUID
			INNER JOIN TBPM_PROFILE T1						ON T.PROFIL_ID		= T1.GUID
			INNER JOIN windream60.dbo.BaseAttributes TWM_BA ON T.DOC_ID			= TWM_BA.dwDocID
			  AND T.EDIT = 0  AND T.IN_WORK = 0 AND T.ACTIVE = 1

		--ORDER By T1.PRIORITY

		-- SELECT * FROM [dbo].[FNPM_GET_ACTIVE_PROFILES_USER] (20)
		-- SELECT * FROM [dbo].[FNPM_CUST_GET_DOC_IDS] (2)

		-- Aufruf aus ProcessManager:
		-- Stand: MK // 28.01.2021 
		-- SELECT * FROM [FNCUST_GET_PM_OVERVIEW]({#USER#USER_ID})
		-- ORDER BY [Zuletzt Bearbeitet] DESC

);  
GO


