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)

id
integer
Unique purchase identifier (auto-generated)
proveedor
integer
Foreign key to Proveedor (supplier, optional)
fecha
datetime
Purchase timestamp (default: current time)
tipo
string
Purchase type: productos or activos
total
decimal
Total purchase amount (10 digits, 2 decimals, default: 0.00)
usuario
integer
Foreign key to User who created the purchase

CompraDetalle Model (Purchase Detail)

id
integer
Detail line identifier
compra
integer
Foreign key to parent Compra
producto
integer
Foreign key to Producto (optional, null for new products)
categoria
string
Product category (max 255 characters, optional)
cantidad
integer
Quantity purchased (positive integer, default: 1)
precio
decimal
Unit purchase price WITHOUT IVA (10 digits, 2 decimals)
precio_con_iva
decimal
Unit price WITH IVA (auto-calculated, default: 0.00)
subtotal_con_iva
decimal
Line total with IVA (cantidad × precio_con_iva, default: 0.00)
precio_venta
decimal
Selling price to set for this batch (10 digits, 2 decimals)
tiene_caducidad
boolean
Whether product has expiration date (default: false)
fecha_caducidad
date
Expiration date (required if tiene_caducidad=true)

Activo Model (Asset)

id
integer
Asset identifier
compra
integer
OneToOne foreign key to Compra (tipo=‘activos’)
nombre
string
Asset name (max 255 characters)
descripcion
text
Asset description (optional)
valor
decimal
Asset value (10 digits, 2 decimals)
fecha_adquisicion
date
Acquisition date

Gasto Model (Expense)

id
integer
Expense identifier
usuario
integer
Foreign key to User
concepto
string
Expense concept (max 150 characters)
descripcion
text
Detailed description (optional)
monto
decimal
Amount (12 digits, 2 decimals)
fecha
date
Expense date (auto-set on creation)

List Purchases

Requires authentication and admin role.
curl -X GET http://localhost:8000/compras/ \
  -H "Cookie: sessionid=<your-session>"

Response

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

Create Purchase (Products)

Requires authentication and admin role.
curl -X POST http://localhost:8000/compras/crear/ \
  -H "Cookie: sessionid=<session>; csrftoken=<token>" \
  -H "X-CSRFToken: <token>" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "tipo=productos" \
  -d "proveedor=5" \
  -d "total=5000.00"

Request Parameters

tipo
string
required
Purchase type: productos or activos
proveedor
integer
Supplier ID (optional)
total
decimal
default:"0.00"
Total amount (typically calculated from details)

Adding Purchase Details

Details are added separately (typically via formset in web UI):
curl -X POST http://localhost:8000/compras/crear/ \
  -d "producto=10" \
  -d "cantidad=50" \
  -d "precio=80.00" \
  -d "precio_venta=100.00" \
  -d "tiene_caducidad=true" \
  -d "fecha_caducidad=2025-12-31"
producto
integer
Existing product ID (optional, can create new product)
categoria
string
Product category for new products
cantidad
integer
required
Quantity purchased
precio
decimal
required
Unit price WITHOUT IVA
precio_venta
decimal
required
Selling price to set for product
tiene_caducidad
boolean
default:"false"
Whether product expires
fecha_caducidad
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)

curl -X POST http://localhost:8000/compras/crear/ \
  -H "Cookie: sessionid=<session>; csrftoken=<token>" \
  -H "X-CSRFToken: <token>" \
  -d "tipo=activos" \
  -d "proveedor=3" \
  -d "total=15000.00" \
  -d "nombre=Office Desk" \
  -d "descripcion=Executive wooden desk" \
  -d "valor=15000.00" \
  -d "fecha_adquisicion=2024-03-15"

Asset Parameters

nombre
string
required
Asset name
descripcion
text
Asset description
valor
decimal
required
Asset value
fecha_adquisicion
date
required
Acquisition date (YYYY-MM-DD)

View Purchase Detail

curl -X GET http://localhost:8000/compras/detalle/<pk>/ \
  -H "Cookie: sessionid=<session>"
pk
integer
required
Purchase ID

Response

Returns rendered HTML with purchase details including all line items.

Delete Purchase

Requires authentication and admin role.
curl -X POST http://localhost:8000/compras/eliminar/<pk>/ \
  -H "Cookie: sessionid=<session>; csrftoken=<token>" \
  -H "X-CSRFToken: <token>"
pk
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

curl -X POST http://localhost:8000/compras/ajax/obtener-producto-datos/ \
  -H "Cookie: sessionid=<session>; csrftoken=<token>" \
  -H "Content-Type: application/json" \
  -H "X-CSRFToken: <token>" \
  -d '{"producto_id": 10}'
producto_id
integer
required
Product ID to fetch

Response

{
  "success": true,
  "producto": {
    "id": 10,
    "nombre": "Laptop Dell XPS 15",
    "precio_compra": "1200.00",
    "precio_venta": "1500.00",
    "stock": 8,
    "categoria": "Electronics"
  }
}

Get Purchase Detail (AJAX)

curl -X GET http://localhost:8000/compras/api/detalle-compra/<compra_id>/ \
  -H "Cookie: sessionid=<session>"
compra_id
integer
required
Purchase ID

Response

{
  "id": 12,
  "proveedor": {
    "id": 5,
    "nombre": "TechSupply S.A.",
    "empresa": "TechSupply"
  },
  "fecha": "2024-03-15T09:00:00-05:00",
  "tipo": "productos",
  "total": "5950.00",
  "detalles": [
    {
      "producto": {
        "id": 10,
        "nombre": "Laptop Dell XPS 15"
      },
      "cantidad": 5,
      "precio": "1000.00",
      "precio_con_iva": "1190.00",
      "subtotal_con_iva": "5950.00",
      "precio_venta": "1500.00"
    }
  ]
}

Expenses (Gastos)

List Expenses

curl -X GET http://localhost:8000/compras/gastos/ \
  -H "Cookie: sessionid=<session>"

Create Expense

Requires authentication and admin role.
curl -X POST http://localhost:8000/compras/gastos/nuevo/ \
  -H "Cookie: sessionid=<session>; csrftoken=<token>" \
  -H "X-CSRFToken: <token>" \
  -d "concepto=Office Rent" \
  -d "descripcion=Monthly rent payment" \
  -d "monto=2500000.00"
concepto
string
required
Expense concept (max 150 characters)
descripcion
text
Detailed description (optional)
monto
decimal
required
Amount (e.g., “2500000.00”)

Response

Success: Expense created, redirects to expenses list

Update Expense

curl -X POST http://localhost:8000/compras/gastos/editar/<pk>/ \
  -H "Cookie: sessionid=<session>; csrftoken=<token>" \
  -H "X-CSRFToken: <token>" \
  -d "concepto=Office Rent (Updated)" \
  -d "monto=2600000.00"
pk
integer
required
Expense ID to update

Delete Expense

curl -X POST http://localhost:8000/compras/gastos/eliminar/<pk>/ \
  -H "Cookie: sessionid=<session>; csrftoken=<token>" \
  -H "X-CSRFToken: <token>"
pk
integer
required
Expense ID to delete

Example Workflow

Complete Purchase Flow

# 1. Login as admin
curl -c cookies.txt -X POST http://localhost:8000/login/ \
  -d "username=admin&password=mypass"

# 2. Get product data for purchase
curl -b cookies.txt -X POST http://localhost:8000/compras/ajax/obtener-producto-datos/ \
  -H "Content-Type: application/json" \
  -H "X-CSRFToken: <token>" \
  -d '{"producto_id": 10}'

# Response shows current stock: 8

# 3. Create purchase order
curl -b cookies.txt -X POST http://localhost:8000/compras/crear/ \
  -H "X-CSRFToken: <token>" \
  -d "tipo=productos" \
  -d "proveedor=5" \
  -d "producto=10" \
  -d "cantidad=20" \
  -d "precio=1100.00" \
  -d "precio_venta=1550.00"

# Result:
# - Stock updated: 8 + 20 = 28
# - precio_compra updated: 1100.00
# - precio (selling) updated: 1550.00
# - Total: 20 × 1100.00 × 1.19 = 26,180.00

# 4. View purchase detail
curl -b cookies.txt http://localhost:8000/compras/detalle/12/

# 5. Record business expense
curl -b cookies.txt -X POST http://localhost:8000/compras/gastos/nuevo/ \
  -H "X-CSRFToken: <token>" \
  -d "concepto=Utilities" \
  -d "monto=350000.00"
  • Products API - Stock updates from purchases
  • Suppliers API - Purchase order suppliers
  • See source: applications/compras/models.py and applications/compras/views.py

Build docs developers (and LLMs) love