Skip to content

Convert GOBL documents into the Spanish FacturaE format

License

Notifications You must be signed in to change notification settings

invopop/gobl.facturae

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GOBL to FacturaE Toolkit

Convert GOBL documents into the Spain's FacturaE format.

Copyright Invopop Ltd. 2023. Released publicly under the Apache License Version 2.0. For commercial licenses please contact the dev team at invopop. In order to accept contributions to this library we will require transferring copyrights to Invopop Ltd.

Lint Test Go Go Report Card GoDoc Latest Tag

Usage

Go

There are a couple of entry points to build a new FacturaE document. If you already have a GOBL Envelope available in Go, you could convert and output to a data file like this:

doc, err := facturae.NewInvoice(env)
if err != nil {
    panic(err)
}

data, err := doc.Bytes()
if err != nil {
    panic(err)
}

if err = os.WriteFile("./test.xml", data, 0644); err != nil {
    panic(err)
}

If you're loading from a file, you can use the LoadGOBL convenience method:

doc, err := facturae.LoadGOBL(file)
if err != nil {
    panic(err)
}
// do something with doc

Outputting to a FacturaE XML is most useful when the document is signed. Use a certificate to sign the document as follows:

// import from github.com/invopop/xmldsig
cert, err := xmldsig.LoadCertificate(filename, password)
if err != nil {
    panic(err)
}

doc, err := facturae.NewInvoice(env, facturae.WithCertificate(cert))
if err != nil {
    panic(err)
}

CLI

The command line interface can be useful for situations when you're using a language other than Golang in your application.

# install example

Simply provide the input GOBL JSON file and output to a file or another application:

./gobl.facturae convert input.json output.xml

If you have a digital certificate, run with:

./gobl.facturae convert -c cert.p12 -p password input.json > output.xml

The command also supports pipes:

cat input.json > ./gobl.facturae > output.xml

Testing

This package uses lestrrat-go/libxml2 for testing purporses, which in turn depends on the libxml-2.0 C library. Ensure you have the development dependency installed. In linux this implies:

sudo apt-get install libxml2-dev

To specifically run the examples and update the output in the /test/data/out directory, run:

go test ./examples_test.go --update

For automated testing purposes, we don't attach certificates to the XML output. For manual testing of complete XML documents, digital certificates are available in the /test/certificates path which can be used to generate XML documents in the /test/data/ path:

mage -v convertXML

YAML files are used for base examples. To generate the GOBL JSON, run:

mage -v convertYAML

Notes

  • To validate the XML output and digital certificates, use https://face.gob.es/es/facturas/validar-visualizar-facturas
  • In most cases Go structures have been written using the same naming from the XML style document. This means names are not repeated in tags and generally makes it a bit easier map the XML output to the internal structures.

Current Conversion Limitations

The FacturaE format is quite complex due to the number of local requirements in Spain.

  • Payment Information: FacturaE requires each payment instruction to have a Due Date. The GOBL invoice allows these details to be independent. If you require payment instructions to appear on a FacturaE document, there must be a due date.