33 lines
1.4 KiB
SQL
33 lines
1.4 KiB
SQL
-- Migration: 026_create_erp_entities.sql
|
|
-- Description: Create tables for Customers and Suppliers in ERP
|
|
|
|
-- ERP Entities (Customers and Suppliers)
|
|
CREATE TABLE IF NOT EXISTS erp_entities (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
|
|
name VARCHAR(255) NOT NULL,
|
|
document VARCHAR(20), -- CPF/CNPJ
|
|
email VARCHAR(255),
|
|
phone VARCHAR(20),
|
|
type VARCHAR(20) NOT NULL CHECK (type IN ('customer', 'supplier', 'both')),
|
|
status VARCHAR(20) DEFAULT 'active',
|
|
address TEXT,
|
|
city VARCHAR(100),
|
|
state VARCHAR(2),
|
|
zip VARCHAR(10),
|
|
notes TEXT,
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Update Financial Transactions to link with Entities
|
|
ALTER TABLE erp_financial_transactions ADD COLUMN IF NOT EXISTS entity_id UUID REFERENCES erp_entities(id);
|
|
|
|
-- Update Orders to link with Entities instead of companies (optional but more consistent for ERP)
|
|
-- Keep customer_id for now to avoid breaking existing logic, but allow entity_id
|
|
ALTER TABLE erp_orders ADD COLUMN IF NOT EXISTS entity_id UUID REFERENCES erp_entities(id);
|
|
|
|
-- Indexes
|
|
CREATE INDEX IF NOT EXISTS idx_erp_entities_tenant ON erp_entities(tenant_id);
|
|
CREATE INDEX IF NOT EXISTS idx_erp_entities_type ON erp_entities(type);
|