-- v2.0_multi_currency.sql
-- Mejora #8: Soporte Multi-moneda
--
-- Adds currency and exchange_rate columns to sales, sale_items,
-- quotations, quotation_items, and sale_payments.
--
-- Business rule: inventory prices are ALWAYS in MXN (base currency).
-- Sales can be recorded in USD (or other currencies) with conversion
-- at checkout time. Accounting and CFDI always use MXN.

-- sales
ALTER TABLE sales
    ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'MXN',
    ADD COLUMN IF NOT EXISTS exchange_rate NUMERIC(12,6) DEFAULT 1.0;

CREATE INDEX IF NOT EXISTS idx_sales_currency ON sales(currency);

-- sale_items
ALTER TABLE sale_items
    ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'MXN',
    ADD COLUMN IF NOT EXISTS exchange_rate NUMERIC(12,6) DEFAULT 1.0;

-- quotations
ALTER TABLE quotations
    ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'MXN',
    ADD COLUMN IF NOT EXISTS exchange_rate NUMERIC(12,6) DEFAULT 1.0;

-- quotation_items
ALTER TABLE quotation_items
    ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'MXN',
    ADD COLUMN IF NOT EXISTS exchange_rate NUMERIC(12,6) DEFAULT 1.0;

-- sale_payments
ALTER TABLE sale_payments
    ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'MXN',
    ADD COLUMN IF NOT EXISTS exchange_rate NUMERIC(12,6) DEFAULT 1.0;
