Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring address number handling so no longer required #20

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
8 changes: 3 additions & 5 deletions fatturapa.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ 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
}

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 {
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
14 changes: 4 additions & 10 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ type fatturaElettronicaHeader struct {
CessionarioCommittente *customer `xml:",omitempty"`
}

func newFatturaElettronicaHeader(inv *bill.Invoice, datiTrasmissione *datiTrasmissione) (*fatturaElettronicaHeader, error) {
supplier, err := newCedentePrestatore(inv)
if err != nil {
return nil, err
}
customer, err := newCessionarioCommittente(inv)
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
}
}
63 changes: 24 additions & 39 deletions parties.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,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 +68,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 {
ns := &supplier{
DatiAnagrafici: &datiAnagrafici{
IdFiscaleIVA: &taxID{
IdPaese: s.TaxID.Country.String(),
Expand All @@ -90,44 +78,41 @@ 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
}

func newCessionarioCommittente(inv *bill.Invoice) (*customer, error) {
c := inv.Customer
func newCessionarioCommittente(c *org.Party) *customer {
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{
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 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)
}
}

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 &customer{
DatiAnagrafici: da,
Sede: address,
}, nil
return nc
}

func newAnagrafica(party *org.Party) *anagrafica {
Expand Down
10 changes: 6 additions & 4 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 Expand Up @@ -134,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)
})
}
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