Overview
The Purchases API handles two types of purchases:- Product purchases - Inventory replenishment with automatic stock updates
- Asset/furniture purchases - Fixed assets and equipment
Compra Model (Purchase)
integer
Unique purchase identifier (auto-generated)
integer
Foreign key to Proveedor (supplier, optional)
datetime
Purchase timestamp (default: current time)
string
Purchase type:
productos or activosdecimal
Total purchase amount (10 digits, 2 decimals, default: 0.00)
integer
Foreign key to User who created the purchase
CompraDetalle Model (Purchase Detail)
integer
Detail line identifier
integer
Foreign key to parent Compra
integer
Foreign key to Producto (optional, null for new products)
string
Product category (max 255 characters, optional)
integer
Quantity purchased (positive integer, default: 1)
decimal
Unit purchase price WITHOUT IVA (10 digits, 2 decimals)
decimal
Unit price WITH IVA (auto-calculated, default: 0.00)
decimal
Line total with IVA (cantidad × precio_con_iva, default: 0.00)
decimal
Selling price to set for this batch (10 digits, 2 decimals)
boolean
Whether product has expiration date (default: false)
date
Expiration date (required if tiene_caducidad=true)
Activo Model (Asset)
integer
Asset identifier
integer
OneToOne foreign key to Compra (tipo=‘activos’)
string
Asset name (max 255 characters)
text
Asset description (optional)
decimal
Asset value (10 digits, 2 decimals)
date
Acquisition date
Gasto Model (Expense)
integer
Expense identifier
integer
Foreign key to User
string
Expense concept (max 150 characters)
text
Detailed description (optional)
decimal
Amount (12 digits, 2 decimals)
date
Expense date (auto-set on creation)
List Purchases
Response
Returns rendered HTML with list of purchases for the authenticated user.Create Purchase (Products)
Request Parameters
string
required
Purchase type:
productos or activosinteger
Supplier ID (optional)
decimal
default:"0.00"
Total amount (typically calculated from details)
Adding Purchase Details
Details are added separately (typically via formset in web UI):integer
Existing product ID (optional, can create new product)
string
Product category for new products
integer
required
Quantity purchased
decimal
required
Unit price WITHOUT IVA
decimal
required
Selling price to set for product
boolean
default:"false"
Whether product expires
date
Expiration date (YYYY-MM-DD format, required if tiene_caducidad=true)
Response
Success: Redirects to purchase list with flash messageBusiness Logic on Purchase Creation
- Stock Update:
producto.stock += cantidad - Price Update:
producto.precio_compra = precio - Selling Price:
producto.precio = precio_venta - IVA Calculation:
precio_con_iva = precio * 1.19(19% tax) - Subtotal:
subtotal_con_iva = cantidad * precio_con_iva
Create Purchase (Assets)
Asset Parameters
string
required
Asset name
text
Asset description
decimal
required
Asset value
date
required
Acquisition date (YYYY-MM-DD)
View Purchase Detail
integer
required
Purchase ID
Response
Returns rendered HTML with purchase details including all line items.Delete Purchase
integer
required
Purchase ID to delete
Response
Success: Purchase and related details/assets deleted, redirects to listDeleting a purchase does NOT restore product stock. Consider carefully before deletion.
AJAX/API Endpoints
Get Product Data
integer
required
Product ID to fetch
Response
Get Purchase Detail (AJAX)
integer
required
Purchase ID
Response
Expenses (Gastos)
List Expenses
Create Expense
string
required
Expense concept (max 150 characters)
text
Detailed description (optional)
decimal
required
Amount (e.g., “2500000.00”)
Response
Success: Expense created, redirects to expenses listUpdate Expense
integer
required
Expense ID to update
Delete Expense
integer
required
Expense ID to delete
Example Workflow
Complete Purchase Flow
Related Endpoints
- Products API - Stock updates from purchases
- Suppliers API - Purchase order suppliers
- See source:
applications/compras/models.pyandapplications/compras/views.py