-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparties.go
More file actions
131 lines (112 loc) · 2.93 KB
/
Copy pathparties.go
File metadata and controls
131 lines (112 loc) · 2.93 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package convert
import (
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/org"
)
const (
idTypeCodeTaxID = "02"
)
var idTypeCodeMap = map[cbc.Key]string{
org.IdentityKeyPassport: "03",
org.IdentityKeyForeign: "04",
org.IdentityKeyResident: "05",
org.IdentityKeyOther: "06",
}
// Sujetos contains invoice parties info
type Sujetos struct {
Emisor *Emisor
Destinatarios *Destinatarios
EmitidaPorTercerosODestinatario string
}
// Emisor contains info about the invoice supplier
type Emisor struct {
NIF string
ApellidosNombreRazonSocial string
}
// Destinatarios contains info about the invoice customers,
// Tickebai allows up to 100 customers but GOBL only allows one
// per invoice
type Destinatarios struct {
IDDestinatario []*IDDestinatario
}
// IDDestinatario contains info about a single customer
type IDDestinatario struct {
NIF string `xml:",omitempty"`
IDOtro *IDOtro `xml:",omitempty"`
ApellidosNombreRazonSocial string
CodigoPostal string `xml:",omitempty"`
Direccion string `xml:",omitempty"`
}
// IDOtro identifies a customer for a contry other than Spain
type IDOtro struct {
CodigoPais string `xml:",omitempty"`
IDType string
ID string
}
func newEmisor(party *org.Party) *Emisor {
return &Emisor{
NIF: party.TaxID.Code.String(),
ApellidosNombreRazonSocial: party.Name,
}
}
func newDestinatario(party *org.Party) *IDDestinatario {
d := &IDDestinatario{
ApellidosNombreRazonSocial: party.Name,
}
if party.TaxID != nil && party.TaxID.Country == "ES" && party.TaxID.Code != "" {
d.NIF = party.TaxID.Code.String()
} else {
d.IDOtro = otherIdentity(party)
}
if d.NIF == "" && d.IDOtro == nil {
// Assume this is a B2C operation.
return nil
}
if len(party.Addresses) > 0 && party.Addresses[0].Code != "" {
d.CodigoPostal = party.Addresses[0].Code.String()
d.Direccion = formatAddress(party.Addresses[0])
}
return d
}
func otherIdentity(party *org.Party) *IDOtro {
oid := new(IDOtro)
if party.TaxID != nil {
oid.CodigoPais = party.TaxID.Country.String()
}
if party.TaxID != nil && party.TaxID.Code != "" {
oid.IDType = idTypeCodeTaxID
oid.ID = party.TaxID.Code.String()
return oid
}
for _, id := range party.Identities {
if id == nil || id.Code == "" {
continue
}
it, ok := idTypeCodeMap[id.Key]
if !ok {
continue
}
oid.IDType = it
oid.ID = id.Code.String()
if id.Country != "" {
oid.CodigoPais = id.Country.String()
}
return oid
}
return nil
}
func partyCountry(party *org.Party) l10n.TaxCountryCode {
if party == nil {
return ""
}
if party.TaxID != nil && party.TaxID.Country != "" {
return party.TaxID.Country
}
for _, id := range party.Identities {
if id != nil && id.Country != "" {
return l10n.TaxCountryCode(id.Country)
}
}
return ""
}