Skip to content

Commit

Permalink
Only include two-letter province texts
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Jun 27, 2024
1 parent 77fbff9 commit cd39338
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
21 changes: 20 additions & 1 deletion address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fatturapa

import (
"regexp"

"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/org"
)
Expand All @@ -9,6 +11,10 @@ const (
foreignCAP = "00000"
)

var (
provinceRegexp = regexp.MustCompile(`^[A-Z]{2}$`)
)

// address from IndirizzoType
type address struct {
Indirizzo string // Street
Expand All @@ -24,7 +30,7 @@ func newAddress(addr *org.Address) *address {
Indirizzo: addressStreet(addr),
NumeroCivico: addr.Number,
Comune: addr.Locality,
Provincia: addr.Region,
Provincia: addressRegion(addr),
Nazione: addr.Country.String(),
}
if addr.Country == l10n.IT {
Expand All @@ -35,6 +41,19 @@ func newAddress(addr *org.Address) *address {
return ad
}

// addressRegion will simply check if the region is using the
// standard two digital capital letter code for the Italian province,
// or return an empty string to avoid FatturaPA validation issues.
// The province is optional, so it's not a problem if it's not set.
func addressRegion(address *org.Address) string {
if address.Country == l10n.IT {
if provinceRegexp.MatchString(address.Region) {
return address.Region
}
}
return ""
}

func addressStreet(address *org.Address) string {
if address.PostOfficeBox != "" {
return address.PostOfficeBox
Expand Down
53 changes: 53 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package fatturapa

import (
"testing"

"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/org"
"github.com/stretchr/testify/assert"
)

func TestAddressRegion(t *testing.T) {
t.Run("should return the region two letter code", func(t *testing.T) {
addr := &org.Address{
Number: "1",
Street: "Via Roma",
Locality: "Roma",
Region: "RM",
Code: "00100",
Country: l10n.IT,
}

out := newAddress(addr)
assert.Equal(t, "RM", out.Provincia)
})

t.Run("should ignore text name", func(t *testing.T) {
addr := &org.Address{
Number: "1",
Street: "Via Roma",
Locality: "Roma",
Region: "Rome",
Code: "00100",
Country: l10n.IT,
}

out := newAddress(addr)
assert.Empty(t, out.Provincia)
})

t.Run("should ignore foreign addresses", func(t *testing.T) {
addr := &org.Address{
Number: "2",
Street: "Rome Street",
Locality: "London",
Region: "RM",
Code: "00100",
Country: l10n.GB,
}

out := newAddress(addr)
assert.Empty(t, out.Provincia)
})
}

0 comments on commit cd39338

Please sign in to comment.