ticketbai

package module
v0.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: AGPL-3.0 Imports: 13 Imported by: 0

README

GOBL to TicketBAI

Go library to convert GOBL invoices into TicketBAI declarations and send them to the Basque Country web services.

This library assumes that clients will handle a local database of previous invoices in order to comply with the local requirements of chaining all invoices together.

Copyright Invopop Ltd. 2023. Released publicly under the GNU Affero General Public License v3.0. For commercial licenses please contact the dev team at invopop. For contributions to this library to be accepted, we will require you to accept transferring your copyright to Invopop Ltd.

Source

The basque country is split into 4 "foral" tax agencies. 3 of those tax agencies decided to adopt the TicketBAI format. Each of the three agencies has their own set of documentation and integration definitions.

Links to key information for each agency are described in the following subchapters.

Bizkaia

Usage

Go Package

You must have first created a GOBL Envelope containing an Invoice that you'd like to send to one of the TicketBAI web services.

For the document to accepted, the supplier contained in the invoice should have a "Tax ID" that includes:

  • A country code set to ES
  • A zone code set to region of one of the three Basque Country tax agencies, i.e. BI, SS, or VI. (We don't consider the address field reliable for this.)

The following is an example of how the GOBL TicketBAI package could be used:

package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/invopop/gobl"
	ticketbai "github.com/invopop/gobl.ticketbai"
	"github.com/invopop/xmldsig"
)

func main() {
	// Load sample envelope:
	data, _ := os.ReadFile("./test/data/sample-invoice.json")

	env := new(gobl.Envelope)
	if err := json.Unmarshal(data, env); err != nil {
		panic(err)
	}

	// Prepare software configuration:
	soft := &ticketbai.Software{
		License: "XYZ",        // provided by tax agency
		NIF:     "B123456789", // Software company's tax code
		Name:    "Invopop",    // Name of application
		Version: "v0.1.0",     // Software version
	}

	// Load sample certificate:
	cert, err := xmldsig.LoadCertificate(
		"./test/certs/EnpresaZigilua_SelloDeEmpresa.p12", "IZDesa2021")
	if err != nil {
		panic(err)
	}

	// Instantiate the TicketBAI client:
	tbai, err := ticketbai.New(soft,
		ticketbai.WithCertificate(cert), // Use the certificate previously loaded
		ticketbai.WithSupplierIssuer(),  // The issuer is the invoice's supplier
		ticketbai.InTesting(),           // Use the tax agency testing environment
	)
	if err != nil {
		panic(err)
	}

	// Create a new TBAI document:
	doc, err := tbai.NewDocument(env)
	if err != nil {
		panic(err)
	}

	// Create the document fingerprint:
	if err = doc.Fingerprint(prev); err != nil {
		panic(err)
	}

	// Sign the document:
	if err := doc.Sign(); err != nil {
		panic(err)
	}

	// Create the XML output
	bytes, err := doc.BytesIndent()
	if err != nil {
		panic(err)
	}

	// Do something with the output
	fmt.Println("Document created:\n", string(bytes))
}

Command Line

The GOBL TicketBAI package tool also includes a command line helper. You can find pre-built gobl.cfdi binaries in the github repository, or install manually in your Go environment with:

go install github.com/invopop/gobl.ticketbai

Usage is very straightforward:

gobl.ticketbai convert ./test/data/invoice.json

At the moment, it's not possible to add a fingertip or sign TicketBAI files using the CLI.

Limitations

  • Tickebai allows more than one customer per invoice, but GOBL only has one possible customer.

  • Invoices should have a note of type general that will be used as a general description of the invoice. If an invoice is missing this info, it will be rejected with an error.

  • Currently GOBL does not allow to distinguish between different VAT regimes. Ticketbai format requires a list of the different regimes applied to the invoice so currently only equivalence surcharge and general regime are available (for a complete list of the other possibilities you can check the documentation on https://www.batuz.eus/es/documentacion-tecnica)

  • GOBL's corrective invoices aren't supported at the moment. Only credit and debit notes are, and they are converted into "Facturas Rectificativas por Diferencias" with either positive or inverted quantities depending on whether it is a debit or a credit note.

Tags, Keys and Extensions

In order to provide the supplier specific data required by TicketBAI, invoices need to include a bit of extra data. We've managed to simplify these into specific cases.

Tax Tags

Invoice tax tags can be added to invoice documents in order to reflect a special situation. The following schemes are supported:

  • simplified-scheme - a retailer operating under a simplified tax regime (regimen simplificado) that must indicate that all of their sales are under this scheme. This implies that all operations in the invoice will have the OperacionEnRecargoDeEquivalenciaORegimenSimplificado tag set to S.
  • reverse-charge - B2B services or goods sold to a tax registered EU member who will pay VAT on the suppliers behalf. Implies that all items will be classified under the TipoNoExenta value of S2.
  • customer-rates - B2C services, specifically for the EU digital goods act (2015) which imply local taxes will be applied. All items will specify the DetalleNoSujeta cause of RL.

Tax Extensions

The following extension can be applied to each line tax:

  • es-tbai-product – allows to correctly group the invoice's lines taxes in the TicketBAI breakdowns (a.k.a. desgloses). These are the valid values:

    • services - indicates that the product being sold is a service (as opposed to a physical good). Services are accounted in the DesgloseTipoOperacion > PrestacionServicios breakdown of invoices to foreign customers. By default, all items are considered services.
    • goods - indicates that the product being sold is a physical good. Products are accounted in the DesgloseTipoOperacion > Entrega breakdown of invoices to foreign customers.
    • resale - indicates that a line item is sold without modification from a provider under the Equalisation Charge scheme. (This implies that the OperacionEnRecargoDeEquivalenciaORegimenSimplificado tag will be set to S).
  • es-tbai-exemption - identifies the specific TicketBAI reason code as to why taxes should not be applied to the line according to the whole set of exemptions or not-subject scenarios defined in the law. It has to be set along with the tax rate value of exempt. These are the valid values:

    • E1 – Exenta por el artículo 20 de la Norma Foral del IVA
    • E2 – Exenta por el artículo 21 de la Norma Foral del IVA
    • E3 – Exenta por el artículo 22 de la Norma Foral del IVA
    • E4 – Exenta por el artículo 23 y 24 de la Norma Foral del IVA
    • E5 – Exenta por el artículo 25 de la Norma Foral del IVA
    • E6 – Exenta por otra causa
    • OT – No sujeto por el artículo 7 de la Norma Foral de IVA / Otros supuestos
    • RL – No sujeto por reglas de localización (*)

(*) As noted elsewhere, RL will be set automatically set in invoices using the customer-rates tax tag. It can also be set explicitly using the es-tbai-exemption extension in invoices not using that tag.

Use-Cases

Under what situations should the TicketBAI system be expected to function:

  • B2B & B2C: regular national invoice with VAT. Operation with minimal data.
  • B2B Provider to Retailer: Include equalisation surcharge VAT rates
  • B2B Retailer: Same as regular invoice, except with invoice lines that include ext[es-tbai-product] = resale when the goods being provided are being sold without modification (recargo de equivalencia), very much related to the next point.
  • B2B Retailer Simplified: Include the simplified scheme key. (This implies that the OperacionEnRecargoDeEquivalenciaORegimenSimplificado tag will be set to S).
  • EU B2B: Reverse charge EU export, scheme: reverse-charge taxes calculated, but not applied to totals. By default all line items assumed to be services. Individual lines can use the ext[es-tbai-product] = goods value to identify when the line is a physical good. Operations like this are normally assigned the TipoNoExenta value of S2. If however the service or goods are exempt of tax, each line's tax ext[exempt] field can be used to identify a reason.
  • EU B2C Digital Goods: use tax tag customer-rates, that applies VAT according to customer location. In TicketBAI, these cases are "not subject" to tax, and thus should have the cause RL (por reglas de localización).

Test Data

Some sample test data is available in the ./test directory.

If you make any modifications to the source YAML files, the JSON envelopes will need to be updated.

Make sure you have the GOBL CLI installed (more details).

Documentation

Overview

Package ticketbai provides a client for generating and sending TicketBAI documents to the different regional services in the Basque Country.

Index

Constants

View Source
const (
	ZoneBI l10n.Code = doc.ZoneBI
	ZoneSS l10n.Code = doc.ZoneSS
	ZoneVI l10n.Code = doc.ZoneVI
)

Expose zone codes for external use.

Variables

View Source
var (
	ErrNotSpanish       = newClientError("only spanish invoices are supported")
	ErrAlreadyProcessed = newClientError("already processed")
	ErrOnlyInvoices     = newClientError("only invoices are supported")
	ErrInvalidZone      = newClientError("invalid zone")
)

Standard error responses.

Functions

This section is empty.

Types

type CancelDocument added in v0.15.0

type CancelDocument struct {
	// contains filtered or unexported fields
}

CancelDocument is a wrapper around the internal AnulaTicketBAI document.

func (*CancelDocument) Fingerprint added in v0.15.0

func (d *CancelDocument) Fingerprint() error

Fingerprint generates a finger print for the TicketBAI document using the data provided from the previous invoice data.

func (*CancelDocument) Sign added in v0.15.0

func (d *CancelDocument) Sign() error

Sign is used to generate the XML DSig components of the final XML document.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides the main interface to the TicketBAI package.

func New

func New(software *Software, opts ...Option) (*Client, error)

New creates a new TicketBAI client with shared software and configuration options for creating and sending new documents.

func (*Client) Cancel added in v0.15.0

func (c *Client) Cancel(ctx context.Context, d *CancelDocument) error

Cancel will send the cancel document in the TicketBAI gateway.

func (*Client) CurrentTime

func (c *Client) CurrentTime() time.Time

CurrentTime returns the current time to use when generating the TicketBAI document.

func (*Client) Fetch added in v0.15.0

func (c *Client) Fetch(ctx context.Context, zone l10n.Code, nif string, name string, year int, page int) ([]*doc.TicketBAI, error)

Fetch will retrieve the issued documents from the TicketBAI gateway.

func (*Client) NewCancelDocument added in v0.15.0

func (c *Client) NewCancelDocument(env *gobl.Envelope) (*CancelDocument, error)

NewCancelDocument creates a new AnulaTicketBAI document from the provided GOBL Envelope.

func (*Client) NewDocument

func (c *Client) NewDocument(env *gobl.Envelope) (*Document, error)

NewDocument creates a new TicketBAI document from the provided GOBL Envelope. The envelope must contain a valid Invoice.

func (*Client) Post

func (c *Client) Post(ctx context.Context, d *Document) error

Post will send the document to the TicketBAI gateway. It manages idempotently the possible scenario of the same document having been previously posted.

type ClientError added in v0.16.0

type ClientError struct {
	// contains filtered or unexported fields
}

ClientError is a simple wrapper around client-side errors (that should not be retried) as opposed to server-side errors (that should be retried).

func (*ClientError) Error added in v0.16.0

func (e *ClientError) Error() string

Error implements the error interface for ClientError.

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document is a wrapper around the internal TicketBAI document.

func (*Document) Bytes

func (d *Document) Bytes() ([]byte, error)

Bytes generates the byte output of the TicketBAI Document.

func (*Document) BytesIndent

func (d *Document) BytesIndent() ([]byte, error)

BytesIndent generates the indented byte output of the TicketBAI Document.

func (*Document) Fingerprint added in v0.15.0

func (d *Document) Fingerprint(prev *PreviousInvoice) error

Fingerprint generates a finger print for the TicketBAI document using the data provided from the previous invoice data. If there was no previous invoice, the parameter should be nil.

func (*Document) Head

func (d *Document) Head() *doc.CabeceraFactura

Head returns the CabeceraFactura from the TicketBAI document.

func (*Document) SetIssueTimestamp added in v0.16.0

func (d *Document) SetIssueTimestamp(ts time.Time)

SetIssueTimestamp updates the issue date and time of the TicketBAI document.

func (*Document) Sign added in v0.15.0

func (d *Document) Sign() error

Sign is used to generate the XML DSig components of the final XML document. This method will also update the GOBL Envelope with the QR codes that are generated.

func (*Document) SignatureValue

func (d *Document) SignatureValue() string

SignatureValue provides quick access to the XML signatures final value.

type Option

type Option func(*Client)

Option is used to configure the client.

func InProduction

func InProduction() Option

InProduction defines the connection to use the production environment.

func InTesting

func InTesting() Option

InTesting defines the connection to use the testing environment.

func WithCertificate

func WithCertificate(cert *xmldsig.Certificate) Option

WithCertificate defines the signing certificate to use when producing the TicketBAI document.

func WithCurrentTime

func WithCurrentTime(curTime time.Time) Option

WithCurrentTime defines the current time to use when generating the TicketBAI document. Useful for testing.

func WithCustomerIssuer added in v0.9.0

func WithCustomerIssuer() Option

WithCustomerIssuer set the issuer type to customer. To be used when the invoice's supplier, using their own certificate, is issuing the document.

func WithGateway

func WithGateway(code l10n.Code, conn gateways.Connection) Option

WithGateway defines a new gateway connection to use for a specific zone. This option can be used multiple times to define multiple gateways. Useful for testing.

func WithSupplierIssuer added in v0.9.0

func WithSupplierIssuer() Option

WithSupplierIssuer set the issuer type to supplier. To be used when the invoice's supplier, using their own certificate, is issuing the document.

func WithThirdPartyIssuer added in v0.9.0

func WithThirdPartyIssuer() Option

WithThirdPartyIssuer set the issuer type to third party. To be used when the an authorised third party, using their own certificate, is issuing the document on behalf of the invoice's supplier.

func WithZone added in v0.20.0

func WithZone(zone l10n.Code) Option

WithZone defines the zone to use when generating the TicketBAI document.

type PreviousInvoice

type PreviousInvoice struct {
	Series    string
	Code      string
	IssueDate string
	Signature string
}

PreviousInvoice stores the fields from the previously generated invoice document that are linked to in the new document.

type Software

type Software struct {
	License     string
	NIF         string
	Name        string
	CompanyName string
	Version     string
}

Software defines the details about the software that is using this library to generate TicketBAI documents. These details are included in the final document.

type TestConnection added in v0.16.0

type TestConnection struct {
	// contains filtered or unexported fields
}

TestConnection is a mock gateway connection for testing purposes

func (*TestConnection) Cancel added in v0.16.0

Cancel mocks the Cancel method of the Connection interface

func (*TestConnection) Fetch added in v0.16.0

func (tc *TestConnection) Fetch(_ context.Context, _ string, _ string, _ int, _ int, _ *doc.CabeceraFactura) ([]*doc.TicketBAI, error)

Fetch mocks the Fetch method of the Connection interface

func (*TestConnection) Post added in v0.16.0

Post mocks the Post method of the Connection interface

Directories

Path Synopsis
cmd
gobl.ticketbai
Package main provides the command line interface to the TicketBAI package.
Package main provides the command line interface to the TicketBAI package.
internal
doc
Package doc contains the TicketBAI document structures and methods to generate it.
Package doc contains the TicketBAI document structures and methods to generate it.
gateways
Package gateways contains the different interfaces to send the TicketBAI documents to.
Package gateways contains the different interfaces to send the TicketBAI documents to.
gateways/ebizkaia
Package ebizkaia provides a gatewy for generating and sending documents to the Bizkaia region.
Package ebizkaia provides a gatewy for generating and sending documents to the Bizkaia region.
Package test provides common functions for testing.
Package test provides common functions for testing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL