﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[fds__fn_bankingtransactions]
(
	@startdate date = NULL
	,@enddate date = NULL
	,@invoice_startdate date = NULL
	,@invoice_enddate date = NULL
	,@invoice_idlist [dbo].[fds__tt__idlist_vchar] READONLY
)
RETURNS @bankingtransactions TABLE
(
	[taID] varchar(10)
	,[AccountIdentification] varchar(50)
	,[ValueDate] date
	,[Amount] numeric(9,2)
	,[AccountNumberOfPayer] varchar(30)
	,[NameOfPayer] nvarchar(60)
	,[SepaRemittanceInformation] varchar(150)
	,[EndToEndReference] varchar(50)
	,[manu] bit
	,[InvId] varchar(15)
	,[done_manually] bit
	,[fds] bit
)
AS
BEGIN
	-- Fill the table variable with the rows for your result set
	DECLARE @firstever date = '1900-01-01', @tomorrow date = DATEADD(dAy, 1, GETDATE());
	SELECT @invoice_startdate = ISNULL(@invoice_startdate, @firstever), @invoice_enddate = ISNULL(@invoice_enddate, @tomorrow);
	DECLARE @idlistempty bit = IIF(EXISTS (SELECT 0 FROM @invoice_idlist) ,0,1);

	with bt as (
		SELECT [taID],[AccountIdentification],[ValueDate],[Amount],AccountNumberOfPayer,NameOfPayer, SepaRemittanceInformation, EndToEndReference
		FROM [dbo].[fds__bankingtransactions] as bt_
		where (@startdate is null OR bt_.ValueDate >= @startdate)
			AND (@enddate is null or bt_.ValueDate <= @enddate)
			AND (bt_.[DebitCreditMark] in ('C')
				AND NOT (ISNULL(bt_.[AccountNumberOfPayer],'') IN ('DE52301502000002091478') AND ISNULL(bt_.[SepaRemittanceInformation],'') like ('%umbuchung%'))
				AND NOT (ISNULL(bt_.[AccountNumberOfPayer],'') IN ('DE23501108006161606386') AND ISNULL(bt_.[SepaRemittanceInformation],'') like ('%ebay%'))
				AND NOT (ISNULL(bt_.[SepaRemittanceInformation],'') like ('%Umsatzsteuer%'))
				AND ISNULL(bt_.AccountNumberOfPayer,'') <> ''
			) 
	), bs0manu as (
		SELECT bs.[taID], [invid] = spl.[value], [manu] = cast(1 as bit)
		FROM [dbo].[fds__bankingtransactions_settings] as bs
			CROSS APPLY STRING_SPLIT(bs.assigned_invoice_id, ',') as spl
			JOIN bt on bt.[taID] = bs.[taID] 
		WHERE bs.assigned_invoice_id is not null
	), bs1 as (
		SELECT * FROM bs0manu
		UNION
		SELECT bs.[taID], [invid] = spl.[value], [manu] = cast(0 as bit)  
		FROM [dbo].[fds__bankingtransactions_settings] as bs
			CROSS APPLY STRING_SPLIT(bs.auto_invoice_id, ',') as spl
			JOIN bt on bt.[taID] = bs.[taID] 
		WHERE bs.auto_invoice_id is not null
			and bt.[taID] NOT IN (SELECT [taID] FROM bs0manu)
	), bs as (
		SELECT [taID], [invid], manu = CAST(MAX(CAST([manu] as int))  as bit)
		from bs1
		WHERE @idlistempty = 1 OR bs1.[invid] in (SELECT [id] FROM @invoice_idlist)
		GROUP BY [taID], [invid]
	)
	INSERT INTO @bankingtransactions
	SELECT 
		bt.[taID]
		,[AccountIdentification]
		, bt.[ValueDate]
		, [Amount]
		,bt.AccountNumberOfPayer
		,bt.NameOfPayer
		,bt.SepaRemittanceInformation
		,bt.EndToEndReference
		,[manu]
		,[invid]
		,[done_manually] = IIF(ISNULL(bs2.[done_manually],'') <> '', 1,0)
		,[fds] = IIF(fi.isExternal = 0, 1, 0)
	FROM bt 
		JOIN bs ON bt.[taID] = bs.[taID] 
		LEFT JOIN [dbo].[fds__bankingtransactions_settings] as bs2 ON bt.[taID] = bs2.[taID]
		LEFT JOIN [dbo].[fds__invoices] as fi on bs.[invid] = fi.[id] AND fi.[DateFinalized] BETWEEN @invoice_startdate AND @invoice_enddate

	RETURN 
END