-- Copyright (C) 2026  Eduard Wisch  <data@data-it-solution.de>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

CREATE TABLE llx_bankimport_transaction (
    rowid           INTEGER AUTO_INCREMENT PRIMARY KEY,
    ref             VARCHAR(128) NOT NULL,              -- Unique reference (hash of transaction)
    entity          INTEGER DEFAULT 1 NOT NULL,         -- Multi-company id

    -- Bank account info
    iban            VARCHAR(34),                         -- IBAN of the account
    bic             VARCHAR(11),                         -- BIC of the bank

    -- Transaction data
    datec           DATETIME,                            -- Creation date in system
    tms             TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    date_trans      DATE NOT NULL,                       -- Transaction date (booking date)
    date_value      DATE,                                -- Value date

    -- Counterparty
    name            VARCHAR(255),                        -- Counterparty name
    counterparty_iban VARCHAR(34),                       -- Counterparty IBAN
    counterparty_bic  VARCHAR(11),                       -- Counterparty BIC

    -- Amount
    amount          DOUBLE(24,8) NOT NULL,               -- Amount (positive = credit, negative = debit)
    currency        VARCHAR(3) DEFAULT 'EUR',            -- Currency code

    -- Description
    label           VARCHAR(255),                        -- Short label/booking text
    description     TEXT,                                -- Full description/reference
    end_to_end_id   VARCHAR(128),                        -- End-to-end ID from SEPA
    mandate_id      VARCHAR(128),                        -- Mandate ID for direct debits

    -- Matching with Dolibarr
    fk_bank         INTEGER,                             -- Link to llx_bank (bank line)
    fk_facture      INTEGER,                             -- Link to llx_facture (customer invoice)
    fk_facture_fourn INTEGER,                            -- Link to llx_facture_fourn (supplier invoice)
    fk_paiement     INTEGER,                             -- Link to llx_paiement
    fk_paiementfourn INTEGER,                            -- Link to llx_paiementfourn
    fk_salary       INTEGER,                             -- Link to llx_salary (salary payment)
    fk_don          INTEGER,                             -- Link to llx_don (donation)
    fk_loan         INTEGER,                             -- Link to llx_loan (loan payment)
    fk_societe      INTEGER,                             -- Link to llx_societe (third party)
    fk_statement    INTEGER,                             -- Link to llx_bankimport_statement (PDF statement)

    -- Status
    status          SMALLINT DEFAULT 0,                  -- 0=new, 1=matched, 2=reconciled, 9=ignored
    import_key      VARCHAR(64),                         -- Import batch key

    -- User tracking
    fk_user_creat   INTEGER,                             -- User who imported
    fk_user_modif   INTEGER,                             -- User who last modified
    fk_user_match   INTEGER,                             -- User who matched/reconciled
    date_match      DATETIME,                            -- Date of matching

    note_private    TEXT,                                -- Private notes
    note_public     TEXT                                 -- Public notes
) ENGINE=InnoDB;
