Orden de Compra API
The Orden de Compra (Purchase Order) API manages the complete lifecycle of purchase orders in the POS system, including creation, detail management, approval, and cancellation workflows. Purchase orders track materials ordered from suppliers and integrate with inventory management upon approval.Endpoints Overview
| Method | Endpoint | Authentication | Description |
|---|---|---|---|
| POST | /api/pos/ordencompra | Required | Create new purchase order |
| GET | /api/pos/ordencompra | Required | Get all purchase orders with pagination |
| GET | /api/pos/ordencompra/search | Required | Search orders by supplier |
| GET | /api/pos/ordencompra/:id | Required | Get purchase order with details |
| POST | /api/pos/ordencompra/:id/detalle | Required | Add/update order line item |
| DELETE | /api/pos/ordencompra/:id/detalle/:mp | Required | Remove order line item |
| POST | /api/pos/ordencompra/:id/aprobar | Required | Approve purchase order |
| POST | /api/pos/ordencompra/:id/anular | Required | Cancel purchase order |
Purchase Order States
Purchase orders can be in one of three states:| State | Code | Description |
|---|---|---|
| Pending | PEN | Order is being prepared, details can be modified |
| Approved | APR | Order is approved and stock has been updated |
| Cancelled | ANU | Order has been cancelled |
POST /api/pos/ordencompra
Create a new purchase order. This endpoint calls the stored proceduresp_crear_orden_compra which initializes the order in pending state.
Request
Supplier code. Must reference a valid and active supplier (e.g., PRV000123).
Response
Auto-generated purchase order code
Complete purchase order header information
Example Request
Response Examples
Success Response (201 Created)
Success Response (201 Created)
New purchase orders are created with zero totals. Add line items using the detalle endpoints to populate the order.
Missing Supplier (400)
Missing Supplier (400)
Invalid Supplier (500)
Invalid Supplier (500)
GET /api/pos/ordencompra
Retrieve a paginated list of purchase orders. Supports filtering by state and searching by supplier name or RUC.Query Parameters
Page number for pagination
Filter by order state:
PEN (Pending), APR (Approved), or ANU (Cancelled)Search term to filter by supplier name or RUC (case-insensitive)
Response
Current page number
Records per page (from PAGINATION_LIMIT env variable, default 20)
Total count of orders matching the filters
Array of purchase order objects
Example Request
Response Examples
Success Response (200 OK)
Success Response (200 OK)
No Results (404)
No Results (404)
GET /api/pos/ordencompra/search
Search purchase orders by supplier code with pagination. Alternative to the main GET endpoint when you know the exact supplier code.Query Parameters
Supplier code to filter by (e.g., PRV000123)
Page number for pagination
Example Request
Response Examples
Success Response (200 OK)
Success Response (200 OK)
Missing Parameter (400)
Missing Parameter (400)
GET /api/pos/ordencompra/:id
Retrieve a complete purchase order including header information and all line item details.Path Parameters
Purchase order code (e.g., OC00000123)
Response
Purchase order header information (see POST response for structure)
Array of order line items
Example Request
Response Examples
Success Response (200 OK)
Success Response (200 OK)
Not Found (404)
Not Found (404)
POST /api/pos/ordencompra/:id/detalle
Add or update a line item in a purchase order. Uses UPSERT logic: if the material already exists in the order, the quantity is updated; otherwise, a new line is added.Path Parameters
Purchase order code (e.g., OC00000123)
Request
Raw material code. Must reference a valid material in the materia_prima table.
Quantity to order. Must be a positive number.
Response
Updated purchase order header (totals recalculated)
The added/updated line item
Complete list of all order line items
Example Request
Response Examples
Success Response (200 OK)
Success Response (200 OK)
Totals are automatically recalculated by database triggers when line items are added or updated.
Missing Parameters (400)
Missing Parameters (400)
Order Not Pending (500)
Order Not Pending (500)
DELETE /api/pos/ordencompra/:id/detalle/:mp
Remove a line item from a purchase order. Can only be performed on pending orders.Path Parameters
Purchase order code (e.g., OC00000123)
Raw material code to remove (e.g., MP00001)
Response
Success message
Updated purchase order header
Remaining line items after deletion
Example Request
Response Examples
Success Response (200 OK)
Success Response (200 OK)
Not Found (404)
Not Found (404)
POST /api/pos/ordencompra/:id/aprobar
Approve a purchase order, changing its state fromPEN to APR. This action:
- Updates the order state to
APR - Sets the approval date (
oc_fecha_aprobacion) - Triggers inventory synchronization
- Creates kardex entries for materials
- Updates stock levels
Path Parameters
Purchase order code to approve
Response
Success message: “Orden aprobada”
Updated purchase order header with approval date
Order line items with updated state
Example Request
Response Examples
Success Response (200 OK)
Success Response (200 OK)
The approval timestamp is automatically set by the database when the order is approved.
Not Found (404)
Not Found (404)
POST /api/pos/ordencompra/:id/anular
Cancel a purchase order, changing its state toANU. Sets the cancellation date (oc_fecha_eliminacion).
Cancelled orders remain in the database for audit purposes but cannot be reactivated or modified.
Path Parameters
Purchase order code to cancel
Response
Success message: “Orden anulada”
Updated purchase order header with cancellation date
Order line items with updated state
Example Request
Response Examples
Success Response (200 OK)
Success Response (200 OK)
Not Found (404)
Not Found (404)
Error Codes
| Status Code | Description |
|---|---|
| 200 | Request successful |
| 201 | Purchase order created successfully |
| 400 | Bad request - missing required parameters |
| 404 | Purchase order not found |
| 500 | Internal server error or database constraint violation |
Purchase Order Workflow
The typical workflow for a purchase order is:Create Order
Create a new purchase order with a supplier code. Order is created in
PEN state with zero totals.Add Line Items
Add materials and quantities using the detalle endpoint. Totals are automatically calculated.
Approve Order
Approve the order to trigger inventory updates. This changes state to
APR and is irreversible.Database Triggers and Business Logic
Automatic Calculations:
- Line item subtotals are calculated automatically based on material price × quantity
- Order subtotal is the sum of all line item subtotals
- IVA (tax) is calculated as 12% of the subtotal
- Order total = subtotal + IVA
Implementation Notes
The Orden de Compra API uses the stored proceduresp_crear_orden_compra for order creation, which ensures proper initialization and sequencing. The approval process integrates with the inventory management system through database triggers that:
- Synchronize the order state across header and detail records
- Create kardex entries for inventory tracking
- Update stock levels for each material
- Set timestamps for audit trailing