VerifactuMidAPI/documentacion/formatos.md

133 lines
3.0 KiB
Markdown

# 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 BFF de Dolibarr. Se detecta por la presencia del campo `invoice`. Agrupa automáticamente las líneas por tipo de IVA.
```json
{
"invoice": {
"number": "FA2024/001",
"date": "2024-09-13T00:00:00Z",
"totalHt": 100.00,
"totalTax": 21.00,
"total": 121.00,
"notePublic": "Servicios de consultoría",
"lines": [
{"description": "Servicio A", "quantity": 1, "unitPrice": 60, "taxRate": 21, "total": 72.60},
{"description": "Servicio B", "quantity": 1, "unitPrice": 40, "taxRate": 21, "total": 48.40}
]
},
"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` (convierte ISO → dd-mm-yyyy) |
| `invoice.notePublic` | `descripcion` |
| `lines[].taxRate` | agrupa por tipo → `iva[].tipo` |
| `lines[].total` | calcula `base = total / (1 + rate/100)`, `cuota = total - base` |
| `client.name` | `destinatario.nombre` |
| `client.vatNumber` | `destinatario.nif` |
| `invoice.total` | `importe_total` |
| `emisor.nif` | `emisor_nif` |
| `emisor.nombre` | `emisor_nombre` |
---
## 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.