Skip to main content

Overview

The Purchases API handles two types of purchases:
  1. Product purchases - Inventory replenishment with automatic stock updates
  2. Asset/furniture purchases - Fixed assets and equipment
Additionally manages general business expenses (Gastos).

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 activos
decimal
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

Requires authentication and admin role.

Response

Returns rendered HTML with list of purchases for the authenticated user.

Create Purchase (Products)

Requires authentication and admin role.

Request Parameters

string
required
Purchase type: productos or activos
integer
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 message

Business Logic on Purchase Creation

  1. Stock Update: producto.stock += cantidad
  2. Price Update: producto.precio_compra = precio
  3. Selling Price: producto.precio = precio_venta
  4. IVA Calculation: precio_con_iva = precio * 1.19 (19% tax)
  5. 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

Requires authentication and admin role.
integer
required
Purchase ID to delete

Response

Success: Purchase and related details/assets deleted, redirects to list
Deleting 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

Requires authentication and admin role.
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 list

Update Expense

integer
required
Expense ID to update

Delete Expense

integer
required
Expense ID to delete

Example Workflow

Complete Purchase Flow

  • Products API - Stock updates from purchases
  • Suppliers API - Purchase order suppliers
  • See source: applications/compras/models.py and applications/compras/views.py