# Base de Datos - ERP Retail/POS

## Resumen

| Aspecto | Valor |
|---------|-------|
| **Schema principal** | `retail` |
| **Tablas específicas** | 16 |
| **ENUMs** | 6 |
| **Hereda de ERP-Core** | 144 tablas (12 schemas) |

## Prerequisitos

1. **ERP-Core instalado** con todos sus schemas
2. **Extensiones PostgreSQL**: pg_trgm

## Orden de Ejecución DDL

```bash
# 1. Instalar ERP-Core primero
cd apps/erp-core/database
./scripts/reset-database.sh

# 2. Instalar extensión Retail
cd apps/verticales/retail/database
psql $DATABASE_URL -f init/00-extensions.sql
psql $DATABASE_URL -f init/01-create-schemas.sql
psql $DATABASE_URL -f init/02-rls-functions.sql
psql $DATABASE_URL -f init/03-retail-tables.sql
```

## Tablas Implementadas

### Schema: retail (16 tablas)

| Tabla | Módulo | Descripción |
|-------|--------|-------------|
| branches | RT-002 | Sucursales |
| cash_registers | RT-001 | Cajas registradoras |
| pos_sessions | RT-001 | Sesiones de POS |
| pos_orders | RT-001 | Ventas/Órdenes |
| pos_order_lines | RT-001 | Líneas de venta |
| pos_payments | RT-001 | Pagos (mixtos) |
| cash_movements | RT-001 | Entradas/salidas efectivo |
| branch_stock | RT-002 | Stock por sucursal |
| stock_transfers | RT-002 | Transferencias |
| stock_transfer_lines | RT-002 | Líneas de transferencia |
| product_barcodes | RT-003 | Códigos de barras |
| promotions | RT-003 | Promociones |
| promotion_products | RT-003 | Productos en promo |
| loyalty_programs | RT-004 | Programas fidelización |
| loyalty_cards | RT-004 | Tarjetas |
| loyalty_transactions | RT-004 | Transacciones puntos |

## ENUMs

| Enum | Valores |
|------|---------|
| pos_session_status | opening, open, closing, closed |
| pos_order_status | draft, paid, done, cancelled, refunded |
| payment_method | cash, card, transfer, credit, mixed |
| cash_movement_type | in, out |
| transfer_status | draft, pending, in_transit, received, cancelled |
| promotion_type | percentage, fixed_amount, buy_x_get_y, bundle |

## Row Level Security

Todas las tablas tienen RLS con:
```sql
tenant_id = current_setting('app.current_tenant_id', true)::UUID
```

## Consideraciones Especiales

- **Operación offline**: POS puede operar sin conexión
- **Rendimiento**: <100ms por transacción
- **Hardware**: Integración con impresoras y lectores
- **CFDI 4.0**: Facturación en tiempo real

## Referencias

- [HERENCIA-ERP-CORE.md](./HERENCIA-ERP-CORE.md)
- [DATABASE_INVENTORY.yml](../orchestration/inventarios/DATABASE_INVENTORY.yml)
