-- ============================================================
-- quoERP — Cuentas contables individuales por beneficio de ley
-- Migración: 016_payroll_benefit_accounts.sql
-- Versión: 1.0.0 | Fecha: 2026-06-06
-- ============================================================
-- Desglosa "acc_benefits_expense" y "acc_benefits_payable" en
-- cuentas individuales por cada beneficio de ley Ecuador:
--   Décimo tercer sueldo · Décimo cuarto · Fondos de reserva · Vacaciones
-- Los campos agrupados siguen existiendo como fallback.
-- ============================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

ALTER TABLE company_account_map
    -- GASTOS individuales (DÉBITOS)
    ADD COLUMN IF NOT EXISTS acc_decimo3_expense  INT UNSIGNED DEFAULT NULL
        COMMENT '5.3.03 Gasto décimo tercer sueldo',
    ADD COLUMN IF NOT EXISTS acc_decimo4_expense  INT UNSIGNED DEFAULT NULL
        COMMENT '5.3.04 Gasto décimo cuarto sueldo',
    ADD COLUMN IF NOT EXISTS acc_fondos_expense   INT UNSIGNED DEFAULT NULL
        COMMENT '5.3.05 Gasto fondos de reserva',
    ADD COLUMN IF NOT EXISTS acc_vacation_expense INT UNSIGNED DEFAULT NULL
        COMMENT '5.3.06 Gasto vacaciones',
    -- PASIVOS individuales (CRÉDITOS)
    ADD COLUMN IF NOT EXISTS acc_decimo3_payable  INT UNSIGNED DEFAULT NULL
        COMMENT '2.1.05.02 Décimo tercer sueldo por pagar',
    ADD COLUMN IF NOT EXISTS acc_decimo4_payable  INT UNSIGNED DEFAULT NULL
        COMMENT '2.1.05.03 Décimo cuarto sueldo por pagar',
    ADD COLUMN IF NOT EXISTS acc_fondos_payable   INT UNSIGNED DEFAULT NULL
        COMMENT '2.1.04.04 Fondos de reserva IESS por pagar',
    ADD COLUMN IF NOT EXISTS acc_vacation_payable INT UNSIGNED DEFAULT NULL
        COMMENT '2.1.05.04 Vacaciones por pagar';

-- FKs (separadas porque ADD CONSTRAINT no soporta IF NOT EXISTS)
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_d3_exp FOREIGN KEY (acc_decimo3_expense)  REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_d4_exp FOREIGN KEY (acc_decimo4_expense)  REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_fo_exp FOREIGN KEY (acc_fondos_expense)   REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_va_exp FOREIGN KEY (acc_vacation_expense) REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_d3_pay FOREIGN KEY (acc_decimo3_payable)  REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_d4_pay FOREIGN KEY (acc_decimo4_payable)  REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_fo_pay FOREIGN KEY (acc_fondos_payable)   REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_va_pay FOREIGN KEY (acc_vacation_payable) REFERENCES chart_of_accounts(id) ON DELETE SET NULL;

SET FOREIGN_KEY_CHECKS = 1;
