﻿
CREATE   PROCEDURE [dbo].[fds__setBankingtransaction_manual]
    @taID                       varchar(10)     = NULL,  -- NULL=INSERT, value=UPDATE, value+others NULL=DELETE
    @ValueDate                  date            = NULL,
    @Amount                     numeric(9,2)    = NULL,
    @NameOfPayer                nvarchar(140)   = NULL,
    @SepaRemittanceInformation  varchar(200)    = NULL,
    @authuser                   varchar(100)    = NULL
AS
BEGIN
    SET NOCOUNT ON;
    IF [dbo].[fis_getModuleAuth]('fds_bam', @authuser) < 2
        THROW 60000, N'not authorized', 1;

    BEGIN TRY

        -- DELETE
        IF @taID IS NOT NULL AND @Amount IS NULL
        BEGIN
            DELETE FROM [dbo].[fds__bankingtransactions]
            WHERE [taID] = @taID;
            RETURN;
        END

        -- INSERT
        IF @taID IS NULL
        BEGIN
            INSERT INTO [dbo].[fds__bankingtransactions]
                ([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](), '999', @ValueDate, @Amount,
                 NULL, NULL, NULL,
                 NULL, NULL, NULL,
                 NULL, NULL, NULL,
                 NULL, @NameOfPayer, NULL,
                 NULL, NULL,
                 'MANUELLE GUTSCHRIFT', @SepaRemittanceInformation, NULL,
                 NULL, 0, NULL,
                 NULL, 'C', 'MAN');
            RETURN;
        END

        -- UPDATE
        UPDATE [dbo].[fds__bankingtransactions]
        SET
            [ValueDate]                     = @ValueDate,
            [Amount]                        = @Amount,
            [NameOfPayer]                   = @NameOfPayer,
            [SepaRemittanceInformation]     = @SepaRemittanceInformation
        WHERE [taID] = @taID;

    END TRY
    BEGIN CATCH
        THROW;
    END CATCH
END