erp-construccion-database-v2/seeds/dev/03-empresa-tenant.sql
Adrian Flores Cortes 8df74f73d2 [TASK-2026-02-03-SEEDS] feat: Add complete development seeds for demo
Seeds include:
- 01-core-catalogs.sql: Units, currencies, taxes, banks, states (~100 records)
- 02-users-profiles.sql: 12 roles, 50+ permissions, 12 demo users
- 03-empresa-tenant.sql: Demo company with 2 branches, 4 warehouses
- 04-proyectos-obras.sql: 3 projects, stages, lots, apartments
- 05-presupuestos.sql: CMIC-based concepts, 5 budgets
- 06-estimaciones.sql: Contracts, estimates, advances
- 07-avances-calidad.sql: Progress, logbook, quality checklists
- 08-hse.sql: PPE catalog, training, incidents
- 09-finanzas.sql: Chart of accounts, CxP, CxC, balanced entries
- 10-activos-documentos.sql: Assets, maintenance, documents

Total: ~940 records for complete demo coverage

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 11:08:26 -06:00

295 lines
17 KiB
PL/PgSQL

-- ═══════════════════════════════════════════════════════════════════════════════
-- SEED 03: EMPRESA Y ESTRUCTURA ORGANIZACIONAL
-- ═══════════════════════════════════════════════════════════════════════════════
-- Proyecto: ERP Construcción
-- Autor: Claude Opus 4.5
-- Fecha: 2026-02-03
-- Descripción: Tenant demo, sucursales, almacenes, configuración
-- Dependencias: 01-core-catalogs.sql, 02-users-profiles.sql
-- ═══════════════════════════════════════════════════════════════════════════════
-- UUIDs determinísticos:
-- Prefijo: 00000000-0000-0000-0003-XXXXXXXXXXXX para empresa/tenant
BEGIN;
-- ─────────────────────────────────────────────────────────────────────────────────
-- 1. TENANT (EMPRESA) PRINCIPAL
-- ─────────────────────────────────────────────────────────────────────────────────
INSERT INTO tenants.tenants (
id, code, name, legal_name, rfc,
business_type, status,
address_street, address_city, address_state, address_zip, address_country,
phone, email, website,
logo_url, primary_color, secondary_color,
fiscal_regime, cfdi_certificate_number,
is_active, created_at, created_by
) VALUES (
'00000000-0000-0000-0003-000000000001',
'DEMO001',
'Constructora Demo',
'Constructora Demo S.A. de C.V.',
'CDM010101ABC',
'construccion_residencial',
'active',
'Av. Tecnológico 1500, Parque Industrial',
'Aguascalientes',
'Aguascalientes',
'20290',
'México',
'+52 449 123 4567',
'contacto@constructorademo.com',
'https://www.constructorademo.com',
'/assets/logos/demo-logo.png',
'#1E40AF', -- Azul corporativo
'#F59E0B', -- Amarillo construcción
'601', -- Régimen General de Ley PM
'CSD00001234567890',
true,
NOW(),
'00000000-0000-0000-0002-000000000101' -- admin
)
ON CONFLICT (id) DO NOTHING;
-- ─────────────────────────────────────────────────────────────────────────────────
-- 2. CONFIGURACIÓN DEL TENANT
-- ─────────────────────────────────────────────────────────────────────────────────
INSERT INTO tenants.tenant_settings (id, tenant_id, setting_key, setting_value, setting_type, created_at) VALUES
-- General
('00000000-0000-0000-0003-000000000101', '00000000-0000-0000-0003-000000000001',
'timezone', 'America/Mexico_City', 'string', NOW()),
('00000000-0000-0000-0003-000000000102', '00000000-0000-0000-0003-000000000001',
'locale', 'es-MX', 'string', NOW()),
('00000000-0000-0000-0003-000000000103', '00000000-0000-0000-0003-000000000001',
'currency', 'MXN', 'string', NOW()),
('00000000-0000-0000-0003-000000000104', '00000000-0000-0000-0003-000000000001',
'date_format', 'DD/MM/YYYY', 'string', NOW()),
-- Construcción
('00000000-0000-0000-0003-000000000110', '00000000-0000-0000-0003-000000000001',
'default_retention_percentage', '5', 'number', NOW()),
('00000000-0000-0000-0003-000000000111', '00000000-0000-0000-0003-000000000001',
'default_advance_percentage', '30', 'number', NOW()),
('00000000-0000-0000-0003-000000000112', '00000000-0000-0000-0003-000000000001',
'estimate_period_days', '15', 'number', NOW()),
('00000000-0000-0000-0003-000000000113', '00000000-0000-0000-0003-000000000001',
'require_photos_for_advances', 'true', 'boolean', NOW()),
-- HSE
('00000000-0000-0000-0003-000000000120', '00000000-0000-0000-0003-000000000001',
'hse_incident_auto_notify', 'true', 'boolean', NOW()),
('00000000-0000-0000-0003-000000000121', '00000000-0000-0000-0003-000000000001',
'hse_require_daily_checkin', 'true', 'boolean', NOW()),
-- Finanzas
('00000000-0000-0000-0003-000000000130', '00000000-0000-0000-0003-000000000001',
'fiscal_year_start_month', '1', 'number', NOW()),
('00000000-0000-0000-0003-000000000131', '00000000-0000-0000-0003-000000000001',
'default_payment_terms_days', '30', 'number', NOW()),
-- Notificaciones
('00000000-0000-0000-0003-000000000140', '00000000-0000-0000-0003-000000000001',
'email_notifications_enabled', 'true', 'boolean', NOW()),
('00000000-0000-0000-0003-000000000141', '00000000-0000-0000-0003-000000000001',
'sms_notifications_enabled', 'false', 'boolean', NOW())
ON CONFLICT (id) DO NOTHING;
-- ─────────────────────────────────────────────────────────────────────────────────
-- 3. SUCURSALES / BRANCHES
-- ─────────────────────────────────────────────────────────────────────────────────
INSERT INTO tenants.branches (
id, tenant_id, code, name, branch_type,
address_street, address_city, address_state, address_zip,
phone, email,
manager_id,
is_headquarters, is_active, created_at
) VALUES
-- Matriz
('00000000-0000-0000-0003-000000000201', '00000000-0000-0000-0003-000000000001',
'MATRIZ', 'Oficinas Corporativas', 'headquarters',
'Av. Tecnológico 1500, Parque Industrial', 'Aguascalientes', 'Aguascalientes', '20290',
'+52 449 123 4567', 'matriz@constructorademo.com',
'00000000-0000-0000-0002-000000000102', -- director
true, true, NOW()),
-- Sucursal Obra Norte
('00000000-0000-0000-0003-000000000202', '00000000-0000-0000-0003-000000000001',
'OBRA-NORTE', 'Oficina de Obra - Zona Norte', 'field_office',
'Carr. Panamericana Km 15', 'Aguascalientes', 'Aguascalientes', '20340',
'+52 449 234 5678', 'obranorte@constructorademo.com',
'00000000-0000-0000-0002-000000000103', -- gerente1
false, true, NOW())
ON CONFLICT (id) DO NOTHING;
-- ─────────────────────────────────────────────────────────────────────────────────
-- 4. ALMACENES
-- ─────────────────────────────────────────────────────────────────────────────────
INSERT INTO inventory.warehouses (
id, tenant_id, branch_id, code, name, warehouse_type,
address_street, address_city, address_state,
manager_id,
is_default, is_active, created_at
) VALUES
-- Almacén Central (Matriz)
('00000000-0000-0000-0003-000000000301', '00000000-0000-0000-0003-000000000001',
'00000000-0000-0000-0003-000000000201',
'ALM-CENTRAL', 'Almacén Central', 'central',
'Av. Tecnológico 1500', 'Aguascalientes', 'Aguascalientes',
'00000000-0000-0000-0002-000000000106', -- almacenista
true, true, NOW()),
-- Almacén de Obra (Palmas)
('00000000-0000-0000-0003-000000000302', '00000000-0000-0000-0003-000000000001',
'00000000-0000-0000-0003-000000000202',
'ALM-PALMAS', 'Almacén Residencial Las Palmas', 'field',
'Dentro del fraccionamiento Las Palmas', 'Aguascalientes', 'Aguascalientes',
NULL,
false, true, NOW()),
-- Almacén de Obra (Diamante)
('00000000-0000-0000-0003-000000000303', '00000000-0000-0000-0003-000000000001',
'00000000-0000-0000-0003-000000000202',
'ALM-DIAMANTE', 'Almacén Torre Diamante', 'field',
'Torre Diamante, Zona Centro', 'Aguascalientes', 'Aguascalientes',
NULL,
false, true, NOW()),
-- Almacén de Tránsito
('00000000-0000-0000-0003-000000000304', '00000000-0000-0000-0003-000000000001',
'00000000-0000-0000-0003-000000000201',
'ALM-TRANSITO', 'Almacén de Tránsito', 'transit',
'Av. Tecnológico 1500', 'Aguascalientes', 'Aguascalientes',
NULL,
false, true, NOW())
ON CONFLICT (id) DO NOTHING;
-- ─────────────────────────────────────────────────────────────────────────────────
-- 5. ASIGNAR USUARIOS AL TENANT
-- ─────────────────────────────────────────────────────────────────────────────────
INSERT INTO auth.user_tenants (user_id, tenant_id, is_default, is_active, created_at) VALUES
('00000000-0000-0000-0002-000000000101', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000102', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000103', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000104', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000105', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000106', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000107', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000108', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000109', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000110', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000111', '00000000-0000-0000-0003-000000000001', true, true, NOW()),
('00000000-0000-0000-0002-000000000112', '00000000-0000-0000-0003-000000000001', true, true, NOW())
ON CONFLICT DO NOTHING;
-- ─────────────────────────────────────────────────────────────────────────────────
-- 6. PERFILES DE USUARIO (Datos adicionales)
-- ─────────────────────────────────────────────────────────────────────────────────
INSERT INTO profiles.user_profiles (
id, user_id, tenant_id,
phone, mobile, job_title, department,
avatar_url, bio,
preferences,
created_at
) VALUES
('00000000-0000-0000-0003-000000000401', '00000000-0000-0000-0002-000000000101', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0001', '+52 449 200 0001', 'Administrador del Sistema', 'TI',
'/avatars/admin.png', 'Administrador general del sistema ERP',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000402', '00000000-0000-0000-0002-000000000102', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0002', '+52 449 200 0002', 'Director General', 'Dirección',
'/avatars/director.png', 'Director General de Constructora Demo',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000403', '00000000-0000-0000-0002-000000000103', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0003', '+52 449 200 0003', 'Gerente de Proyectos', 'Construcción',
'/avatars/gerente.png', 'Gerente de proyectos con 15 años de experiencia',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000404', '00000000-0000-0000-0002-000000000104', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0004', '+52 449 200 0004', 'Residente de Obra', 'Construcción',
'/avatars/residente.png', 'Residente de obra certificado CMIC',
'{"theme": "dark", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000405', '00000000-0000-0000-0002-000000000105', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0005', '+52 449 200 0005', 'Supervisor de Campo', 'Construcción',
'/avatars/supervisor.png', 'Supervisor con experiencia en obra residencial',
'{"theme": "dark", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000406', '00000000-0000-0000-0002-000000000106', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0006', '+52 449 200 0006', 'Jefe de Almacén', 'Logística',
'/avatars/almacen.png', 'Responsable de control de inventarios',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000407', '00000000-0000-0000-0002-000000000107', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0007', '+52 449 200 0007', 'Contador General', 'Finanzas',
'/avatars/contador.png', 'CPA con especialidad en construcción',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000408', '00000000-0000-0000-0002-000000000108', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0008', '+52 449 200 0008', 'Coordinador de RRHH', 'Recursos Humanos',
'/avatars/rrhh.png', 'Especialista en gestión de personal de construcción',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000409', '00000000-0000-0000-0002-000000000109', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0009', '+52 449 200 0009', 'Coordinador HSE', 'Seguridad',
'/avatars/hse.png', 'Certificado STPS en seguridad industrial',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000410', '00000000-0000-0000-0002-000000000110', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0010', '+52 449 200 0010', 'Inspector de Calidad', 'Calidad',
'/avatars/calidad.png', 'Inspector certificado ISO 9001',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000411', '00000000-0000-0000-0002-000000000111', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0011', '+52 449 200 0011', 'Comprador', 'Compras',
'/avatars/compras.png', 'Especialista en compras de construcción',
'{"theme": "light", "notifications": true}'::jsonb, NOW()),
('00000000-0000-0000-0003-000000000412', '00000000-0000-0000-0002-000000000112', '00000000-0000-0000-0003-000000000001',
'+52 449 100 0012', '+52 449 200 0012', 'Cliente', 'Externo',
'/avatars/cliente.png', 'Cliente del fraccionamiento Las Palmas',
'{"theme": "light", "notifications": true}'::jsonb, NOW())
ON CONFLICT (id) DO NOTHING;
-- ─────────────────────────────────────────────────────────────────────────────────
-- VERIFICACIÓN
-- ─────────────────────────────────────────────────────────────────────────────────
DO $$
DECLARE
v_tenants INTEGER;
v_branches INTEGER;
v_warehouses INTEGER;
v_user_tenants INTEGER;
v_profiles INTEGER;
BEGIN
SELECT COUNT(*) INTO v_tenants FROM tenants.tenants;
SELECT COUNT(*) INTO v_branches FROM tenants.branches;
SELECT COUNT(*) INTO v_warehouses FROM inventory.warehouses;
SELECT COUNT(*) INTO v_user_tenants FROM auth.user_tenants;
SELECT COUNT(*) INTO v_profiles FROM profiles.user_profiles;
RAISE NOTICE '══════════════════════════════════════════════════════════════';
RAISE NOTICE 'SEED 03 - EMPRESA Y ESTRUCTURA - COMPLETADO';
RAISE NOTICE '══════════════════════════════════════════════════════════════';
RAISE NOTICE 'Tenants (empresas): %', v_tenants;
RAISE NOTICE 'Sucursales: %', v_branches;
RAISE NOTICE 'Almacenes: %', v_warehouses;
RAISE NOTICE 'Usuarios asignados a tenant: %', v_user_tenants;
RAISE NOTICE 'Perfiles de usuario: %', v_profiles;
RAISE NOTICE '══════════════════════════════════════════════════════════════';
END $$;
COMMIT;
-- ═══════════════════════════════════════════════════════════════════════════════
-- FIN SEED 03
-- ═══════════════════════════════════════════════════════════════════════════════