-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparty.go
More file actions
309 lines (282 loc) · 8.85 KB
/
Copy pathparty.go
File metadata and controls
309 lines (282 loc) · 8.85 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package cii
import (
"fmt"
"slices"
"github.com/invopop/gobl/addons/fr/choruspro"
"github.com/invopop/gobl/catalogues/iso"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/regimes/fr"
"github.com/invopop/gobl/tax"
)
// Party defines the structure of the TradePartyType of the CII standard
type Party struct {
ID []*PartyID `xml:"ram:ID,omitempty"`
GlobalID []*PartyID `xml:"ram:GlobalID,omitempty"`
Name string `xml:"ram:Name,omitempty"`
Description string `xml:"ram:Description,omitempty"`
LegalOrganization *LegalOrganization `xml:"ram:SpecifiedLegalOrganization,omitempty"`
Contact *Contact `xml:"ram:DefinedTradeContact,omitempty"`
PostalTradeAddress *PostalTradeAddress `xml:"ram:PostalTradeAddress,omitempty"`
URIUniversalCommunication *URIUniversalCommunication `xml:"ram:URIUniversalCommunication,omitempty"`
SpecifiedTaxRegistration []*SpecifiedTaxRegistration `xml:"ram:SpecifiedTaxRegistration,omitempty"`
}
// PartyID defines the structure of the ID of the CII standard
type PartyID struct {
SchemeID string `xml:"schemeID,attr,omitempty"`
Value string `xml:",chardata"`
}
// PostalTradeAddress defines the structure of the PostalTradeAddress of the CII standard
type PostalTradeAddress struct {
Postcode string `xml:"ram:PostcodeCode,omitempty"`
LineOne string `xml:"ram:LineOne,omitempty"`
LineTwo string `xml:"ram:LineTwo,omitempty"`
City string `xml:"ram:CityName,omitempty"`
CountryID string `xml:"ram:CountryID"`
Region string `xml:"ram:CountrySubDivisionName,omitempty"`
}
// URIUniversalCommunication defines the structure of URIUniversalCommunication of the CII standard
type URIUniversalCommunication struct {
ID *PartyID `xml:"ram:URIID"`
}
// SpecifiedTaxRegistration defines the structure of the SpecifiedTaxRegistration of the CII standard
type SpecifiedTaxRegistration struct {
ID *PartyID `xml:"ram:ID"`
}
// LegalOrganization defines the structure of the SpecifiedLegalOrganization of the CII standard
type LegalOrganization struct {
ID *PartyID `xml:"ram:ID"`
Name string `xml:"ram:TradingBusinessName,omitempty"`
}
// Contact defines the structure of the DefinedTradeContact of the CII standard
type Contact struct {
PersonName string `xml:"ram:PersonName,omitempty"`
Department string `xml:"ram:DepartmentName,omitempty"`
Phone *PhoneNumber `xml:"ram:TelephoneUniversalCommunication,omitempty"`
Email *Email `xml:"ram:EmailURIUniversalCommunication,omitempty"`
}
// PhoneNumber defines the structure of the TelephoneUniversalCommunication of the CII standard
type PhoneNumber struct {
CompleteNumber string `xml:"ram:CompleteNumber,omitempty"`
}
// Email defines the structure of the EmailURIUniversalCommunication of the CII standard
type Email struct {
URIID string `xml:"ram:URIID,omitempty"`
}
const (
// SchemeIDEmail represents the Scheme ID for email addresses
SchemeIDEmail = "EM"
// SchemeIDVAT represents a VAT registration (BT-31, BT-48, BT-63)
SchemeIDVAT = "VA"
// SchemeIDTaxRegistration represents a non-VAT tax registration (BT-32)
SchemeIDTaxRegistration = "FC"
)
// newParty creates the SellerTradeParty part of a EN 16931 compliant invoice
func newParty(party *org.Party, ctx Context) *Party {
if party == nil {
return nil
}
p := &Party{
Name: party.Name,
Contact: newContact(party),
PostalTradeAddress: newPostalTradeAddress(party.Addresses),
}
// BT-28/BT-45: Trading name (alias)
if party.Alias != "" {
p.LegalOrganization = &LegalOrganization{
Name: party.Alias,
}
}
if party.TaxID != nil && party.TaxID.Code != "" {
// BT-31/BT-48: VAT identifier, other registrations come from the identities below
p.SpecifiedTaxRegistration = append(p.SpecifiedTaxRegistration, &SpecifiedTaxRegistration{
ID: &PartyID{
Value: party.TaxID.String(),
SchemeID: mapGOBLTaxIDScheme(party.TaxID),
},
})
// Override address country from tax ID (authoritative source)
if party.TaxID.Country != "" {
if p.PostalTradeAddress == nil {
p.PostalTradeAddress = new(PostalTradeAddress)
}
p.PostalTradeAddress.CountryID = party.TaxID.Country.String()
}
}
if len(party.Identities) > 0 {
for _, id := range party.Identities {
// BT-30/BT-47: Legal registration identifier
if id.Scope == org.IdentityScopeLegal {
if p.LegalOrganization == nil {
p.LegalOrganization = &LegalOrganization{}
}
p.LegalOrganization.ID = &PartyID{
Value: id.Code.String(),
}
if id.Ext.Has(iso.ExtKeySchemeID) {
p.LegalOrganization.ID.SchemeID = id.Ext.Get(iso.ExtKeySchemeID).String()
}
continue
}
// BT-32: Tax registration identifier, "FC" required by BR-E-02, BR-Z-02, BR-AE-02
if id.Scope == org.IdentityScopeTax {
p.SpecifiedTaxRegistration = append(p.SpecifiedTaxRegistration, &SpecifiedTaxRegistration{
ID: &PartyID{
Value: id.Code.String(),
SchemeID: SchemeIDTaxRegistration,
},
})
continue
}
// GlobalID: identity with scheme ID and no scope
if id.Ext.Has(iso.ExtKeySchemeID) {
p.GlobalID = append(p.GlobalID, &PartyID{
SchemeID: id.Ext.Get(iso.ExtKeySchemeID).String(),
Value: id.Code.String(),
})
continue
}
// BT-29/BT-46: Seller/Buyer identifier (no scheme, no scope)
p.ID = append(p.ID, &PartyID{
Value: id.Code.String(),
})
}
}
if slices.Contains(ctx.Addons, choruspro.V1) {
applyChorusPro(party, p)
}
p.URIUniversalCommunication = newURIUniversalCommunication(party.Inboxes)
if p.LegalOrganization != nil && p.LegalOrganization.ID != nil && p.LegalOrganization.ID.Value == "" {
p.LegalOrganization.ID = nil
}
return p
}
// chorusProLegalOrgID returns the LegalOrganization ID for Chorus Pro based on the
// scheme extension value, which determines the type of identifier to use.
// applyChorusPro sets Chorus Pro-specific fields on the CII party. It suppresses
// the GlobalID (which is not used in Chorus Pro) and sets the LegalOrganization ID
// to the identifier required for the given scheme type.
func applyChorusPro(party *org.Party, p *Party) {
p.GlobalID = nil
if p.LegalOrganization == nil {
p.LegalOrganization = &LegalOrganization{}
}
p.LegalOrganization.ID = chorusProLegalOrgID(party)
}
func chorusProLegalOrgID(party *org.Party) *PartyID {
scheme := party.Ext.Get(choruspro.ExtKeyScheme)
pid := &PartyID{
SchemeID: scheme.String(),
}
switch scheme {
case "1":
// SIRET: identity with type SIRET
for _, id := range party.Identities {
if id.Type == fr.IdentityTypeSIRET {
pid.Value = id.Code.String()
return pid
}
}
case "2":
// Intra-community VAT number
if party.TaxID != nil {
pid.Value = party.TaxID.String()
}
case "3", "6":
// Country code + first 16 characters of company name
if party.TaxID != nil && party.TaxID.Country != "" {
name := []rune(party.Name)
if len(name) > 16 {
name = name[:16]
}
pid.Value = party.TaxID.Country.String() + string(name)
}
case "4", "5":
// RIDET / Tahiti: identity with scope legal
for _, id := range party.Identities {
if id.Scope == org.IdentityScopeLegal {
pid.Value = id.Code.String()
return pid
}
}
}
return pid
}
func mapGOBLTaxIDScheme(id *tax.Identity) string {
s := id.GetScheme()
switch s {
case tax.CategoryVAT:
return SchemeIDVAT
default:
// TODO: cover more versions here.
return s.String()
}
}
func newContact(p *org.Party) *Contact {
c := new(Contact)
if len(p.People) > 0 {
pp := p.People[0]
c.PersonName = contactName(pp.Name)
if len(pp.Telephones) > 0 {
c.Phone = &PhoneNumber{
CompleteNumber: pp.Telephones[0].Number,
}
}
if len(pp.Emails) > 0 {
c.Email = &Email{
URIID: pp.Emails[0].Address,
}
}
c.Department = pp.Role
}
if c.Phone == nil && len(p.Telephones) > 0 {
c.Phone = &PhoneNumber{
CompleteNumber: p.Telephones[0].Number,
}
}
// Fallback to adding the base company email addresses
if c.Email == nil && len(p.Emails) > 0 {
c.Email = &Email{
URIID: p.Emails[0].Address,
}
}
if c.PersonName == "" && c.Email == nil && c.Phone == nil {
return nil
}
return c
}
func contactName(p *org.Name) string {
g := p.Given
s := p.Surname
if g == "" && s == "" {
return ""
}
if g == "" {
return s
}
if s == "" {
return g
}
return fmt.Sprintf("%s %s", g, s)
}
func newURIUniversalCommunication(inboxes []*org.Inbox) *URIUniversalCommunication {
if len(inboxes) == 0 {
return nil
}
ib := inboxes[0]
if ib.Email != "" {
return &URIUniversalCommunication{
ID: &PartyID{
Value: ib.Email,
SchemeID: SchemeIDEmail,
},
}
} else if ib.Code != "" {
return &URIUniversalCommunication{
ID: &PartyID{
Value: ib.Code.String(),
SchemeID: ib.Scheme.String(),
},
}
}
return nil
}