The Four States
Orders progress through these states in sequence:Borrador
Draft - Initial state when branch creates an order
bg-gray-100 text-gray-700 border-gray-300)Characteristics:- Editable by branch user
- Deletable by branch user
- Not visible in admin approval queue
- Auto-saved every 30 seconds
- Not yet submitted for review
- Edit quantities and materials
- Change delivery date/type
- Save changes
- Delete entirely
- Submit to move to next state
Enviado
Visual Indicator:Badge color: Amber (
bg-amber-50 text-amber-700 border-amber-300)Characteristics:- Read-only for branch users
- Visible in admin dashboard
- Timestamp recorded in
enviado_at - User recorded in
enviado_por - Awaiting admin approval
- Branch: View only, export PDF
- Admin: Edit, approve, or reject
This is the primary review state. Admins validate quantities and delivery feasibility before approval.
Aprobado
Approved - Admin has validated and approved the order
bg-emerald-50 text-emerald-700 border-emerald-300)Characteristics:- Admin-approved for fulfillment
- Ready for warehouse picking
- Fulfillment format available
- Read-only for branch users
- Admin can still edit if needed
- Branch: View and export PDF
- Admin: Print format, mark as printed, edit if critical changes needed
Impreso
Printed - Fulfillment format printed, order in progress
bg-blue-50 text-blue-700 border-blue-300)Characteristics:- Warehouse has printed picking list
- Order fulfillment in progress
- Final state in the system
- Historical record complete
- Branch: View final details
- Admin: View, track completion, edit if emergency changes required
State Transition Diagram
State Constants
Fromsrc/lib/constants.ts:49-61:
Permission Matrix
Who can do what in each state:- Borrador
- Enviado
- Aprobado
- Impreso
| Action | Branch User | Admin |
|---|---|---|
| View | ✅ Owner only | ✅ All |
| Edit | ✅ Owner only | ✅ All |
| Delete | ✅ Owner only | ❌ No |
| Save | ✅ Owner only | ✅ All |
| Submit | ✅ Owner only | ❌ No |
| Approve | ❌ No | ❌ No |
| ❌ No | ❌ No |
Database Schema
Fromsupabase/schema.sql:45-56, the estado field:
estado: Current state (enum, defaults to ‘borrador’)enviado_at: Timestamp when moved to ‘enviado’enviado_por: User ID who submitted the order
Row Level Security (RLS)
Fromsupabase/schema.sql:132-137, update permissions:
Branch Users
Branch users can only update orders in borrador state from their own branch.
Administrators
Admins can update orders in any state without restrictions.
Typical Order Lifecycle Timeline
Day 1: Creation
9:00 AM - Branch user starts new order (borrador)9:30 AM - User adds materials from catalog10:15 AM - User saves draft, continues later3:00 PM - User finalizes quantities and clicks “Enviar pedido”Estado: borrador → enviado
Day 2: Review
8:00 AM - Admin sees order in dashboard “Total Enviados” count8:30 AM - Admin reviews materials against inventory9:00 AM - Admin clicks “Aprobar” buttonEstado: enviado → aprobado
Day 3: Fulfillment
7:00 AM - Admin exports PDF fulfillment format7:15 AM - Admin clicks “Imprimir” to mark as printed8:00 AM - Warehouse begins picking materialsEstado: aprobado → impreso
Special Cases
Editing After Submission
If an admin needs to modify a submitted order:Deleting Orders
Fromsrc/pages/MisPedidos.tsx:34-52:
Only borrador orders show the delete button. Once submitted (enviado), orders cannot be deleted.
Best Practices
For Branch Users
For Branch Users
- Save frequently as borrador while building orders
- Review carefully before submitting - you can’t edit after enviado
- Check weight limits before submission
- Monitor state changes in Mis Pedidos to track approval progress
For Administrators
For Administrators
- Review enviado orders daily - don’t let them accumulate
- Approve promptly to give warehouse adequate prep time
- Print formats early for next-day deliveries
- Communicate edits if you modify submitted orders
- Use filters to manage high-volume periods (filter by estado)
For System Integrators
For System Integrators
- Respect RLS policies - don’t bypass security
- Set enviado_at and enviado_por when transitioning to enviado
- Validate state transitions - follow the allowed flow
- Use estado_pedido enum - don’t use string literals
Monitoring Order Flow
Admins can track order flow health using dashboard metrics:High Enviados Count
Problem: Backlog building upAction: Prioritize reviews, add admin capacity
Low Aprobados Count
Status: Healthy flowMeaning: Orders moving through system efficiently
Many Printed Today
Status: Active fulfillmentMeaning: Warehouse busy with current orders
High Weekly Tonnage
Alert: Capacity concernAction: Plan additional truck capacity