VerifactuMidAPI/documentacion/formatos.md

132 lines
3.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Formatos de Entrada
La API detecta automáticamente el formato de entrada. No hay que indicarlo.
## Endpoint
```
POST /api/v1/facturas → Formato detectado automáticamente
GET /api/v1/formats → Lista formatos soportados
```
---
## Formato: `native`
Formato propio de la API. Se detecta por la presencia del campo `factura`.
```json
{
"tipo": "alta",
"factura": {
"emisor_nif": "A12345678",
"emisor_nombre": "EMPRESA EJEMPLO SL",
"num_serie": "2024-001",
"fecha_expedicion": "13-09-2024",
"tipo_factura": "F1",
"descripcion": "Servicios de consultoría",
"destinatario": {
"nombre": "CLIENTE SL",
"nif": "B98765432"
},
"iva": [
{"base": 100.00, "cuota": 21.00, "tipo": 21.00}
],
"importe_total": 121.00
},
"sistema": {
"nombre": "Mi Software",
"nif_proveedor": "A12345678",
"version": "1.0.0"
}
}
```
---
## Formato: `dolibarr`
Compatible con el formato que la web envía al BFF de Dolibarr. Los campos numéricos son `number` (no strings) y las fechas son ISO 8601. Se detecta por la presencia del campo `invoice`.
```json
{
"invoice": {
"number": "FA2024/001",
"date": "2024-09-13T00:00:00Z",
"notePublic": "Servicios de consultoría",
"lines": [
{"description": "Servicio A", "quantity": 1, "unitPrice": 60, "taxRate": 21},
{"description": "Servicio B", "quantity": 2, "unitPrice": 40, "taxRate": 10}
]
},
"client": {
"name": "CLIENTE SL",
"vatNumber": "B98765432"
},
"emisor": {
"nif": "A12345678",
"nombre": "EMPRESA EJEMPLO SL"
},
"sistema": {
"nombre": "Mi Software",
"nif_proveedor": "A12345678",
"version": "1.0.0"
}
}
```
### Mapeo Dolibarr → VeriFactu
| Dolibarr | VeriFactu |
|---|---|
| `invoice.number` | `num_serie` |
| `invoice.date` | `fecha_expedicion` (ISO → dd-mm-yyyy) |
| `invoice.notePublic` | `descripcion` |
| `lines[].quantity × unitPrice` | calcula `base = total / (1 + rate/100)`, `cuota = total - base` |
| `lines[].taxRate` | agrupa por tipo → `iva[].tipo` |
| `client.name` | `destinatario.nombre` |
| `client.vatNumber` | `destinatario.nif` |
| suma de líneas | `importe_total` (calculado automáticamente) |
| `emisor.nif` | `emisor_nif` |
| `emisor.nombre` | `emisor_nombre` |
> Los totales (`totalHt`, `totalTax`, `total`) **no se envían** — se calculan a partir de las líneas. Esto evita inconsistencias y permite validar que los números cuadran.
---
## Añadir un nuevo formato
1. Crear carpeta `internal/formats/miformato/format.go`
2. Implementar la interfaz `formats.Transformer`:
```go
package miformato
import (
"encoding/json"
"VerifactuMidAPI/internal/formats"
)
func init() {
formats.Register(&Transformer{})
}
type Transformer struct{}
func (t *Transformer) Name() string { return "miformato" }
func (t *Transformer) Transform(raw json.RawMessage) (*formats.TransformResult, error) {
// Parsear JSON de entrada
// Validar
// Devolver TransformResult
}
```
3. Importar en `main.go`:
```go
import _ "VerifactuMidAPI/internal/formats/miformato"
```
La detección es automática: el primer formato que pueda parsear el JSON se usa.