95 lines
2.5 KiB
Go
95 lines
2.5 KiB
Go
package native
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"VerifactuMidAPI/internal/formats"
|
|
)
|
|
|
|
func init() {
|
|
formats.Register(&Transformer{})
|
|
}
|
|
|
|
type Transformer struct{}
|
|
|
|
func (t *Transformer) Name() string { return "native" }
|
|
|
|
type Input struct {
|
|
Tipo string `json:"tipo"`
|
|
Factura FacturaInput `json:"factura"`
|
|
Sistema SistemaInput `json:"sistema"`
|
|
}
|
|
|
|
type FacturaInput struct {
|
|
EmisorNIF string `json:"emisor_nif"`
|
|
EmisorNombre string `json:"emisor_nombre"`
|
|
NumSerie string `json:"num_serie"`
|
|
FechaExpedicion string `json:"fecha_expedicion"`
|
|
TipoFactura string `json:"tipo_factura"`
|
|
Descripcion string `json:"descripcion"`
|
|
Destinatario *DestinatarioInput `json:"destinatario,omitempty"`
|
|
IVA []IVAInput `json:"iva"`
|
|
ImporteTotal float64 `json:"importe_total"`
|
|
}
|
|
|
|
type DestinatarioInput struct {
|
|
Nombre string `json:"nombre"`
|
|
NIF string `json:"nif"`
|
|
}
|
|
|
|
type IVAInput struct {
|
|
Base float64 `json:"base"`
|
|
Cuota float64 `json:"cuota"`
|
|
Tipo float64 `json:"tipo"`
|
|
}
|
|
|
|
type SistemaInput struct {
|
|
Nombre string `json:"nombre"`
|
|
NIFProveedor string `json:"nif_proveedor"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
func (t *Transformer) Transform(raw json.RawMessage) (*formats.TransformResult, error) {
|
|
var in Input
|
|
if err := json.Unmarshal(raw, &in); err != nil {
|
|
return nil, fmt.Errorf("invalid native format: %w", err)
|
|
}
|
|
|
|
var dest *formats.DestinatarioData
|
|
if in.Factura.Destinatario != nil {
|
|
dest = &formats.DestinatarioData{
|
|
Nombre: in.Factura.Destinatario.Nombre,
|
|
NIF: in.Factura.Destinatario.NIF,
|
|
}
|
|
}
|
|
|
|
iva := make([]formats.IVAData, len(in.Factura.IVA))
|
|
for i, v := range in.Factura.IVA {
|
|
iva[i] = formats.IVAData{Base: v.Base, Cuota: v.Cuota, Tipo: v.Tipo}
|
|
}
|
|
|
|
_, err := time.Parse("02-01-2006", in.Factura.FechaExpedicion)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid fecha_expedicion: %w", err)
|
|
}
|
|
|
|
return &formats.TransformResult{
|
|
EmisorNIF: in.Factura.EmisorNIF,
|
|
EmisorNombre: in.Factura.EmisorNombre,
|
|
NumSerie: in.Factura.NumSerie,
|
|
FechaExpedicion: in.Factura.FechaExpedicion,
|
|
TipoFactura: in.Factura.TipoFactura,
|
|
Descripcion: in.Factura.Descripcion,
|
|
Destinatario: dest,
|
|
IVA: iva,
|
|
ImporteTotal: in.Factura.ImporteTotal,
|
|
Sistema: formats.SistemaData{
|
|
Nombre: in.Sistema.Nombre,
|
|
NIFProveedor: in.Sistema.NIFProveedor,
|
|
Version: in.Sistema.Version,
|
|
},
|
|
}, nil
|
|
}
|