User Management Tables
rol - User Roles
Defines the role hierarchy for the system.
Unique role identifier
Role name (Director, Madre Procesadora, Supervisor, Desarrollador)
Role description and permissions summary
id_rol=1: Director - School system administratorid_rol=2: Madre Procesadora - Kitchen and inventory operationsid_rol=3: Supervisor - Read-only supervisionid_rol=4: Desarrollador - Technical administrator (assigned from DB only)
users - User Accounts
Extends Supabase auth.users with application-specific fields.
User ID (references
auth.users.id)Foreign Key: auth.users(id) ON DELETE CASCADEUnique username for login
User’s full name
User role (references
rol.id_rol)Default: 2 (Madre Procesadora)Account active statusDefault:
TRUELast activity timestamp
Last seen (heartbeat) timestamp
Last known IP address
Account creation timestampDefault:
NOW()trigger_protect_director- Prevents unauthorized modifications to Director accountstrigger_protect_director_insert- Validates role assignments on creation
Inventory Management Tables
category - Product Categories
Unique category identifier
Category name
Category description
- Lacteos (Dairy)
- Proteinas (Proteins)
- Carbohidratos (Carbohydrates)
- Legumbres (Legumes)
- Vegetales (Vegetables)
- Frutas (Fruits)
- Aceites y grasas (Oils and fats)
- Condimentos (Condiments)
- Otros (Others)
product - Product Catalog
Master product catalog with current stock levels.
Unique product identifier
Product name
Current stock quantityDefault:
0Constraint: CHECK (stock >= 0)Unit of measurement (kg, lt, unidades)
Product description or notes
Product category (references
category.id_category)Record creation timestampDefault:
NOW()Last update timestampDefault:
NOW()update_product_updated_at- Automatically updatesupdated_aton modificationsaudit_product- Logs all INSERT/UPDATE/DELETE operations
guia_entrada - Delivery Guides
Delivery guides with maker-checker approval workflow.
Unique guide identifier
SUNAGRO guide number (unique)Constraint:
UNIQUEOptional SISECAL guide number
Delivery dateDefault:
CURRENT_DATECommunity representative name
Representative phone number
Additional notes
Approval status: Pendiente, Aprobada, RechazadaDefault:
'Pendiente'Constraint: CHECK (estado IN ('Pendiente', 'Aprobada', 'Rechazada'))User who approved/rejected (references
users.id_user)Approval/rejection timestamp
Approval/rejection comments
User who created the guide (references
users.id_user)Creation timestampDefault:
NOW()idx_guia_entrada_estado- Fast filtering by status
audit_guia- Logs all operations
input - Delivery Guide Line Items
Individual products within delivery guides, with batch/lot tracking.
Unique input identifier
Parent delivery guide (references
guia_entrada.id_guia)Foreign Key: ON DELETE CASCADEProduct (references
product.id_product)Quantity receivedConstraint:
CHECK (amount > 0)Quantity in bulk units (if applicable)
Batch/lot details with expiration datesFormat:
[{"cantidad": 50.00, "fecha_vencimiento": "2026-06-30"}]Input dateDefault:
CURRENT_DATECreation timestampDefault:
NOW()idx_input_lotes_detalle- GIN index for JSONB queries
output - Stock Withdrawals
Records all inventory withdrawals and stock deductions.
Unique output identifier
Product withdrawn (references
product.id_product)Quantity withdrawnConstraint:
CHECK (amount > 0)Withdrawal dateDefault:
CURRENT_DATEReason for withdrawal
Related menu (references
menu_diario.id_menu) - LEGACYRelated daily record (references
registro_diario.id_registro)User who created the withdrawal (references
auth.users.id)Creation timestampDefault:
NOW()trigger_update_stock_output- Automatically deducts stock fromproducttableaudit_output- Logs all operations
Daily Operations Tables
registro_diario - Daily Meal Records
Unified daily meal operations (replaces legacy attendance + menu tables).
Unique record identifier
Operation date
Meal shift: Desayuno, Almuerzo, MeriendaConstraint:
CHECK (turno IN ('Desayuno', 'Almuerzo', 'Merienda'))Total student attendanceConstraint:
CHECK (asistencia_total >= 0)Additional notes
User who created the record (references
auth.users.id)Creation timestampDefault:
NOW()UNIQUE(fecha, turno)- One record per date/shift combination
receta_porcion - Product Yield Configuration
Defines portion yield per unit for meal planning calculations.
Unique portion configuration identifier
Product (references
product.id_product)Constraint: One configuration per productPortions per unit (e.g., 1 kg rice = 12 portions)Default:
1.0Unit of measurement
Additional notes or preparation instructions
Creation timestampDefault:
NOW()- 1 kg arroz → 12 porciones
- 1 kg leche en polvo → 40 porciones
- 1 kg pollo → 4 porciones
Legacy Tables
asistencia_diaria - Student Attendance (LEGACY)
Unique attendance record identifier
Attendance date
Total students presentConstraint:
CHECK (total_alumnos >= 0)Notes
User who created the record (references
auth.users.id)Creation timestampDefault:
NOW()menu_diario - Daily Menu (LEGACY)
Unique menu identifier
Menu date
Related attendance (references
asistencia_diaria.id_asistencia)Menu notes
Menu confirmation statusDefault:
FALSEUser who created the menu (references
auth.users.id)Creation timestampDefault:
NOW()audit_menu- Logs all operations
menu_detalle - Menu Line Items (LEGACY)
Unique detail identifier
Parent menu (references
menu_diario.id_menu)Foreign Key: ON DELETE CASCADEProduct (references
product.id_product)Planned quantity
Actually used quantity
Creation timestampDefault:
NOW()Audit & Logging
audit_log - System Audit Trail
Comprehensive audit trail for all critical operations.
Unique log entry identifier
User who performed the action (references
auth.users.id)Action type: INSERT, UPDATE, DELETE, APPROVE, REJECT
Table name where action occurred
ID of affected record
JSON details of the operation (old/new values)
Operation timestampDefault:
NOW()User IP address
log_audit()trigger function- RPC functions (
aprobar_guia,rechazar_guia, etc.)
Entity Relationships
Key Foreign Key Relationships
Sample Data
The schema includes default seed data:- 4 roles: Director, Madre Procesadora, Supervisor, Desarrollador
- 9 categories: Lacteos, Proteinas, Carbohidratos, etc.
- 8 sample products: Arroz, Aceite, Leche, Pollo, etc.
- 5 portion yields: Configured for common products
supabase_schema.sql lines 870-888 for complete seed data.