SELECT TOP 1000 [Client_DB]
     , [Kunde_Nr]
     , [Fahrzeug_Nr]
     , [Beleg_Nr]
     , [Bezug_Beleg_Nr]
     , [Beleg_Datum]
     , [Beleg_Beschreibung]
     , [Betrag]
     , [Benutzer]
     , iif([Open] = '0', 'N', 'J') as [Status]
     , CASE
           WHEN [Document Type] = '2' THEN 'R'
           WHEN [Document Type] = '3' THEN 'R'
           WHEN [Document Type] = '1' THEN 'Z'
           WHEN [Document Type] = '0' THEN 'K'
           ELSE 'X' END AS [Beleg_Art]
FROM [dbo].[Forderung_Belege]
WHERE (
	[Client_DB] = {{ appsmith.URL.queryParams.Client_DB }}
AND [Kunde_Nr] IN (SELECT [Kunde_Nr] FROM [dbo].[Forderung_Mahnung] WHERE [Rechnung_Nr] = {{ appsmith.URL.queryParams.Document_No }} )
AND (
	('D' = '{{ BelegeFilter.selectedOptionValue }}')
	OR
	('F' = '{{ BelegeFilter.selectedOptionValue }}' AND [Fahrzeug_Nr] IN (SELECT [Fahrzeug_Nr] FROM [dbo].[Forderung_Mahnung] WHERE [Rechnung_Nr] = {{ appsmith.URL.queryParams.Document_No }} )) 

)
	
) OR 1 != 1

ORDER BY Beleg_Datum
