Overview
The Sales API manages both sales (ventas) and separations (separaciones/reservations). A separation is a temporary reservation that can be converted to a full sale. Both operations share the sameventas table but have different business logic.
Model Structure
Venta Model
Table:ventasPrimary Key:
id_venta (auto-increment)
Core Attributes
Primary key, auto-increment
Operation type:
'venta' or 'separacion'Operation status:
'vigente', 'vencida', 'convertida', 'cancelada'Sales agent/employee ID. Foreign key to
empleados table.Client document number. Foreign key to
clientes table.Sale or separation date
Project ID. Foreign key to
proyectos table.Property Information
Apartment ID if selling apartment. Foreign key to
apartamentos table.Commercial space ID if selling local. Foreign key to
locales table.Optional additional parking space ID. Foreign key to
parqueaderos table.Property status ID. Foreign key to
estados_inmueble table.Financial Attributes
Base value of the property (excluding parking)
VAT/Tax amount (if applicable)
Total sale value (base + parking + taxes)
Payment method ID. Foreign key to
formas_pago table.Sale-Specific Fields (tipo_operacion = ‘venta’)
Down payment amount. Must meet project minimum percentage.
Remaining balance after down payment
Down payment term in months
Payment frequency in months (e.g., 1 = monthly, 2 = bimonthly)
Final payment due date
Separation-Specific Fields (tipo_operacion = ‘separacion’)
Separation/reservation fee. Must meet project minimum.
Separation expiration date. Must be within project maximum days.
Separation period in days
Additional notes or description
Relationships
Sales agent who created the operation
Client/buyer information
Project details
Apartment details (if applicable)
Commercial space details (if applicable)
Additional parking space (if purchased separately)
Payment method details
Property status (Disponible, Separado, Vendido)
Payment schedule for down payment
Collection of payment records
List Sales & Separations
Query Parameters
Filter by type:
venta or separacionFilter by project ID
Filter by client document
Filter by status:
vigente, vencida, convertida, canceladaResponse
Get Sale by ID
Path Parameters
Sale ID (id_venta)
Create Sale
Request Body
Operation type:
'venta' or 'separacion'Sales agent ID
Client document number
Sale/separation date (format: YYYY-MM-DD)
Project ID
Property type:
'apartamento' or 'local'Property ID (apartamento or local ID based on tipo)
Payment method ID
Optional additional parking space ID (apartments only)
Down payment amount (required for sales)
Down payment term in months (required for sales)
Payment frequency in months (default: 1)
Separation fee (required for separations)
Separation expiration date (required for separations)
Additional notes (max 300 characters)
Business Logic
Sale Validation (tipo_operacion = ‘venta’)
- Down Payment Minimum:
cuota_inicialmust be ≥ project’sporcentaje_cuota_inicial_minofvalor_total - Term Limits:
plazo_cuota_inicial_mesesmust be ≤ project’splazo_max_cuota_inicial_meses - Frequency Validation:
frecuencia_cuota_inicial_mesesmust be ≤plazo_cuota_inicial_meses - Property Status: Property must have status
'Disponible' - Parking Validation:
- Only apartments can have additional parking
- Parking must be “additional” (not assigned to an apartment)
- Parking must belong to same project
- Parking must not be reserved by another sale
Separation Validation (tipo_operacion = ‘separacion’)
- Minimum Fee:
valor_separacionmust be ≥ project’svalor_min_separacion - Maximum Period:
fecha_limite_separacionmust be ≤ project’splazo_max_separacion_diasfrom today - Property Status: Property must have status
'Disponible'
Automatic Operations
- Value Calculation: Backend calculates
valor_total= property value + parking price - Remaining Balance: For sales,
valor_restante=valor_total-cuota_inicial - Property Status Update: Changes to
'Vendido'(sale) or'Separado'(separation) - Parking Assignment: If parking included, assigns to apartment in
parqueaderostable - Amortization Plan: For sales, auto-generates payment schedule
- Price Recalculation: Triggers project pricing engine (dynamic pricing based on sales count)
Response
Error Responses
Validation errors:
- Down payment below minimum
- Term exceeds maximum
- Property not available
- Parking already reserved
- Separation fee below minimum
Business logic conflicts:
- Property already has active operation
- Parking assigned to another apartment
Update Sale
Path Parameters
Sale ID to update
Updating a sale regenerates the amortization plan. Previous payment plan is deleted.
Delete Sale
Convert Separation to Sale
Path Parameters
Separation ID to convert
Request Body
Payment method for the sale
Down payment amount (must meet project minimum)
Payment term in months
Payment frequency (1 = monthly, 2 = bimonthly, etc.)
Optional: Add or change parking space
Updated description
Business Logic
-
Validation:
- Only separations with
estado_operacion = 'vigente'can be converted - Down payment must meet project minimum percentage
- Term must be within project maximum
- Only separations with
-
State Changes:
tipo_operacion:'separacion'→'venta'estado_operacion:'vigente'→'convertida'- Property status:
'Separado'→'Vendido'
-
Financial Changes:
valor_separacion→nullfecha_limite_separacion→null- Sets
cuota_inicial,valor_restante, payment terms
-
Auto-Generated:
- Payment schedule (amortization plan)
- Updates project pricing
Response
Cancel Separation
Path Parameters
Separation ID to cancel
Canceling a separation:
- Deletes the separation record
- Restores property status to “Disponible”
- Releases any parking assignment
- Triggers project price recalculation
Auto-Expire Separations (CRON)
This endpoint should be called by a scheduled task (CRON job) to automatically expire separations past their
fecha_limite_separacion.Helper Methods
Check if Sale
Check if Separation
Check if Expired
Get Property (Polymorphic)
State Transitions
Separation Lifecycle
- vigente: Active separation within expiration date
- convertida: Successfully converted to sale
- vencida: Expired (past fecha_limite_separacion)
- cancelada: Manually canceled
Sale Lifecycle
- vigente: Active sale with pending payments
- completed: All payments received (tracked externally)
- cancelada: Sale canceled (rare)
Related Endpoints
- Clients API - Manage buyers
- Payments API - Record payments
- Amortization API - Payment schedules