-- ============================================================
-- quoERP — Integración Nómina → Contabilidad
-- Migración: 015_payroll_accounting.sql
-- Versión: 1.0.0 | Fecha: 2026-06-06
-- ============================================================
-- Agrega cuentas contables específicas para la contabilización
-- del rol de pago mensual según NIIF Ecuador.
-- ============================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ─────────────────────────────────────────────────────────────
-- AJUSTE: company_account_map — cuentas de nómina
-- Asiento tipo: DR Gasto sueldos / CR Sueldos por pagar + IESS + Beneficios
-- ─────────────────────────────────────────────────────────────
ALTER TABLE company_account_map
    -- DÉBITOS (Gastos)
    ADD COLUMN IF NOT EXISTS acc_salary_expense   INT UNSIGNED DEFAULT NULL
        COMMENT '5.3.01 Gasto sueldos y salarios (DR al provisionar nómina)',
    ADD COLUMN IF NOT EXISTS acc_iess_expense     INT UNSIGNED DEFAULT NULL
        COMMENT '5.3.02 Gasto aporte patronal IESS (DR al provisionar nómina)',
    ADD COLUMN IF NOT EXISTS acc_benefits_expense INT UNSIGNED DEFAULT NULL
        COMMENT '5.3.03-06 Gasto beneficios de ley: décimos, fondos reserva, vacaciones (DR)',
    -- CRÉDITOS (Pasivos)
    ADD COLUMN IF NOT EXISTS acc_salary_payable   INT UNSIGNED DEFAULT NULL
        COMMENT '2.1.05.01 Sueldos y salarios por pagar (CR neto a pagar)',
    ADD COLUMN IF NOT EXISTS acc_iess_payable     INT UNSIGNED DEFAULT NULL
        COMMENT '2.1.04.01/02 IESS por pagar (empleado + patronal)',
    ADD COLUMN IF NOT EXISTS acc_benefits_payable INT UNSIGNED DEFAULT NULL
        COMMENT '2.1.05.02-04 Beneficios de ley por pagar (décimos, fondos, vacaciones)';

-- FKs separadas (ADD CONSTRAINT no soporta IF NOT EXISTS)
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_sal_exp  FOREIGN KEY (acc_salary_expense)   REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_iess_exp FOREIGN KEY (acc_iess_expense)     REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_ben_exp  FOREIGN KEY (acc_benefits_expense) REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_sal_pay  FOREIGN KEY (acc_salary_payable)   REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_iess_pay FOREIGN KEY (acc_iess_payable)     REFERENCES chart_of_accounts(id) ON DELETE SET NULL;
ALTER TABLE company_account_map
    ADD CONSTRAINT fk_cam_ben_pay  FOREIGN KEY (acc_benefits_payable) REFERENCES chart_of_accounts(id) ON DELETE SET NULL;

SET FOREIGN_KEY_CHECKS = 1;

-- ─────────────────────────────────────────────────────────────
-- FIN — ALTER company_account_map:
--   acc_salary_expense, acc_iess_expense, acc_benefits_expense
--   acc_salary_payable, acc_iess_payable, acc_benefits_payable
-- ─────────────────────────────────────────────────────────────
