-- ============================================================================ -- FINANCIAL SCHEMA - Tabla: customers -- ============================================================================ -- Clientes de Stripe y datos de facturacion -- Vincula usuarios con su informacion de pago -- ============================================================================ CREATE TABLE IF NOT EXISTS financial.customers ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), -- Relacion con usuario user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, -- Stripe stripe_customer_id VARCHAR(100) UNIQUE, stripe_default_payment_method_id VARCHAR(100), -- Datos de facturacion billing_name VARCHAR(255), billing_email VARCHAR(255), billing_phone VARCHAR(50), -- Direccion de facturacion billing_address_line1 VARCHAR(255), billing_address_line2 VARCHAR(255), billing_city VARCHAR(100), billing_state VARCHAR(100), billing_postal_code VARCHAR(20), billing_country VARCHAR(2), -- ISO 3166-1 alpha-2 -- Datos fiscales (Mexico) tax_id VARCHAR(20), -- RFC tax_id_type VARCHAR(20) DEFAULT 'mx_rfc', -- Tipo de ID fiscal legal_name VARCHAR(255), -- Razon social -- Preferencias currency financial.currency_code NOT NULL DEFAULT 'USD', locale VARCHAR(10) DEFAULT 'en-US', -- Estado is_active BOOLEAN NOT NULL DEFAULT TRUE, delinquent BOOLEAN NOT NULL DEFAULT FALSE, delinquent_since TIMESTAMPTZ, -- Metadata metadata JSONB DEFAULT '{}', -- Timestamps created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), -- Constraints CONSTRAINT uq_customers_user UNIQUE(user_id), CONSTRAINT chk_valid_country CHECK (billing_country IS NULL OR LENGTH(billing_country) = 2) ); -- Indices CREATE INDEX idx_customers_user ON financial.customers(user_id); CREATE INDEX idx_customers_stripe ON financial.customers(stripe_customer_id) WHERE stripe_customer_id IS NOT NULL; CREATE INDEX idx_customers_email ON financial.customers(billing_email) WHERE billing_email IS NOT NULL; CREATE INDEX idx_customers_delinquent ON financial.customers(delinquent) WHERE delinquent = TRUE; CREATE INDEX idx_customers_tax_id ON financial.customers(tax_id) WHERE tax_id IS NOT NULL; -- Comentarios COMMENT ON TABLE financial.customers IS 'Clientes de Stripe con datos de facturacion'; COMMENT ON COLUMN financial.customers.stripe_customer_id IS 'ID del cliente en Stripe (cus_xxx)'; COMMENT ON COLUMN financial.customers.tax_id IS 'RFC para Mexico, VAT para EU, etc.'; COMMENT ON COLUMN financial.customers.delinquent IS 'True si tiene pagos vencidos';