-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaddress.go
62 lines (54 loc) · 1.51 KB
/
address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package fatturapa
import (
"regexp"
"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/org"
)
const (
foreignCAP = "00000"
)
var (
provinceRegexp = regexp.MustCompile(`^[A-Z]{2}$`)
)
// Address from IndirizzoType
type Address struct {
Street string `xml:"Indirizzo"` // Street
Number string `xml:"NumeroCivico,omitempty"` // Number
Code string `xml:"CAP"` // Post Code
Locality string `xml:"Comune"` // Locality
Region string `xml:"Provincia,omitempty"` // Region
Country string `xml:"Nazione"` // Country Code
}
func newAddress(addr *org.Address) *Address {
ad := &Address{
Street: addressStreet(addr),
Number: addr.Number,
Locality: addr.Locality,
Region: addressRegion(addr),
Country: addr.Country.String(),
}
if addr.Country == l10n.IT.ISO() {
ad.Code = addr.Code.String()
} else {
ad.Code = foreignCAP
}
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.ISO() {
if provinceRegexp.MatchString(address.Region) {
return address.Region
}
}
return ""
}
func addressStreet(address *org.Address) string {
if address.PostOfficeBox != "" {
return address.PostOfficeBox
}
return address.Street
}