Overview
The invoice processing system transforms Excel spreadsheet rows into structured JSON invoices using thetransformar_fila() function. Each row represents one customer invoice with columns for customer data, amounts, dates, and service details.
Transformation Flow
The transformation process follows this pipeline:Step 1: Reading Excel Data
The API endpoint receives an Excel file and processes it:main.py:252-255
- Accepts Excel files via HTTP POST
- Reads all rows into a pandas DataFrame
- Fills empty cells with empty strings to avoid null-handling issues
Step 2: Row Iteration and Transformation
Each row is transformed individually:main.py:262-263
The transformar_fila() Function
This is the core transformation function that maps Excel columns to the invoice JSON schema.Date Formatting
The function starts by parsing and formatting three date fields:main.py:75-77
fmt_fecha() helper function:
- Handles missing/empty dates gracefully
- Converts various date formats to
DD/MM/YYYY - Returns
Nonefor invalid dates
Currency Calculations
The function calculates amounts in both currencies (BSD and USD):main.py:79-84
Calculation Logic:
- IVA (tax) is calculated by subtracting the base amount from the total
- BSD amounts are rounded to 2 decimal places
- USD amounts are rounded to 0 decimal places (whole numbers)
- All conversions use explicit
float()casting to handle numeric strings
Field Mappings
| Excel Column | Calculation | JSON Field (BSD) | JSON Field (USD) |
|---|---|---|---|
bolivares | Round to 2 decimals | Totales.TotalAPagar | - |
Total | Round to 0 decimals | - | TotalesOtraMoneda.TotalAPagar |
bolivares sin iva | Round to 2 decimals | Totales.Subtotal | - |
precio sin iva | Round to 2 decimals | - | TotalesOtraMoneda.Subtotal |
| Calculated | monto_bs - bolivares_sin_iva | Totales.TotalIVA | - |
| Calculated | monto_usd - precio_sin_iva | - | TotalesOtraMoneda.TotalIVA |
Navigation Data Generation
Random navigation/usage data is generated for each invoice:main.py:86-92
InfoAdicional section:
main.py:236-242
Complete Field Mappings
Document Identification Fields
main.py:97-120
Excel to JSON Mapping Table
Excel to JSON Mapping Table
| Excel Column | Type | Transformation | JSON Path |
|---|---|---|---|
Correlativo | String | Convert to string | IdentificacionDocumento.NumeroDocumento |
Correlativo | String | Format as "FA{value}" | IdentificacionDocumento.TransaccionId |
Fecha Emision | Date | Format as DD/MM/YYYY | IdentificacionDocumento.FechaEmision |
Fecha Vencimiento | Date | Format as DD/MM/YYYY | IdentificacionDocumento.FechaVencimiento |
| - | Static | Always "01" | IdentificacionDocumento.TipoDocumento |
| - | Static | Always "BSD" | IdentificacionDocumento.Moneda |
| - | Static | Always "002" | IdentificacionDocumento.Sucursal |
Buyer (Customer) Fields
main.py:123-134
| Excel Column | Transformation | JSON Path |
|---|---|---|
Documento | Convert to string | Comprador.TipoIdentificacion |
DNI/C.I./C.C./IFE | Convert to string | Comprador.NumeroIdentificacion |
Cliente | Direct mapping | Comprador.RazonSocial |
Dirección | Direct mapping | Comprador.Direccion |
Telefono | Convert to string, wrap in array | Comprador.Telefono |
Correo | Wrap in array | Comprador.Correo |
Totals Section (BSD)
main.py:137-172
Totals Section (USD)
main.py:174-198
Tasa) from Excel is included to document the conversion factor.
Line Items
main.py:201-224
| Excel Column | Transformation | JSON Path |
|---|---|---|
Plan | Direct mapping | DetallesItems[0].Descripcion |
bolivares sin iva | Convert to string | DetallesItems[0].PrecioUnitario |
Calculated iva_bs | Convert to string | DetallesItems[0].ValorIVA |
Additional Information
main.py:228-243
Batch Organization
Invoices are organized into batches of 100 for efficient processing:main.py:262-271
Batch Structure
Calculate Batch Number
lote_num = (idx // 100) + 1- Rows 0-99 → Batch 1
- Rows 100-199 → Batch 2
- Rows 200-299 → Batch 3
Example Output Structure
Transformation Example
Input: Excel Row
| Correlativo | Cliente | DNI/C.I./C.C./IFE | Plan | bolivares | Total | Tasa | Fecha Emision |
|---|---|---|---|---|---|---|---|
| 6746 | CARLOS ALBERTO SOLANO | 14965208 | PLAN OPTIMO 300 MBPS | 4494.49 | 29 | 154.9825 | 09/09/2025 |
Output: JSON Invoice
View Generated JSON
View Generated JSON
Data Type Conversions
All numeric and date values are converted to strings for JSON serialization:| Python Type | Conversion Method | Example |
|---|---|---|
float | str(round(value, 2)) | 3874.56 → "3874.56" |
int | str(value) | 6746 → "6746" |
datetime | strftime("%d/%m/%Y") | 2025-09-09 → "09/09/2025" |
string | Direct or str() cast | "CARLOS" → "CARLOS" |
String conversion is required because the JSON schema expects all numeric fields as strings, likely for precision and compatibility with tax authority systems.