fix(erp): enable erp pages and menu items

This commit is contained in:
Erik Silva
2025-12-29 17:23:59 -03:00
parent e124a64a5d
commit adbff9bb1e
13990 changed files with 1110936 additions and 59 deletions

View File

@@ -0,0 +1,93 @@
-- Migration: 025_create_erp_tables.sql
-- Description: Create tables for Finance, Inventory, and Order management
-- Financial Categories
CREATE TABLE IF NOT EXISTS erp_financial_categories (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
type VARCHAR(20) NOT NULL CHECK (type IN ('income', 'expense')),
color VARCHAR(20),
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Bank Accounts
CREATE TABLE IF NOT EXISTS erp_bank_accounts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
bank_name VARCHAR(255),
initial_balance DECIMAL(15,2) DEFAULT 0.00,
current_balance DECIMAL(15,2) DEFAULT 0.00,
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Financial Transactions
CREATE TABLE IF NOT EXISTS erp_financial_transactions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
account_id UUID REFERENCES erp_bank_accounts(id),
category_id UUID REFERENCES erp_financial_categories(id),
description TEXT,
amount DECIMAL(15,2) NOT NULL,
type VARCHAR(20) NOT NULL CHECK (type IN ('income', 'expense')),
status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'paid', 'cancelled')),
due_date DATE,
payment_date TIMESTAMP WITH TIME ZONE,
attachments TEXT[], -- URLs for proofs
created_by UUID REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Products & Services
CREATE TABLE IF NOT EXISTS erp_products (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
name VARCHAR(255) NOT NULL,
sku VARCHAR(100),
description TEXT,
price DECIMAL(15,2) NOT NULL,
cost_price DECIMAL(15,2),
type VARCHAR(20) DEFAULT 'product' CHECK (type IN ('product', 'service')),
stock_quantity INT DEFAULT 0,
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Orders
CREATE TABLE IF NOT EXISTS erp_orders (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
customer_id UUID REFERENCES companies(id), -- Linked to CRM (companies)
status VARCHAR(20) NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'confirmed', 'completed', 'cancelled')),
total_amount DECIMAL(15,2) DEFAULT 0.00,
notes TEXT,
created_by UUID REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Order Items
CREATE TABLE IF NOT EXISTS erp_order_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
order_id UUID NOT NULL REFERENCES erp_orders(id) ON DELETE CASCADE,
product_id UUID NOT NULL REFERENCES erp_products(id),
quantity INT NOT NULL DEFAULT 1,
unit_price DECIMAL(15,2) NOT NULL,
total_price DECIMAL(15,2) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Indexes for performance and multi-tenancy
CREATE INDEX idx_erp_fin_cat_tenant ON erp_financial_categories(tenant_id);
CREATE INDEX idx_erp_bank_acc_tenant ON erp_bank_accounts(tenant_id);
CREATE INDEX idx_erp_fin_trans_tenant ON erp_financial_transactions(tenant_id);
CREATE INDEX idx_erp_products_tenant ON erp_products(tenant_id);
CREATE INDEX idx_erp_orders_tenant ON erp_orders(tenant_id);
CREATE INDEX idx_erp_order_items_order ON erp_order_items(order_id);

View File

@@ -0,0 +1,32 @@
-- 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);

View File

@@ -0,0 +1,4 @@
-- Migration: 027_add_payment_method_to_transactions.sql
-- Description: Add payment_method field to financial transactions
ALTER TABLE erp_financial_transactions ADD COLUMN IF NOT EXISTS payment_method VARCHAR(50);

View File

@@ -0,0 +1,5 @@
-- Migration: 028_add_crm_links_to_transactions.sql
-- Description: Add fields to link financial transactions to CRM Customers and Companies
ALTER TABLE erp_financial_transactions ADD COLUMN IF NOT EXISTS crm_customer_id UUID REFERENCES crm_customers(id) ON DELETE SET NULL;
ALTER TABLE erp_financial_transactions ADD COLUMN IF NOT EXISTS company_id UUID REFERENCES companies(id) ON DELETE SET NULL;

View File

@@ -0,0 +1,15 @@
-- Migration: 029_create_documents_table.sql
-- Description: Create table for text documents (Google Docs style)
CREATE TABLE IF NOT EXISTS documents (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
title VARCHAR(255) NOT NULL,
content TEXT,
status VARCHAR(50) DEFAULT 'draft',
created_by UUID REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_documents_tenant_id ON documents(tenant_id);

View File

@@ -0,0 +1,22 @@
-- Migration: 030_add_subpages_and_activities_to_documents.sql
-- Description: Add parent_id for subpages and tracking columns (Fixed)
ALTER TABLE documents
ADD COLUMN IF NOT EXISTS parent_id UUID REFERENCES documents(id) ON DELETE CASCADE,
ADD COLUMN IF NOT EXISTS last_updated_by UUID REFERENCES users(id),
ADD COLUMN IF NOT EXISTS version INTEGER DEFAULT 1;
CREATE INDEX IF NOT EXISTS idx_documents_parent_id ON documents(parent_id);
-- Simple activity log table
CREATE TABLE IF NOT EXISTS document_activities (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id),
action VARCHAR(50) NOT NULL, -- 'created', 'updated', 'deleted', 'status_change'
description TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_doc_activities_doc_id ON document_activities(document_id);