La Previa Restobar treats inventory as a first-class concern. Rather than requiring manual stock adjustments after each sale, the system hooks into the order lifecycle and deducts quantities automatically whenever an order enters theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/luisumit/LaPreviaRestobar/llms.txt
Use this file to discover all available pages before exploring further.
EN_PREPARACION stage. Stock levels are stored in both Firebase Realtime Database and a local Room table, kept consistent by InventorySyncService and the offline SyncManager.
The Inventory Data Class
productId
Matches the
Product.id. The same key is used as the Firebase node key under the inventory/ collection and as the @PrimaryKey in InventoryEntity.currentStock / minimumStock
Both are
Double to support fractional units (e.g. 0.5 kg of cheese). When currentStock ≤ minimumStock, the item is considered low-stock.unitOfMeasure
A free-text field (e.g.
"kg", "unidades", "litros") displayed in admin screens and notification messages.The Product Fields That Drive Inventory
Not every product tracks stock. The Product data class has two fields that control inventory behavior:
trackInventory = true, the product is enrolled in the inventory system. Its stock value seeds the Inventory.currentStock when first synced, and minStock maps to Inventory.minimumStock.
When an
OrderItem is constructed from a Product, the trackInventory flag is copied into the item. This means the inventory deduction logic reads OrderItem.trackInventory at order-confirmation time — it does not re-query the product catalog.How Stock Deduction Works
Stock is deducted when the order status transitions toEN_PREPARACION. For every OrderItem in the order where trackInventory = true, the system calls FirebaseInventoryRepository.updateStock(productId, newQuantity) with the reduced amount.
FirebaseInventoryRepositoryImpl handles the update as a partial field patch, writing only currentStock to avoid overwriting other inventory metadata:
adjustStock in FirebaseInventoryRepositoryImpl performs the read-then-write cycle, allowing callers to pass a negative quantity for deductions:
InventorySyncService
InventorySyncService is a @Singleton that bridges the product catalog and the inventory collection. It runs as a coroutine listener in the background and reacts to real-time product changes.
Listen to product changes
startInventorySync() opens a real-time listener on the products/ Firebase node via getProductsRealTime(). distinctUntilChanged() prevents redundant processing when unrelated fields change.Filter trackable products
Only products where
trackInventory = true AND isActive = true are enrolled in inventory management.Seed missing inventory entries
If a product has no inventory record yet (
getCurrentStock returns 0.0), syncProductToInventory creates one seeded with product.stock.Clean up orphaned entries
cleanupOrphanedInventory compares the set of valid product IDs against all inventory nodes. Entries without a matching active product have their stock zeroed out, preventing phantom inventory from accumulating.Low-Stock Detection and AdminStockWorker
Periodic low-stock checks are run by AdminStockWorker, a HiltWorker that reads directly from the local Room database to avoid requiring network connectivity.
| Condition | Label | Notification color |
|---|---|---|
stock == 0.0 | AGOTADO | R.color.notification_out_of_stock (red) |
stock > 0 && stock <= minStock | Stock bajo | R.color.notification_low_stock (amber) |
admin_stock_channel channel with IMPORTANCE_HIGH and includes a PendingIntent that deep-links directly to the inventory screen inside MainActivity.
InventoryDao Queries
Local Entity: InventoryEntity
syncStatus mirrors the same two-state pattern used by orders and tables: "PENDING" means the record has a local change that has not yet reached Firebase; "SYNCED" means the local and remote copies agree.