From 3dbf85d21b1700b340de88c18d1425e1fc16925c Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Thu, 21 Sep 2023 11:05:07 +0000 Subject: [PATCH 1/2] Refactoring number handling so no longer required --- address.go | 52 ++++++++++++++++--------------------------------- fatturapa.go | 3 ++- go.mod | 6 ++---- go.sum | 14 ++++--------- header.go | 4 ++-- parties.go | 41 ++++++++++++++++---------------------- parties_test.go | 6 ++++-- test/test.go | 3 ++- 8 files changed, 50 insertions(+), 79 deletions(-) diff --git a/address.go b/address.go index 0cb8d32..1044666 100644 --- a/address.go +++ b/address.go @@ -1,51 +1,33 @@ package fatturapa import ( - "errors" - "github.com/invopop/gobl/org" ) +// address from IndirizzoType type address struct { - Indirizzo string - NumeroCivico string `xml:",omitempty"` - CAP string - Comune string - Provincia string `xml:",omitempty"` - Nazione string + Indirizzo string // Street + NumeroCivico string `xml:",omitempty"` // Number + CAP string // Post Code + Comune string // Locality + Provincia string `xml:",omitempty"` // Region + Nazione string // Country Code } -func newAddress(p *org.Party) (*address, error) { - if len(p.Addresses) == 0 { - return nil, errors.New("party missing address") - } - - addr := p.Addresses[0] - +func newAddress(addr *org.Address) *address { return &address{ - Indirizzo: addressLine(addr), - CAP: addr.Code, - Comune: addr.Locality, - Provincia: addr.Region, - Nazione: addr.Country.String(), - }, nil + Indirizzo: addressStreet(addr), + NumeroCivico: addr.Number, + CAP: addr.Code, + Comune: addr.Locality, + Provincia: addr.Region, + Nazione: addr.Country.String(), + } } -func addressLine(address *org.Address) string { +func addressStreet(address *org.Address) string { if address.PostOfficeBox != "" { return address.PostOfficeBox } - - return address.Street + - ", " + address.Number + - addressMaybe(address.Block) + - addressMaybe(address.Floor) + - addressMaybe(address.Door) -} - -func addressMaybe(element string) string { - if element != "" { - return ", " + element - } - return "" + return address.Street } diff --git a/fatturapa.go b/fatturapa.go index 8992aca..b538892 100644 --- a/fatturapa.go +++ b/fatturapa.go @@ -50,7 +50,8 @@ func (c *Converter) ConvertFromGOBL(env *gobl.Envelope) (*Document, error) { } // Make sure we're dealing with raw data - invoice, err := invoice.RemoveIncludedTaxes() + var err error + invoice, err = invoice.RemoveIncludedTaxes() if err != nil { return nil, err } diff --git a/go.mod b/go.mod index 255019d..69bdda6 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,11 @@ module github.com/invopop/gobl.fatturapa go 1.19 require ( - github.com/invopop/gobl v0.55.0 + github.com/invopop/gobl v0.58.1 github.com/invopop/xmldsig v0.7.0 github.com/magefile/mage v1.14.0 github.com/spf13/cobra v1.7.0 - github.com/stretchr/testify v1.8.2 + github.com/stretchr/testify v1.8.4 gitlab.com/flimzy/testy v0.12.4 ) @@ -32,5 +32,3 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect software.sslmate.com/src/go-pkcs12 v0.2.0 // indirect ) - -// replace github.com/invopop/gobl => ../gobl diff --git a/go.sum b/go.sum index ad55be8..84bd598 100644 --- a/go.sum +++ b/go.sum @@ -792,14 +792,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= 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.50.2 h1:wQgx85xDE9wHdrtMjIctvlkpY2BKHUNs7qn9/cC9qMw= -github.com/invopop/gobl v0.50.2/go.mod h1:X7EqVSEfeeIymdaOUrwkHu+OPPNATAHDQ8FPs2BvZFM= -github.com/invopop/gobl v0.54.0 h1:4sejjcl+p6thFoFVmi1w+yHNpHV2gbM/k9flT8q9SYk= -github.com/invopop/gobl v0.54.0/go.mod h1:/s8rsPX3RK5J/yGK41lzzzB+zXeC29rpIZBYjSjvYzM= -github.com/invopop/gobl v0.54.1-0.20230828145858-ddfafb15835d h1:LoEAkw3y/Fa5GtiVV8bVkaej1XywpbROB55a3XU+Jf8= -github.com/invopop/gobl v0.54.1-0.20230828145858-ddfafb15835d/go.mod h1:/s8rsPX3RK5J/yGK41lzzzB+zXeC29rpIZBYjSjvYzM= -github.com/invopop/gobl v0.55.0 h1:NV62UR/W2f/mWwqhnlkjuBad/Zb64u50pDIQDBTeFnw= -github.com/invopop/gobl v0.55.0/go.mod h1:/s8rsPX3RK5J/yGK41lzzzB+zXeC29rpIZBYjSjvYzM= +github.com/invopop/gobl v0.58.1 h1:Iagqgx1Z6IsqypAaMWUnITMUIcDautfK/k1SeIPH7S0= +github.com/invopop/gobl v0.58.1/go.mod h1:V4YpkOYucgBYbk3VMlUZJSENjg1N5NTGV0Baok6H5J0= github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So= github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0= github.com/invopop/validation v0.3.0 h1:o260kbjXzoBO/ypXDSSrCLL7SxEFUXBsX09YTE9AxZw= @@ -889,8 +883,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/header.go b/header.go index 1feb49c..3236003 100644 --- a/header.go +++ b/header.go @@ -13,11 +13,11 @@ type fatturaElettronicaHeader struct { } func newFatturaElettronicaHeader(inv *bill.Invoice, datiTrasmissione *datiTrasmissione) (*fatturaElettronicaHeader, error) { - supplier, err := newCedentePrestatore(inv) + supplier, err := newCedentePrestatore(inv.Supplier) if err != nil { return nil, err } - customer, err := newCessionarioCommittente(inv) + customer, err := newCessionarioCommittente(inv.Customer) if err != nil { return nil, err } diff --git a/parties.go b/parties.go index 1183df5..5da7252 100644 --- a/parties.go +++ b/parties.go @@ -3,7 +3,6 @@ package fatturapa import ( "errors" - "github.com/invopop/gobl/bill" "github.com/invopop/gobl/l10n" "github.com/invopop/gobl/org" "github.com/invopop/gobl/regimes/it" @@ -71,17 +70,8 @@ type contatti struct { Email string `xml:",omitempty"` } -func newCedentePrestatore(inv *bill.Invoice) (*supplier, error) { - s := inv.Supplier - - address, err := newAddress(s) - if err != nil { - return nil, err - } - - contatti := newContatti(s) - - return &supplier{ +func newCedentePrestatore(s *org.Party) (*supplier, error) { + ns := &supplier{ DatiAnagrafici: &datiAnagrafici{ IdFiscaleIVA: &taxID{ IdPaese: s.TaxID.Country.String(), @@ -90,18 +80,22 @@ func newCedentePrestatore(inv *bill.Invoice) (*supplier, error) { Anagrafica: newAnagrafica(s), RegimeFiscale: s.Ext[it.ExtKeySDIFiscalRegime].String(), }, - Sede: address, IscrizioneREA: newIscrizioneREA(s), - Contatti: contatti, - }, nil + Contatti: newContatti(s), + } + + if len(s.Addresses) > 0 { + ns.Sede = newAddress(s.Addresses[0]) + } + + return ns, nil } -func newCessionarioCommittente(inv *bill.Invoice) (*customer, error) { - c := inv.Customer +func newCessionarioCommittente(c *org.Party) (*customer, error) { + nc := new(customer) - address, err := newAddress(c) - if err != nil { - return nil, err + if len(c.Addresses) > 0 { + nc.Sede = newAddress(c.Addresses[0]) } da := &datiAnagrafici{ @@ -124,10 +118,9 @@ func newCessionarioCommittente(inv *bill.Invoice) (*customer, error) { da.IdFiscaleIVA = customerFiscaleIVA(c.TaxID, nonEUCitizenTaxCodeDefault) } - return &customer{ - DatiAnagrafici: da, - Sede: address, - }, nil + nc.DatiAnagrafici = da + + return nc, nil } func newAnagrafica(party *org.Party) *anagrafica { diff --git a/parties_test.go b/parties_test.go index 3d83849..5193bcb 100644 --- a/parties_test.go +++ b/parties_test.go @@ -22,7 +22,8 @@ func TestPartiesSupplier(t *testing.T) { assert.Equal(t, "12345678903", s.DatiAnagrafici.IdFiscaleIVA.IdCodice) assert.Equal(t, "MªF. Services", s.DatiAnagrafici.Anagrafica.Denominazione) assert.Equal(t, "RF01", s.DatiAnagrafici.RegimeFiscale) - assert.Equal(t, "VIALE DELLA LIBERTÀ, 1", s.Sede.Indirizzo) + assert.Equal(t, "VIALE DELLA LIBERTÀ", s.Sede.Indirizzo) + assert.Equal(t, "1", s.Sede.NumeroCivico) assert.Equal(t, "00100", s.Sede.CAP) assert.Equal(t, "ROMA", s.Sede.Comune) assert.Equal(t, "RM", s.Sede.Provincia) @@ -48,7 +49,8 @@ func TestPartiesCustomer(t *testing.T) { assert.Equal(t, "MARIO", c.DatiAnagrafici.Anagrafica.Nome) assert.Equal(t, "LEONI", c.DatiAnagrafici.Anagrafica.Cognome) assert.Equal(t, "Dott.", c.DatiAnagrafici.Anagrafica.Titolo) - assert.Equal(t, "VIALE DELI LAVORATORI, 32", c.Sede.Indirizzo) + assert.Equal(t, "VIALE DELI LAVORATORI", c.Sede.Indirizzo) + assert.Equal(t, "32", c.Sede.NumeroCivico) assert.Equal(t, "50100", c.Sede.CAP) assert.Equal(t, "FIRENZE", c.Sede.Comune) assert.Equal(t, "FI", c.Sede.Provincia) diff --git a/test/test.go b/test/test.go index 9cdc852..06189fa 100644 --- a/test/test.go +++ b/test/test.go @@ -13,6 +13,7 @@ import ( fatturapa "github.com/invopop/gobl.fatturapa" "github.com/invopop/gobl/bill" "github.com/invopop/gobl/l10n" + "github.com/invopop/gobl/schema" "github.com/invopop/xmldsig" ) @@ -122,7 +123,7 @@ func ModifyInvoice(env *gobl.Envelope, modifyFunc func(*bill.Invoice)) { modifyFunc(inv) - doc, err := gobl.NewDocument(inv) + doc, err := schema.NewObject(inv) if err != nil { panic(err) } From 41f515ea17c08932ac8b97de65943c5106d5a21f Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Thu, 21 Sep 2023 11:16:01 +0000 Subject: [PATCH 2/2] Removing unnecessary errors --- fatturapa.go | 5 +---- header.go | 14 ++++---------- parties.go | 32 ++++++++++++-------------------- parties_test.go | 4 ++-- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/fatturapa.go b/fatturapa.go index b538892..494a0a2 100644 --- a/fatturapa.go +++ b/fatturapa.go @@ -58,10 +58,7 @@ func (c *Converter) ConvertFromGOBL(env *gobl.Envelope) (*Document, error) { datiTrasmissione := c.newDatiTrasmissione(invoice, env) - header, err := newFatturaElettronicaHeader(invoice, datiTrasmissione) - if err != nil { - return nil, err - } + header := newFatturaElettronicaHeader(invoice, datiTrasmissione) body, err := newFatturaElettronicaBody(invoice) if err != nil { diff --git a/header.go b/header.go index 3236003..a8f44a3 100644 --- a/header.go +++ b/header.go @@ -12,19 +12,13 @@ type fatturaElettronicaHeader struct { CessionarioCommittente *customer `xml:",omitempty"` } -func newFatturaElettronicaHeader(inv *bill.Invoice, datiTrasmissione *datiTrasmissione) (*fatturaElettronicaHeader, error) { - supplier, err := newCedentePrestatore(inv.Supplier) - if err != nil { - return nil, err - } - customer, err := newCessionarioCommittente(inv.Customer) - if err != nil { - return nil, err - } +func newFatturaElettronicaHeader(inv *bill.Invoice, datiTrasmissione *datiTrasmissione) *fatturaElettronicaHeader { + supplier := newCedentePrestatore(inv.Supplier) + customer := newCessionarioCommittente(inv.Customer) return &fatturaElettronicaHeader{ DatiTrasmissione: datiTrasmissione, CedentePrestatore: supplier, CessionarioCommittente: customer, - }, nil + } } diff --git a/parties.go b/parties.go index 5da7252..3ff1519 100644 --- a/parties.go +++ b/parties.go @@ -1,8 +1,6 @@ package fatturapa import ( - "errors" - "github.com/invopop/gobl/l10n" "github.com/invopop/gobl/org" "github.com/invopop/gobl/regimes/it" @@ -70,7 +68,7 @@ type contatti struct { Email string `xml:",omitempty"` } -func newCedentePrestatore(s *org.Party) (*supplier, error) { +func newCedentePrestatore(s *org.Party) *supplier { ns := &supplier{ DatiAnagrafici: &datiAnagrafici{ IdFiscaleIVA: &taxID{ @@ -88,10 +86,10 @@ func newCedentePrestatore(s *org.Party) (*supplier, error) { ns.Sede = newAddress(s.Addresses[0]) } - return ns, nil + return ns } -func newCessionarioCommittente(c *org.Party) (*customer, error) { +func newCessionarioCommittente(c *org.Party) *customer { nc := new(customer) if len(c.Addresses) > 0 { @@ -102,25 +100,19 @@ func newCessionarioCommittente(c *org.Party) (*customer, error) { Anagrafica: newAnagrafica(c), } - if c.TaxID == nil { - return nil, errors.New("missing customer TaxID") - } - - if c.TaxID.Country == "" { - return nil, errors.New("missing customer TaxID Country Code") - } - - if isCodiceFiscale(c.TaxID) { - da.CodiceFiscale = c.TaxID.Code.String() - } else if isEUCountry(c.TaxID.Country) { - da.IdFiscaleIVA = customerFiscaleIVA(c.TaxID, euCitizenTaxCodeDefault) - } else { - da.IdFiscaleIVA = customerFiscaleIVA(c.TaxID, nonEUCitizenTaxCodeDefault) + if c.TaxID != nil { + if isCodiceFiscale(c.TaxID) { + da.CodiceFiscale = c.TaxID.Code.String() + } else if isEUCountry(c.TaxID.Country) { + da.IdFiscaleIVA = customerFiscaleIVA(c.TaxID, euCitizenTaxCodeDefault) + } else { + da.IdFiscaleIVA = customerFiscaleIVA(c.TaxID, nonEUCitizenTaxCodeDefault) + } } nc.DatiAnagrafici = da - return nc, nil + return nc } func newAnagrafica(party *org.Party) *anagrafica { diff --git a/parties_test.go b/parties_test.go index 5193bcb..8711176 100644 --- a/parties_test.go +++ b/parties_test.go @@ -136,13 +136,13 @@ func TestPartiesCustomer(t *testing.T) { assert.Equal(t, "99999999999", c.DatiAnagrafici.IdFiscaleIVA.IdCodice) }) - t.Run("should return error for missing tax ID Country", func(t *testing.T) { + t.Run("should not fail if missing key data", func(t *testing.T) { env := test.LoadTestFile("invoice-simple.json") test.ModifyInvoice(env, func(inv *bill.Invoice) { inv.Customer.TaxID.Country = "" }) _, err := test.ConvertFromGOBL(env) - require.Error(t, err) + require.NoError(t, err) }) }