-- SIGDA: pagos de compras + cuentas por pagar
-- Ejecutar una sola vez después de la migración de documento de compra.

ALTER TABLE compras
    MODIFY COLUMN condicion_pago ENUM('CONTADO','CREDITO','MIXTO') NOT NULL DEFAULT 'CONTADO';

ALTER TABLE caja_movimientos
    ADD COLUMN IF NOT EXISTS id_compra BIGINT UNSIGNED NULL AFTER id_operacion;

ALTER TABLE caja_movimientos
    ADD CONSTRAINT fk_cm_compra
    FOREIGN KEY (id_compra) REFERENCES compras(id_compra)
    ON UPDATE CASCADE
    ON DELETE SET NULL;

CREATE TABLE IF NOT EXISTS compras_pagos (
    id_pago BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_compra BIGINT UNSIGNED NOT NULL,
    metodo_pago ENUM('EFECTIVO','YAPE','TRANSFERENCIA','OTRO') NOT NULL,
    monto DECIMAL(12,2) NOT NULL,
    referencia VARCHAR(150) NULL,
    fecha_pago DATETIME NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_cp_compra
        FOREIGN KEY (id_compra) REFERENCES compras(id_compra)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS cuentas_por_pagar (
    id_cuenta_por_pagar BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_compra BIGINT UNSIGNED NOT NULL,
    id_proveedor BIGINT UNSIGNED NOT NULL,
    fecha_emision DATE NOT NULL,
    total DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    total_pagado DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    saldo DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    estado ENUM('PENDIENTE','PARCIAL','PAGADA','ANULADA') NOT NULL DEFAULT 'PENDIENTE',
    observacion VARCHAR(255) NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_cxp_compra (id_compra),
    CONSTRAINT fk_cxp_compra
        FOREIGN KEY (id_compra) REFERENCES compras(id_compra)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    CONSTRAINT fk_cxp_proveedor
        FOREIGN KEY (id_proveedor) REFERENCES proveedores(id_proveedor)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS cuentas_por_pagar_cuotas (
    id_cuota BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_cuenta_por_pagar BIGINT UNSIGNED NOT NULL,
    numero_cuota INT NOT NULL,
    fecha_vencimiento DATE NOT NULL,
    monto DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    monto_pagado DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    saldo DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    estado ENUM('PENDIENTE','PARCIAL','PAGADA','ANULADA') NOT NULL DEFAULT 'PENDIENTE',
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_cxp_cuota (id_cuenta_por_pagar, numero_cuota),
    CONSTRAINT fk_cuota_cxp
        FOREIGN KEY (id_cuenta_por_pagar) REFERENCES cuentas_por_pagar(id_cuenta_por_pagar)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS cuentas_por_pagar_pagos (
    id_pago BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_cuenta_por_pagar BIGINT UNSIGNED NOT NULL,
    id_cuota BIGINT UNSIGNED NULL,
    metodo_pago ENUM('EFECTIVO','YAPE','TRANSFERENCIA','OTRO') NOT NULL,
    monto DECIMAL(12,2) NOT NULL,
    referencia VARCHAR(150) NULL,
    fecha_pago DATETIME NOT NULL,
    id_usuario BIGINT UNSIGNED NOT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_cxpp_cxp
        FOREIGN KEY (id_cuenta_por_pagar) REFERENCES cuentas_por_pagar(id_cuenta_por_pagar)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    CONSTRAINT fk_cxpp_cuota
        FOREIGN KEY (id_cuota) REFERENCES cuentas_por_pagar_cuotas(id_cuota)
        ON UPDATE CASCADE
        ON DELETE SET NULL,
    CONSTRAINT fk_cxpp_usuario
        FOREIGN KEY (id_usuario) REFERENCES usuarios(id_usuario)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
