Overview
The purchase system handles inventory procurement from suppliers. Purchases automatically update product stock levels and maintain detailed records of costs and quantities for inventory management.Purchase Entity
TheCompra entity represents a complete purchase transaction from a supplier.
Entity Fields
Unique purchase order identifier (primary key)
Sequential order number for display purposes
Supplier code (foreign key to
proveedores table)User ID who created the purchase (foreign key to
users table)Purchase date and time (defaults to current datetime)
Sum of all line items before tax
Tax amount (IVA - Value Added Tax)
Final purchase total (Subtotal + IVA)
Purchase status (1 = active, other values for different states)
Relationships
- ComprasDetalles: Collection of line items for this purchase
- CodprovNavigation: Supplier information
- CodusuNavigation: User who created the purchase
Purchase Details
Each purchase contains one or more line items stored in theComprasDetalle entity.
Line Item Fields
Auto-generated line item identifier
Purchase order reference (foreign key to
compras)Product code (foreign key to
productos)Quantity of product purchased
Unit purchase price for this product
Line total (Cantidad × Precio)
Relationships
- NordenNavigation: Parent purchase record
- CodprodNavigation: Product information
Creating a Purchase
Select Supplier
Choose the supplier (
Codprov) from the proveedores table. Ensure the supplier is active (estado = 1).Create Purchase Header
Create a new
Compra record with:- Unique
Norden(purchase order ID) - Sequential
OrdenNfor display - Supplier code (
Codprov) - User creating the purchase (
Codusu) - Current date/time (
Fecha)
Add Purchase Line Items
Create
ComprasDetalle records for each product being purchased:- Product code (
Codprod) - Quantity (
Cantidad) - Unit price (
Precio) - Calculate line subtotal:
Cantidad × Precio
Calculate Totals
Sum all line item subtotals to get the purchase
Subtotal, calculate Iva (tax), and compute the final Total:Inventory Impact
When purchase details are saved, the system should increase product stock levels:Cascade Deletion
Purchase details are configured withON DELETE CASCADE, meaning:
- Deleting a purchase (
Compra) automatically deletes all associated line items (ComprasDetalle) - This maintains referential integrity
- Ensure stock adjustments are handled before deletion if necessary
Database Schema
Key Differences from Sales
| Aspect | Purchases | Sales |
|---|---|---|
| Entity | Compra | Venta |
| Partner | Supplier (proveedores) | Customer (clientes) |
| Stock Impact | Increases inventory | Decreases inventory |
| Payment Plans | Not supported | Supports CONTADO/CREDITO |
| Interest | N/A | Available for credit sales |
| Voiding | Use estado field | Use nula field |
Stock Management
Purchases work together with the inventory system:- Stock Level: Updated automatically when purchase details are saved
- Minimum Stock: Check
stockMinimoto identify reorder points - Purchase Price: Update
precioComprafor margin calculations - Low Stock Alerts: Index on
stock, stockMinimoenables efficient queries
Best Practices
Validate Stock Levels
Validate Stock Levels
Before creating a purchase, verify current stock levels to avoid over-ordering:
Track Purchase History
Track Purchase History
Use purchase history to identify reliable suppliers and track price trends:
Maintain Accurate Dates
Maintain Accurate Dates
Record accurate purchase dates for:
- Cost of goods sold (COGS) calculations
- Supplier performance analysis
- Seasonal demand planning