-- ============================================================
-- quoERP — Módulo de Compras
-- Migración: 005_purchases_module.sql
-- Versión: 1.0.0 | Fecha: 2026-06-05
-- ============================================================
-- Requiere: 001, 003, 004
-- ============================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ─────────────────────────────────────────────────────────────
-- AJUSTE 1: suppliers — campos adicionales para gestión completa
-- ─────────────────────────────────────────────────────────────
ALTER TABLE suppliers
    ADD COLUMN IF NOT EXISTS city         VARCHAR(80)   DEFAULT NULL AFTER address,
    ADD COLUMN IF NOT EXISTS website      VARCHAR(150)  DEFAULT NULL AFTER email,
    ADD COLUMN IF NOT EXISTS notes        TEXT          DEFAULT NULL
        COMMENT 'Notas internas sobre el proveedor',
    ADD COLUMN IF NOT EXISTS bank_name    VARCHAR(80)   DEFAULT NULL COMMENT 'Banco para pagos',
    ADD COLUMN IF NOT EXISTS bank_account VARCHAR(30)   DEFAULT NULL COMMENT 'N° cuenta bancaria',
    ADD COLUMN IF NOT EXISTS bank_type    ENUM('corriente','ahorros') DEFAULT NULL,
    ADD COLUMN IF NOT EXISTS credit_limit DECIMAL(12,2) DEFAULT 0
        COMMENT 'Límite de crédito otorgado por el proveedor',
    ADD COLUMN IF NOT EXISTS updated_at   DATETIME      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;


-- ─────────────────────────────────────────────────────────────
-- AJUSTE 2: purchase_orders — campos adicionales
-- ─────────────────────────────────────────────────────────────
ALTER TABLE purchase_orders
    ADD COLUMN IF NOT EXISTS warehouse_id     INT UNSIGNED  DEFAULT NULL
        COMMENT 'Bodega destino por defecto al recibir',
    ADD COLUMN IF NOT EXISTS invoice_number   VARCHAR(50)   DEFAULT NULL
        COMMENT 'N° factura del proveedor al recibir',
    ADD COLUMN IF NOT EXISTS received_at      DATETIME      DEFAULT NULL
        COMMENT 'Fecha/hora de recepción completa',
    ADD COLUMN IF NOT EXISTS received_by      INT UNSIGNED  DEFAULT NULL;


SET FOREIGN_KEY_CHECKS = 1;

-- ─────────────────────────────────────────────────────────────
-- FIN DE MIGRACIÓN
-- ALTER TABLE suppliers: city, website, notes, bank_*, credit_limit, updated_at
-- ALTER TABLE purchase_orders: warehouse_id, invoice_number, received_at, received_by
-- ─────────────────────────────────────────────────────────────
