Overview
The PAE Inventory System uses FIFO (First-In-First-Out) batch tracking to ensure that the oldest products are used first, minimizing waste from expired items. Every product entry is tracked with detailed batch information including quantities and expiration dates. When inventory is consumed, the system automatically selects batches with the nearest expiration dates.What is FIFO?
FIFO = First In, First Out It means the first items added to inventory are the first items used. In food inventory management, this prevents:- Products expiring before they can be used
- Waste from spoilage
- Using newer items while older items sit unused
Example
You receive rice deliveries:| Date Received | Quantity | Expiration Date | Batch Status |
|---|---|---|---|
| Feb 1 | 50 kg | May 1 | ← Use this first (oldest) |
| Feb 15 | 30 kg | June 15 | ← Use this second |
| Mar 1 | 40 kg | July 1 | ← Use this last (newest) |
- Uses all 50 kg from the Feb 1 batch (expires soonest)
- Uses 10 kg from the Feb 15 batch
- Leaves 20 kg remaining in Feb 15 batch and all 40 kg in Mar 1 batch
How Batches Are Created
Batches are registered when creating entry guides (guías de entrada).Single Batch Entry
If all products in a delivery have the same expiration date:Multiple Batch Entry
If the same product has different expiration dates (mixed batches):Click '+ Agregar Lote' for each batch
Lote 1:
- Cantidad:
80 - Vencimiento:
2026-07-01
- Cantidad:
70 - Vencimiento:
2026-09-15
Validation (GuiasEntrada.jsx:159-177)
Batch Storage Format
Batches are stored as JSONB arrays in theinput.lotes_detalle column.
Database Structure (supabase_schema.sql:100-114)
JSONB Format Example
How FIFO Consumption Works
When you register a daily operation, the system:- Calculates quantity needed (attendance ÷ portion yield)
- Finds all batches for the product from approved entry guides
- Sorts by expiration date (oldest first)
- Consumes batches one by one until the needed quantity is fulfilled
- Updates batch quantities (deducts from JSONB arrays)
- Updates total stock (triggers automatic stock decrease)
FIFO Query (supabase_schema.sql:632-647)
ORDER BY fecha_vencimiento ASC- Oldest dates first- Only processes batches from Aprobada guides (not Pendiente or Rechazada)
- Skips batches that are already depleted (
cantidad > 0) - Uses
WITH ORDINALITYto track position in JSONB array
Batch Consumption Logic (supabase_schema.sql:648-660)
v_consumir = LEAST(batch_amount, remaining_needed)- Take only what’s needed or availablejsonb_set()- Updates the specific batch’s quantity in the JSONB arrayv_restante- Tracks how much more we still need- Loop continues until
v_restante <= 0or no more batches available
Example Consumption Scenario
Initial state:Concurrency Protection
The system uses PostgreSQL row-level locking to prevent race conditions when multiple operations happen simultaneously.Batch Locking (supabase_schema.sql:621-629)
- Two operations processing the same product at the same time don’t cause conflicts
- Batch quantities are updated atomically
- No batch can be over-consumed
Viewing Batches with Upcoming Expiration
The system provides a function to query batches expiring soon.get_lotes_por_vencer() Function (supabase_schema.sql:534-568)
- Identify products that need to be used soon
- Plan menus around expiring items
- Reduce waste from expired products
Batch Quantity Zero vs. Deletion
The system never deletes batch records. When a batch is fully consumed, its quantity is set to 0 but the record remains for audit purposes.
- Maintains complete audit trail
- Can track when batches were consumed
- Historical data for inventory reports
Best Practices
Always register accurate expiration dates
Always register accurate expiration dates
Enter the exact expiration date from the product packaging. For perishables without printed dates, estimate conservatively:
- Leafy vegetables: 3-5 days
- Root vegetables: 7-14 days
- Fresh meat: 2-3 days (if frozen, use freezer guidelines)
Use separate batches for different expiration dates
Use separate batches for different expiration dates
Even if it’s the same delivery, if products have different expiration dates, register them as separate batches.✓ Good:
- Lote 1: 50 kg, expires 2026-07-01
- Lote 2: 50 kg, expires 2026-08-15
- Lote 1: 100 kg, expires 2026-07-15 (averaged - loses FIFO accuracy)
Check expiring batches regularly
Check expiring batches regularly
Use the
get_lotes_por_vencer() function or implement a dashboard widget to monitor batches expiring soon.Set up a routine:- Weekly: Check batches expiring in next 14 days
- Daily: Check batches expiring in next 3 days
Understand FIFO is automatic
Understand FIFO is automatic
You don’t need to manually select which batches to use. The system automatically handles FIFO when you register daily operations.Just enter:
- Attendance count
- Products cooked
Trust the batch validation
Trust the batch validation
If the system rejects your entry guide because “Lotes no coinciden”, don’t override it. Fix the batch quantities so they sum to the total amount.This validation prevents inventory discrepancies.
Common Questions
What if I receive products with no expiration date?
What if I receive products with no expiration date?
For fresh produce or items without printed dates:
- Estimate conservatively based on the product type
- Use a standard shelf life for that category
- Document in notes that the date is estimated
Can I manually choose which batch to use?
Can I manually choose which batch to use?
No. The system always uses FIFO to prevent waste and ensure food safety. You cannot override this behavior.If you need to use a specific batch (e.g., damaged packaging), you would need to:
- Create a manual output record for that specific batch
- This is an advanced operation - consult with a Desarrollador
What happens if FIFO finds insufficient batches?
What happens if FIFO finds insufficient batches?
The operation is rejected with an error message:“Lotes insuficientes para ‘Arroz’. Faltan 5.00 unidades.”This means:
- You tried to consume more than available in approved batches
- You need to either:
- Approve pending entry guides
- Reduce the quantity needed (lower attendance or remove product)
- Receive new inventory
How do I see which batches were used in a past operation?
How do I see which batches were used in a past operation?
Batch consumption is tracked at the database level but not displayed in the UI by default.To trace consumption:
- Check the
outputtable for the operation date and products - Query
input.lotes_detalleto see batch quantities before/after - Check
audit_logfor detailed operation records
What if I entered the wrong expiration date?
What if I entered the wrong expiration date?
Currently, there’s no UI to edit batches after an entry guide is created.If the guide is still Pendiente:
- The Director can reject it
- You can create a new guide with correct information
- Contact a Desarrollador to fix the batch data directly in the database
- Document the correction in the guide’s comments
Technical Implementation Summary
Data Flow
Key Technologies
- JSONB columns - Flexible batch array storage
- GIN indexes - Fast JSONB queries
- Row-level locking - Concurrency control
- Database triggers - Automatic stock updates
- SECURITY DEFINER functions - Controlled execution
Related Resources
Entry Guide Workflow
How batches are created when registering inventory entries
Daily Operations
How FIFO consumption happens during meal service
Managing Products
Understanding product stock levels