Overview
The sales process in Torn is a transactional, atomic operation that coordinates inventory, payments, and Chilean tax document (DTE) generation. Every sale either completes fully or rolls back entirely.Torn follows the Chilean SII (Servicio de Impuestos Internos) standards for electronic invoicing (Factura Electrónica).
Sale Flow Architecture
The sale creation follows this sequence:/app/routers/sales.py:36-315
Creating a Sale
Open a Cash Session
Before processing sales, the cashier must open their cash register:See the Cash Management Guide for details.
Document Types (DTE)
Torn supports multiple Chilean tax document types:| Code | Type | Description |
|---|---|---|
| 33 | Factura Electrónica | Standard invoice for B2B |
| 34 | Factura Exenta | Tax-exempt invoice |
| 39 | Boleta Electrónica | Receipt for retail (B2C) |
| 56 | Nota de Débito | Debit note (increase amount) |
| 61 | Nota de Crédito | Credit note (decrease/refund) |
| 111 | Nota de Crédito Electrónica (Boleta) | Credit note for receipts |
| 112 | Nota de Débito Electrónica (Boleta) | Debit note for receipts |
tipo_dte in the request to specify the document type.
Inventory Management During Sales
Stock Validation
For products withcontrola_stock = true, the system:
- Checks if
stock_actual >= cantidadrequested - Throws
409 Conflictif insufficient - Decrements
stock_actualatomically
/app/routers/sales.py:162-170
Stock Movement (Kardex) Registration
Every sale creates immutable audit records:sale_id for full traceability.
Source: /app/routers/sales.py:174-184
Payment Processing
Multiple Payment Methods
Torn supports split payments across multiple methods:Payment Validation
The system enforces:- Total payments ≥ sale total (exact or overpayment for cash change)
- Returns
400 Bad Requestif underpaid
/app/routers/sales.py:205-212
Internal Credit (Cuenta Corriente)
When usingCREDITO_INTERNO payment method:
- The system increases
customer.current_balance(debt) - No cash changes hands
- Customer can pay later or receive credits from returns
/app/routers/sales.py:266-269
Fiscal Folio Assignment (CAF)
What is a CAF?
CAF (Código de Autorización de Folios) is a range of authorized folio numbers issued by the SII for electronic documents.Folio Assignment Logic
/app/routers/sales.py:215-228
DTE XML Generation
The system automatically generates XML according to SII specifications:- Issuer identification (RUT, business name, address, ACTECO)
- Customer details
- Itemized products with prices and taxes
- Payment totals and references
- Digital signature (if CAF loaded)
/app/routers/sales.py:285-295
Listing Sales
Retrieve sales with pagination:created_at DESC (newest first).
Source: /app/routers/sales.py:47-67
Printing Sales Documents
Generate HTML for printing or PDF conversion:- 80mm thermal (default): Compact receipt format
- Carta (Letter): Full-page invoice format
/app/routers/sales.py:479-539
Available Payment Methods
List all active payment methods:/app/routers/sales.py:39-44
Error Handling
The sales endpoint returns specific HTTP status codes:| Status | Condition |
|---|---|
201 Created | Sale processed successfully |
400 Bad Request | Invalid data (underpayment, missing fields) |
404 Not Found | Customer or product doesn’t exist |
409 Conflict | Cash session closed or insufficient stock |
500 Internal Server Error | DTE generation failed (triggers rollback) |
/app/routers/sales.py:100-103
Transaction Safety
Atomicity Guarantee
All operations use database transactions:/app/routers/sales.py:284-302
Data Integrity
Torn guarantees that your inventory, financial records, and tax documents remain perfectly synchronized—even during system failures or network interruptions.
Best Practices
Cash Session Management
Always ensure the cashier has an open cash session before attempting sales. Implement UI guards to prevent sale attempts when session is closed.
Stock Monitoring
For stock-controlled products, implement low-stock alerts in your frontend to prevent sale failures due to insufficient inventory.
Payment Reconciliation
Store
transaction_code for card payments to facilitate reconciliation with payment processor reports.Audit Trail
Leverage the
audit_metadata field to track which SaaS admin performed the sale when using system users for support.Related Guides
- Returns & Refunds - Process credit notes and returns
- Cash Management - Open/close cash sessions
- Inventory Management - Stock control and movements