﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__merge_bankingtransactions]
	@tblname as nvarchar(50)
	,@authuser varchar(25)
AS
BEGIN
	SET NOCOUNT ON;

    DECLARE @now datetime = GETUTCDATE();
    DECLARE @tmp [dbo].[fds__tt__bankingtransactions];

	DECLARE @tmp_cmd nvarchar(1000) = N'SELECT DISTINCT * FROM ' + @tblname
	INSERT INTO @tmp EXECUTE [sp_executesql] @tmp_cmd;
	
	INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
		VALUES ('fds__merge_bankingtransactions' ,ISNULL(@authuser,''), '');

	MERGE [dbo].[fds__bankingtransactions] as TARGET
		USING @tmp as SOURCE ON 
				TARGET.[AccountIdentification] = SOURCE.[AccountIdentification]
           AND TARGET.[ValueDate] = SOURCE.[ValueDate]
           AND TARGET.[Amount] = SOURCE.[Amount]
           AND TARGET.[FundsCode] = SOURCE.[FundsCode]
           AND TARGET.[AccountNumberOfPayer] = SOURCE.[AccountNumberOfPayer]
           AND TARGET.[BankCodeOfPayer] = SOURCE.[BankCodeOfPayer]
		   AND TARGET.[EndToEndReference] = SOURCE.[EndToEndReference]
		   AND TARGET.[SepaRemittanceInformation] = SOURCE.[SepaRemittanceInformation]
	WHEN NOT MATCHED BY TARGET THEN 
		INSERT
           ([taID]
		   ,[AccountIdentification]
           ,[ValueDate]
           ,[Amount]
           ,[FundsCode]
           ,[AccountNumberOfPayer]
           ,[BankCodeOfPayer]
           ,[CompensationAmount]
           ,[CreditorReference]
           ,[CreditorsReferenceParty]
           ,[CustomerReference]
           ,[EndToEndReference]
           ,[JournalNumber]
           ,[MandateReference]
           ,[NameOfPayer]
           ,[OriginalAmount]
           ,[OriginatorsIdentificationCode]
           ,[PayersReferenceParty]
           ,[PostingText]
           ,[SepaRemittanceInformation]
           ,[TextKeyAddition]
           ,[TransactionCode]
           ,[IsUnstructuredData]
           ,[UnstructuredData]
           ,[UnstructuredRemittanceInformation]
           ,[DebitCreditMark]
           ,[TransactionTypeIdCode])
     VALUES
           ( [dbo].[fds__fn_bankingtransaction_id]()
		   , SOURCE.[AccountIdentification]
           , SOURCE.[ValueDate]
           , SOURCE.[Amount]
           , SOURCE.[FundsCode]
           , SOURCE.[AccountNumberOfPayer]
           , SOURCE.[BankCodeOfPayer]
           , SOURCE.[CompensationAmount]
           , SOURCE.[CreditorReference]
           , SOURCE.[CreditorsReferenceParty]
           , SOURCE.[CustomerReference]
           , SOURCE.[EndToEndReference]
           , SOURCE.[JournalNumber]
           , SOURCE.[MandateReference]
           , SOURCE.[NameOfPayer]
           , SOURCE.[OriginalAmount]
           , SOURCE.[OriginatorsIdentificationCode]
           , SOURCE.[PayersReferenceParty]
           , SOURCE.[PostingText]
           , LEFT(SOURCE.[SepaRemittanceInformation], 200)
           , SOURCE.[TextKeyAddition]
           , SOURCE.[TransactionCode]
           , SOURCE.[IsUnstructuredData]
           , SOURCE.[UnstructuredData]
           , SOURCE.[UnstructuredRemittanceInformation]
           , SOURCE.[DebitCreditMark]
           , SOURCE.[TransactionTypeIdCode]
		   )
	;

	  MERGE [dbo].[fds__admin_settings] as TARGET
		USING (VALUES('log','bankingtransaction_merge',FORMAT(@now, 'yyyy-MM-dd HH:mm:ss', 'de'), NULL)) as SRC ([type],[key],[value],[value2]) 
			ON TARGET.[type] = SRC.[type] AND TARGET.[key] = SRC.[key]
		WHEN MATCHED THEN 
			UPDATE  SET TARGET.[value] = SRC.[Value], TARGET.[value2] = SRC.[value2]
		WHEN NOT MATCHED BY TARGET THEN
			INSERT ([type], [key], [value], [value2]) VALUES (SRC.[type], SRC.[key], SRC.[value], SRC.[value2]);


	with b as (
	SELECT *, ROW_NUMBER() OVER (PARTITION BY [AccountIdentification]
		  ,[ValueDate]
		  ,[Amount]
		  ,[FundsCode]
		  ,[AccountNumberOfPayer]
		  ,[BankCodeOfPayer]
		  ,[SepaRemittanceInformation]
			order by [valuedate]) as [order]
	FROM [dbo].[fds__bankingtransactions]
	)
	DELETE FROM b where [order] > 1;


	--Process transactions and assignments

	EXECUTE [dbo].[fds__setBankingtransaction_autoAssigns];
	EXECUTE [dbo].[fds__setInvoicePaymentStatus_auto];

END