Skip to content

Commit

Permalink
Refactoring number handling so no longer required
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Sep 21, 2023
1 parent bdbadf0 commit 3dbf85d
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 79 deletions.
52 changes: 17 additions & 35 deletions address.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion fatturapa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
14 changes: 4 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
41 changes: 17 additions & 24 deletions parties.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {

Check failure on line 73 in parties.go

View workflow job for this annotation

GitHub Actions / golangci-lint

newCedentePrestatore - result 1 (error) is always nil (unparam)
ns := &supplier{
DatiAnagrafici: &datiAnagrafici{
IdFiscaleIVA: &taxID{
IdPaese: s.TaxID.Country.String(),
Expand All @@ -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{
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions parties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 3dbf85d

Please sign in to comment.