Skip to content

Commit

Permalink
Merge pull request #11 from invopop/bye-tax-id-zone
Browse files Browse the repository at this point in the history
Use zone from config instead of supplier's tax ID
  • Loading branch information
cavalle authored Mar 18, 2024
2 parents 795cf95 + dc9a9a2 commit 6fcf85b
Show file tree
Hide file tree
Showing 35 changed files with 123 additions and 138 deletions.
4 changes: 3 additions & 1 deletion cancel_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ type CancelDocument struct {
client *Client
}

func newCancelDocument(c *Client, env *gobl.Envelope) (*CancelDocument, error) {
// NewCancelDocument creates a new AnulaTicketBAI document from the provided
// GOBL Envelope.
func (c *Client) NewCancelDocument(env *gobl.Envelope) (*CancelDocument, error) {
d := new(CancelDocument)

// Set the client for later use
Expand Down
11 changes: 4 additions & 7 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
type Document struct {
env *gobl.Envelope
inv *bill.Invoice
zone l10n.Code
tbai *doc.TicketBAI // output

client *Client
}

func newDocument(c *Client, env *gobl.Envelope) (*Document, error) {
// NewDocument creates a new TicketBAI document from the provided GOBL Envelope.
// The envelope must contain a valid Invoice.
func (c *Client) NewDocument(env *gobl.Envelope) (*Document, error) {
d := new(Document)

// Set the client for later use
Expand All @@ -44,13 +45,9 @@ func newDocument(c *Client, env *gobl.Envelope) (*Document, error) {
if d.inv.Supplier.TaxID.Country != l10n.ES {
return nil, ErrNotSpanish
}
d.zone = d.inv.Supplier.TaxID.Zone
if d.zone == "" {
return nil, ErrInvalidZone
}

var err error
d.tbai, err = doc.NewTicketBAI(d.inv, c.CurrentTime(), c.issuerRole)
d.tbai, err = doc.NewTicketBAI(d.inv, c.CurrentTime(), c.issuerRole, c.zone)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

ticketbai "github.com/invopop/gobl.ticketbai"
"github.com/invopop/gobl.ticketbai/internal/doc"
"github.com/invopop/gobl.ticketbai/test"
"github.com/invopop/xmldsig"
"github.com/lestrrat-go/libxml2"
Expand Down Expand Up @@ -111,6 +112,7 @@ func loadTBAIClient() (*ticketbai.Client, error) {
ticketbai.WithCertificate(cert),
ticketbai.WithCurrentTime(ts),
ticketbai.WithThirdPartyIssuer(),
ticketbai.WithZone(doc.ZoneBI),
)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/go-resty/resty/v2 v2.10.0
github.com/invopop/gobl v0.67.0
github.com/invopop/gobl v0.69.0
github.com/invopop/xmldsig v0.9.0
github.com/lestrrat-go/libxml2 v0.0.0-20231124114421-99c71026c2f5
github.com/magefile/mage v1.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/gobl v0.67.0 h1:9c28iBunAWYUILIMGkx1zFH/cC5srH5wp9pAb4223MA=
github.com/invopop/gobl v0.67.0/go.mod h1:Jau+ajdfUCBPVH9VMor6aeYq3S9o7HuSNm07QxxxomE=
github.com/invopop/gobl v0.69.0 h1:W0Nn5QzvTJUXhe3T+mLfrWR9yUKx/xdKpYEPMsrT1lI=
github.com/invopop/gobl v0.69.0/go.mod h1:Jau+ajdfUCBPVH9VMor6aeYq3S9o7HuSNm07QxxxomE=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/invopop/validation v0.3.0 h1:o260kbjXzoBO/ypXDSSrCLL7SxEFUXBsX09YTE9AxZw=
Expand Down
58 changes: 29 additions & 29 deletions internal/doc/breakdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestDesgloseConversion(t *testing.T) {
t.Run("should fill DesgloseFactura when customer is from Spain", func(t *testing.T) {
goblInvoice := invoiceFromCountry(l10n.ES)

invoice, err := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, err := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)
require.NoError(t, err)

factura := invoice.Factura
Expand All @@ -37,7 +37,7 @@ func TestDesgloseConversion(t *testing.T) {
goblInvoice, _ := test.LoadInvoice("sample-invoice.json")
goblInvoice.Customer = nil

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose
assert.NotNil(t, desglose.DesgloseFactura)
Expand All @@ -47,7 +47,7 @@ func TestDesgloseConversion(t *testing.T) {
func(t *testing.T) {
goblInvoice := invoiceFromCountry("GB")

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose
assert.NotNil(t, desglose.DesgloseTipoOperacion)
Expand All @@ -56,10 +56,10 @@ func TestDesgloseConversion(t *testing.T) {
t.Run("should distinguish goods from services when customer from other country",
func(t *testing.T) {
goblInvoice := invoiceFromCountry("GB")
goblInvoice.Lines[0].Taxes.Get(tax.CategoryVAT).Ext = tax.ExtMap{es.ExtKeyTBAIProduct: "goods"}
goblInvoice.Lines[0].Taxes.Get(tax.CategoryVAT).Ext = tax.Extensions{es.ExtKeyTBAIProduct: "goods"}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

details := invoice.Factura.TipoDesglose.DesgloseTipoOperacion
assert.NotNil(t, details.Entrega)
Expand All @@ -71,7 +71,7 @@ func TestDesgloseConversion(t *testing.T) {
goblInvoice := invoiceFromCountry("GB")
goblInvoice.Lines[0].Item.Key = cbc.KeyEmpty

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

details := invoice.Factura.TipoDesglose.DesgloseTipoOperacion
assert.NotNil(t, details.PrestacionServicios)
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: "VAT",
Rate: "standard",
Ext: tax.ExtMap{
Ext: tax.Extensions{
es.ExtKeyTBAIProduct: "goods",
},
},
Expand All @@ -121,7 +121,7 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: "VAT",
Rate: "reduced",
Ext: tax.ExtMap{
Ext: tax.Extensions{
es.ExtKeyTBAIProduct: "goods",
},
},
Expand All @@ -130,7 +130,7 @@ func TestDesgloseConversion(t *testing.T) {
}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

details := invoice.Factura.TipoDesglose.DesgloseTipoOperacion
assert.Equal(t, 1, len(details.Entrega.NoSujeta.DetalleNoSujeta))
Expand Down Expand Up @@ -160,14 +160,14 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: "VAT",
Rate: "standard",
Ext: tax.ExtMap{es.ExtKeyTBAIProduct: "goods"},
Ext: tax.Extensions{es.ExtKeyTBAIProduct: "goods"},
},
},
},
}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

details := invoice.Factura.TipoDesglose.DesgloseTipoOperacion
goodsDetails := details.Entrega.Sujeta.NoExenta.DetalleNoExenta[0]
Expand All @@ -188,13 +188,13 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: "VAT",
Rate: "exempt",
Ext: tax.ExtMap{es.ExtKeyTBAIExemption: "OT"},
Ext: tax.Extensions{es.ExtKeyTBAIExemption: "OT"},
},
},
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
assert.Equal(t, "900.00", desglose.NoSujeta.DetalleNoSujeta[0].Importe.String())
Expand All @@ -211,13 +211,13 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: "VAT",
Rate: "exempt",
Ext: tax.ExtMap{es.ExtKeyTBAIExemption: "RL"},
Ext: tax.Extensions{es.ExtKeyTBAIExemption: "RL"},
},
},
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
assert.Equal(t, "RL", desglose.NoSujeta.DetalleNoSujeta[0].Causa)
Expand All @@ -234,13 +234,13 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: "VAT",
Rate: "exempt",
Ext: tax.ExtMap{es.ExtKeyTBAIExemption: "RL"},
Ext: tax.Extensions{es.ExtKeyTBAIExemption: "RL"},
},
},
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseTipoOperacion
assert.Equal(t, "900.00", desglose.PrestacionServicios.NoSujeta.DetalleNoSujeta[0].Importe.String())
Expand All @@ -259,7 +259,7 @@ func TestDesgloseConversion(t *testing.T) {
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
desgloseIVA := desglose.Sujeta.NoExenta.DetalleNoExenta[0].DesgloseIVA
Expand All @@ -283,7 +283,7 @@ func TestDesgloseConversion(t *testing.T) {
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseTipoOperacion
desgloseIVA := desglose.PrestacionServicios.Sujeta.NoExenta.DetalleNoExenta[0].DesgloseIVA
Expand All @@ -304,7 +304,7 @@ func TestDesgloseConversion(t *testing.T) {
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
desgloseIVA := desglose.Sujeta.NoExenta.DetalleNoExenta[0].DesgloseIVA
Expand Down Expand Up @@ -332,14 +332,14 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: "VAT",
Rate: "standard",
Ext: tax.ExtMap{es.ExtKeyTBAIProduct: "resale"},
Ext: tax.Extensions{es.ExtKeyTBAIProduct: "resale"},
},
},
},
}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
desgloseIVA := desglose.Sujeta.NoExenta.DetalleNoExenta[0].DesgloseIVA
Expand All @@ -359,13 +359,13 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: tax.CategoryVAT,
Rate: tax.RateExempt,
Ext: tax.ExtMap{es.ExtKeyTBAIExemption: "E1"},
Ext: tax.Extensions{es.ExtKeyTBAIExemption: "E1"},
},
},
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
assert.Equal(t, "1000.00", desglose.Sujeta.Exenta.DetalleExenta[0].BaseImponible)
Expand All @@ -385,7 +385,7 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: tax.CategoryVAT,
Rate: tax.RateExempt,
Ext: tax.ExtMap{
Ext: tax.Extensions{
es.ExtKeyTBAIExemption: "E1",
},
},
Expand All @@ -402,7 +402,7 @@ func TestDesgloseConversion(t *testing.T) {
&tax.Combo{
Category: tax.CategoryVAT,
Rate: tax.RateExempt,
Ext: tax.ExtMap{
Ext: tax.Extensions{
es.ExtKeyTBAIExemption: "E2",
},
},
Expand All @@ -411,7 +411,7 @@ func TestDesgloseConversion(t *testing.T) {
}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
e1 := findExemption(desglose.Sujeta.Exenta.DetalleExenta, "E1")
Expand All @@ -433,7 +433,7 @@ func TestDesgloseConversion(t *testing.T) {
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
desgloseIVA := desglose.Sujeta.NoExenta.DetalleNoExenta[0].DesgloseIVA
Expand All @@ -451,7 +451,7 @@ func TestDesgloseConversion(t *testing.T) {
}}
_ = goblInvoice.Calculate()

invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role)
invoice, _ := doc.NewTicketBAI(goblInvoice, ts, role, doc.ZoneBI)

desglose := invoice.Factura.TipoDesglose.DesgloseFactura
assert.Equal(t, "S2", desglose.Sujeta.NoExenta.DetalleNoExenta[0].TipoNoExenta)
Expand Down
13 changes: 10 additions & 3 deletions internal/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import (
// location is a shared location object set during init.
var location *time.Location

// TicketBAI zones
const (
ZoneBI l10n.Code = "BI" // Vizcaya
ZoneSS l10n.Code = "SS" // Guipúzcoa
ZoneVI l10n.Code = "VI" // Álava
)

func init() {
var err error
location, err = time.LoadLocation("Europe/Madrid")
Expand Down Expand Up @@ -69,8 +76,8 @@ type Cabecera struct {

// NewTicketBAI takes the GOBL Invoice and converts into a TicketBAI document
// ready to send to a regional API.
func NewTicketBAI(inv *bill.Invoice, ts time.Time, role IssuerRole) (*TicketBAI, error) {
err := validateInvoice(inv)
func NewTicketBAI(inv *bill.Invoice, ts time.Time, role IssuerRole, zone l10n.Code) (*TicketBAI, error) {
err := validate(inv, zone)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -124,7 +131,7 @@ func NewTicketBAI(inv *bill.Invoice, ts time.Time, role IssuerRole) (*TicketBAI,
return nil, err
}

doc.zone = inv.Supplier.TaxID.Zone
doc.zone = zone

return doc, nil
}
Expand Down
Loading

0 comments on commit 6fcf85b

Please sign in to comment.