--
-- Script run when an upgrade of Dolibarr is done. Whatever is the Dolibarr version.
--

-- v1.1: Add fk_statement to transaction table
ALTER TABLE llx_bankimport_transaction ADD COLUMN fk_statement INTEGER AFTER fk_societe;
ALTER TABLE llx_bankimport_transaction ADD INDEX idx_bankimport_transaction_fk_statement (fk_statement);

-- v1.3: Add statement_line table for parsed PDF transactions
CREATE TABLE IF NOT EXISTS llx_bankimport_statement_line (
    rowid           INTEGER AUTO_INCREMENT PRIMARY KEY,
    fk_statement    INTEGER NOT NULL,
    entity          INTEGER DEFAULT 1 NOT NULL,
    line_number     INTEGER DEFAULT 0,
    date_booking    DATE NOT NULL,
    date_value      DATE,
    transaction_type VARCHAR(100),
    amount          DOUBLE(24,8) NOT NULL,
    currency        VARCHAR(3) DEFAULT 'EUR',
    name            VARCHAR(255),
    description     TEXT,
    fk_bank         INTEGER,
    datec           DATETIME,
    tms             TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;
ALTER TABLE llx_bankimport_statement_line ADD INDEX idx_bankimport_stmtline_fk_statement (fk_statement);
ALTER TABLE llx_bankimport_statement_line ADD INDEX idx_bankimport_stmtline_entity (entity);
ALTER TABLE llx_bankimport_statement_line ADD INDEX idx_bankimport_stmtline_date_booking (date_booking);
ALTER TABLE llx_bankimport_statement_line ADD INDEX idx_bankimport_stmtline_amount (amount);
ALTER TABLE llx_bankimport_statement_line ADD INDEX idx_bankimport_stmtline_fk_bank (fk_bank);
ALTER TABLE llx_bankimport_statement_line ADD UNIQUE INDEX uk_bankimport_stmtline (fk_statement, line_number, entity);

-- v1.4: Add match_status to statement_line for approval workflow
ALTER TABLE llx_bankimport_statement_line ADD COLUMN match_status VARCHAR(20) DEFAULT NULL AFTER fk_bank;
