USE [DD_ECM]
GO

DECLARE	@return_value int
EXEC	@return_value = [dbo].[PRDEX_START_PROFILE_EXECUTION]
		@pPROFILE_ID = 2,
		@pTEST_MODE = 0,
		@pASYNC_MODE = 0,
		@pBATCH_ID = N'4712',
		@pBATCH_CREATOR = N'meSamB',
		@pLANGUAGE = N'de-de',
		@pLOG_LEVEL = N'info',
		@pSTART_QUERY_VAR1 = 'erledigt'

SELECT	'Return Value' = @return_value
GO

SELECT * FROM [DD_ECM].[dbo].[TBDEX_RUN_LOG]

-- =========================================
-- Validation: OUT <-> OUT_REFERENCES mapping
-- =========================================

-- 1) Letzte 50 Outputs inkl. Anzahl referenzzeilen
SELECT TOP (50)
		 [O].[GUID] AS [OUT_ID],
		 [O].[PROFILE_ID],
		 [O].[BATCH_ID],
		 [O].[ADDED_WHEN],
		 COUNT([R].[GUID]) AS [REFERENCE_ROWS]
FROM [DD_ECM].[dbo].[TBDEX_OUT] AS [O]
LEFT JOIN [DD_ECM].[dbo].[TBDEX_OUT_REFERENCES] AS [R]
	ON [R].[OUT_ID] = [O].[GUID]
GROUP BY [O].[GUID],[O].[PROFILE_ID],[O].[BATCH_ID],[O].[ADDED_WHEN]
ORDER BY [O].[GUID] DESC;

-- 2) Outputs ohne Referenzzeile (sollte 0 Zeilen liefern)
SELECT
		 [O].[GUID] AS [OUT_ID],
		 [O].[PROFILE_ID],
		 [O].[BATCH_ID],
		 [O].[ADDED_WHEN]
FROM [DD_ECM].[dbo].[TBDEX_OUT] AS [O]
LEFT JOIN [DD_ECM].[dbo].[TBDEX_OUT_REFERENCES] AS [R]
	ON [R].[OUT_ID] = [O].[GUID]
WHERE [R].[GUID] IS NULL
ORDER BY [O].[GUID] DESC;

-- 3) Referenzzeilen ohne Parent (sollte 0 Zeilen liefern)
SELECT
		 [R].[GUID] AS [REFERENCE_ID],
		 [R].[OUT_ID],
		 [R].[EXPORT_NUMBER],
		 [R].[ADDED_WHEN]
FROM [DD_ECM].[dbo].[TBDEX_OUT_REFERENCES] AS [R]
LEFT JOIN [DD_ECM].[dbo].[TBDEX_OUT] AS [O]
	ON [O].[GUID] = [R].[OUT_ID]
WHERE [O].[GUID] IS NULL
ORDER BY [R].[GUID] DESC;
-- delete FROM [DD_ECM].[dbo].[TBDEX_RUN_LOG]


/****

	  SELECT DISTINCT %PROFILE_ID%														as 'PROFILE_ID',
				%BATCH_ID%															as 'BATCH_ID',
				[BA].[dwDocID]														as 'REFERENCE1', 
				RIGHT(LEFT(ISNULL([BA].[dwDate09],RIGHT(year(getdate()),2)),4),2)	as 'REFERENCE2', 
				NULL																as 'REFERENCE3', 
				NULL																as 'REFERENCE4', 
				NULL																as 'REFERENCE5',
				[dbo].[FNDD_GET_WINDREAM_FILE_PATH]([BA].[dwDocID],1)				as 'EXTERNAL_REFERENCE1',
				NULL																as 'EXTERNAL_REFERENCE2', 
				NULL																as 'EXTERNAL_REFERENCE3', 
				NULL																as 'EXTERNAL_REFERENCE4', 
				NULL																as 'EXTERNAL_REFERENCE5',
				%TEST_MODE%															as 'TEST_MODE', 
				%ASYNC_MODE%														as 'ASYNC_MODE', 
				NULL																as 'COMMENT', 
				'%BATCH_CREATOR%'													as 'ADDED_WHO', 
				GETDATE()															as 'ADDED_WHEN'
FROM %CONST_WD_DB%.[BaseAttributes] as [BA] (NOLOCK)
INNER JOIN %CONST_WD_DB%.[Vector] as [V] (NOLOCK) ON [BA].[dwDocID] = [V].[dwDocID]
WHERE [BA].[dwDocID] is not null
	and  [BA].[dwCatalogID] = 1
	and  [BA].[szText37] in ('Eingangsrechnung-WF','Eingangsgutschrift-WF')					-- Dokumentart
	and  [BA].[szText32] = 'Bestätigt'														-- Status-Detail
	and  [BA].[szText33] = '%START_QUERY_VAR1%'												-- Status 
	and  ([BA].[dwDate04] is null or [BA].[dwDate04] = 19700101)							-- Export-Datum
	and  LEFT([BA].[dwDate09],4) in (year(GETDATE()),year(GETDATE())-1,year(GETDATE())+1)	-- Beleg-Datum
	and  ([BA].[dwInteger20] is NULL or [BA].[dwInteger20] = 0)								-- Buchungsnummer
	and  (SELECT COUNT(*) FROM %CONST_WD_DB%.[Vector] as [V] (NOLOCK) WHERE [BA].[dwDocID] = [V].[dwDocID] and [V].[dwAttrID] = %CONST_WD_VECTOR_ID3% and [V].[blValue] = 1) > 0 --WF: Zahlungsfreigabe der Geschäftsführung
	and  (SELECT COUNT(*) FROM %CONST_WD_DB%.[Vector] as [V] (NOLOCK) WHERE [BA].[dwDocID] = [V].[dwDocID] and [V].[dwAttrID] = %CONST_WD_VECTOR_ID4% and [V].[blValue] = 1) = 0 --WF: Manuelle Schleupen Übergabe
ORDER BY [BA].[dwDocID] ASC;


  	SELECT  %PROFILE_ID%                                                                            as [PROFILE_ID],
            %BATCH_ID%                                                                              as [BATCH_ID],
			'EXPORT_BOOKING_NUMBER'																	as [Belegnummer (X)],
			FORMAT(CONVERT(DATE,CONVERT(VARCHAR(8),LEFT([dwDate09],8)),104),'dd.MM.yyyy')			as [Belegdatum (X)],
			FORMAT(CONVERT(DATE,CONVERT(VARCHAR(8),LEFT([dwDate07],8)),104),'dd.MM.yyyy')			as [Valutadatum],
			'K'																						as [Kontoart (X)],
			REPLACE(REPLACE([szText38],';',''),' ','')												as [Kontonummer (X)],
			NULL																					as [Gegenkontoart (X)],
			NULL																					as [Gegenkonto],
			NULL																					as [Zusatzkontoart],
			NULL																					as [Zusatzkonto],
			TRY_CONVERT(float,[lfFloat12])															as [Betrag],
			'IncomeExpensesFlag'																		as [S/H-Kennzeichen],
			''																						as [Steuerschlüssel],
			REPLACE(REPLACE([szText02],';',''),CHAR(13) + CHAR(10),'')								as [Buchungstext],
			NULL																					as [Zahlungsträgerhinweis],
			NULL																					as [Geschäftsbereich],
			FORMAT(CONVERT(DATE,CONVERT(VARCHAR(8),LEFT([decCreationTime],8)),104),'dd.MM.yyyy')	as [externes Rechnungsdatum],
			REPLACE([szText19],';','')																as [externe Rechnungsnummer],
			NULL																					as [Rechnungseinheit],
			FORMAT(CONVERT(DATE,CONVERT(VARCHAR(8),LEFT([dwDate06],8)),104),'dd.MM.yyyy')			as [Fälligkeitsdatum],
			NULL																					as [Skontotage 1],
			NULL																					as [Skonto Prozent 1],
			NULL																					as [Skontotage 2],
			NULL																					as [Skonto Prozent 2],
			[dwInteger22]																			as [Nettotage]
	FROM	[windreamDB].[dbo].[BaseAttributes] (NOLOCK)
	WHERE	[dwDocID]		= '%REFERENCE1%'
	  and	[szText33]		= 'in Arbeit';


	  
****/