Skip to main content

Overview

The Sales API manages sale transactions with automatic stock updates, IVA (tax) calculations, and support for both quick sales and registered client sales.

Venta Model (Sale)

integer
Unique sale identifier (auto-generated)
integer
Foreign key to admin user (owner)
string
Quick sale customer name (optional, max 100 characters)
integer
Foreign key to registered Cliente (optional, takes precedence over cliente)
string
Payment method: Efectivo or Transferencia (default: Efectivo)
string
Bank name (required if forma_pago='Transferencia', max 50 characters)
string
Transaction reference number (optional, max 100 characters)
datetime
Sale timestamp (default: current time)
integer
Foreign key to User who created the sale
decimal
Total amount including IVA (12 digits, 2 decimals, auto-calculated)

DetalleVenta Model (Sale Detail)

integer
Detail line identifier
integer
Foreign key to parent Venta
integer
Foreign key to Producto (PROTECT constraint - cannot delete products in sales)
integer
Quantity sold (positive integer, default: 1)
decimal
Base price before IVA (10 digits, 2 decimals)
decimal
IVA amount per unit (auto-calculated: 19% of precio_unitario)
decimal
Final unit price with IVA (auto-calculated: precio_unitario + monto_iva)
decimal
Line total (auto-calculated: cantidad × precio_unitario_final)
decimal
Product cost at time of sale (copied from producto.precio_compra)

IngresoExtra Model (Extra Income)

integer
Extra income identifier
integer
Foreign key to User
string
Income type: domicilio, servicio, mano_obra, or otro
string
Description (max 200 characters, optional)
decimal
Amount (10 digits, 2 decimals)
datetime
Income timestamp (default: current time)

List Sales

Response

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

Create Sale

Requires authentication. Both admin and vendedor can create sales.

Request Parameters

string
Customer name for quick sale (optional if cliente_fk provided)
integer
Registered client ID (optional, takes precedence)
string
required
Payment method: Efectivo or Transferencia
string
Bank name (required if forma_pago=‘Transferencia’)
string
Transaction reference

Adding Products to Sale

Products are typically added via the frontend interface or separate AJAX calls. The sale detail creation:
  1. Decrements product stock automatically
  2. Calculates IVA (19%)
  3. Calculates subtotals
  4. Updates total_venta on parent Venta

Response

Success: Redirects to sales list with flash message

Update Sale

URL Parameters

integer
required
Sale ID to update

Delete Sale

Deleting a sale restores product stock for all detail lines.

URL Parameters

integer
required
Sale ID to delete

Response

Success: Stock restored, sale deleted, redirects to sales list

AJAX/API Endpoints

Search Client

string
required
Client name to search (partial match)

Response

Search Product by Barcode

string
required
Product barcode to search

Response

Product Not Found:

Get Sale Detail (JSON)

integer
required
Sale ID

Response

Search Sales

string
required
Search query (searches cliente name)

Get Products by Sale

integer
required
Sale ID

Response

Returns JSON array of products in the sale.

Extra Income

Register Extra Income

string
required
Income type: domicilio, servicio, mano_obra, or otro
string
Description (max 200 characters)
decimal
required
Amount (e.g., “5000.00”)

Response

Get Extra Income Detail

Delete Extra Income

Generate Sale Invoice (PDF)

integer
required
Sale ID

Response

Content-Type: application/pdf Downloads PDF invoice with sale details.

Business Logic

Automatic Stock Management

When DetalleVenta is saved:
When DetalleVenta is deleted:
When DetalleVenta quantity updated:

IVA Calculation (19%)

Constant defined: IVA_RATE = Decimal('0.19') On save:

Total Venta Update

After each detail save/delete:

Cost Tracking

On first save, costo_compra copied from product:
This preserves historical cost for profit analysis.

Example Workflow

  • Products API - Stock management
  • Clients API - Registered customer sales
  • See source: applications/ventas/views.py and applications/ventas/models.py