﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[backup__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
(
	[uid] bigint
	,[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 [uid],[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)
	), bs0manu as (
		SELECT bs.[banking_uid], [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.[uid] = bs.[banking_uid] 
		WHERE bs.assigned_invoice_id is not null
	), bs1 as (
		SELECT * FROM bs0manu
		UNION
		SELECT bs.[banking_uid], [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.[uid] = bs.[banking_uid] 
		WHERE bs.auto_invoice_id is not null
			and bt.[uid] NOT IN (SELECT [banking_uid] FROM bs0manu)
	), bs as (
		SELECT banking_uid, [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 banking_uid, [invid]
	)
	INSERT INTO @bankingtransactions
	SELECT 
		bt.[uid]
		,[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(mi.id is null, 1, 0)
	FROM bt 
		JOIN bs ON bt.[uid] = bs.[banking_uid] 
		LEFT JOIN [dbo].[fds__bankingtransactions_settings] as bs2 ON bt.[uid] = bs2.[banking_uid]
		LEFT JOIN [dbo].[fds__invoices] as fi on bs.[invid] = fi.[id] AND fi.[DateFinalized] BETWEEN @invoice_startdate AND @invoice_enddate
		LEFT JOIN [dbo].[mfr__invoices] as mi on try_cast(bs.[invid] as bigint) = mi.[id] AND mi.[DateOfCreation] BETWEEN @invoice_startdate AND @invoice_enddate;

	RETURN 
END