-- ============================================================
-- quoERP — Cuenta bancaria para pagos a proveedores
-- Migración: 012_accounting_payments_config.sql
-- Versión: 1.0.0 | Fecha: 2026-06-05
-- ============================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- Paso 1: agregar la columna (IF NOT EXISTS evita error si ya existe)
ALTER TABLE company_account_map
    ADD COLUMN IF NOT EXISTS acc_bank_payments INT UNSIGNED DEFAULT NULL
        COMMENT 'Cuenta bancaria para egresos/pagos a proveedores (CR al pagar una OC)';

-- Paso 2: agregar la FK (separada porque ADD CONSTRAINT no soporta IF NOT EXISTS)
-- Si ya existe la FK, este paso fallará con error duplicado — es seguro ignorarlo.
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_bank_pay
        FOREIGN KEY (acc_bank_payments) REFERENCES chart_of_accounts(id) ON DELETE SET NULL;

SET FOREIGN_KEY_CHECKS = 1;
