Purchase orders close the inventory loop by providing a structured, auditable way to bring stock in from suppliers. An order moves from a mutable BORRADOR draft through to RECIBIDA receipt, at which point the system automatically posts stock-entry movements for every line item and locks the order against further changes.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/juadariasmar/inventory_project/llms.txt
Use this file to discover all available pages before exploring further.
OrdenCompra model
| Field | Type | Description |
|---|---|---|
id | Int | Auto-incremented primary key |
proveedorId | Int | FK to the Proveedor who will fulfill the order |
estado | EstadoOrdenCompra | BORRADOR, RECIBIDA, or CANCELADA |
total | Float | Sum of all line subtotals (computed at creation) |
notas | String? | Optional free-text notes |
items | OrdenCompraItem[] | Line items |
creadoEn | DateTime | UTC timestamp of creation |
recibidaEn | DateTime? | Timestamp set when the order is received |
empresaId | String | Tenant key |
EstadoOrdenCompra enum and state transitions
BORRADOR
The order has been created and can still be edited. Items can be added or removed. No stock has been affected.
RECIBIDA
Merchandise has arrived. Stock-entry movements have been created automatically. The order record is now immutable.
CANCELADA
The order was cancelled before receipt. No stock was ever affected. Only orders in
BORRADOR can be cancelled.OrdenCompraItem structure
Each line item specifies what is being ordered and at what cost:| Field | Type | Description |
|---|---|---|
id | Int | Auto-incremented primary key |
ordenCompraId | Int | FK to the parent OrdenCompra |
productoId | Int | FK to the Producto being ordered |
cantidad | Int | Units ordered (positive integer) |
costoUnitario | Float | Purchase cost per unit |
subtotal | Float | cantidad × costoUnitario |
Receiving an order
OrdenesCompraService.recibir executes the following steps inside a single database transaction:
Lock product rows
Acquires a pessimistic
SELECT … FOR UPDATE lock on all products referenced in the order items (sorted by ID) to prevent concurrent quantity conflicts.Create stock-entry movements
For each
OrdenCompraItem, creates a Movimiento of type entrada with ordenCompraId set and a note of "Orden de compra #<id>".Cancelling an order
BORRADOR state can be cancelled. Attempting to cancel a RECIBIDA or already CANCELADA order returns a 409 Conflict error. Cancellation does not affect stock because no movements were ever created for a draft order.
PDF generation
Each purchase order can be exported as a formatted PDF document:Content-Type: application/pdf and includes supplier contact details, all line items with costs, and the order total. PDFs are generated server-side using the pdf-lib library (v1.17.1). This is useful for sending to suppliers or archiving with physical delivery notes.
Purchase suggestions
The suggestion engine scans the product catalog for items whose current quantity has dropped at or below theirstockMinimo threshold (plus the alert margin) and recommends how many units to purchase:
sugerenciaCompra) is calculated by calcularSugerenciaCompraInteligente in src/lib/inventario.ts. When a product has sales history, the algorithm aims to cover 14 days of average daily consumption plus the stockMinimo as a safety buffer. For products without sales history it falls back to the simpler formula: max(stockMinimo × 2, stockMinimo + alertMargin + 1) − cantidadActual.
Proveedor model
Suppliers are stored asProveedor records scoped to the company:
| Field | Type | Description |
|---|---|---|
id | Int | Auto-incremented primary key |
nombre | String | Supplier display name (required) |
nit | String? | Tax identification number |
contacto | String? | Contact person name |
telefono | String? | Phone number |
email | String? | Email address |
direccion | String? | Physical address |
activo | Boolean | Whether the supplier is currently active (default true) |
empresaId | String | Tenant key |
Code example — create a purchase order
OrdenCompra object including the nested items array and proveedor summary. The order is created in BORRADOR state with total computed automatically as 200 × 900 + 50 × 4500 = 405 000.