-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparty.go
More file actions
437 lines (375 loc) · 11.3 KB
/
Copy pathparty.go
File metadata and controls
437 lines (375 loc) · 11.3 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package ubl
import (
"fmt"
"strconv"
"strings"
"github.com/invopop/gobl/catalogues/iso"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/l10n"
"github.com/invopop/gobl/org"
)
// SchemeIDEmail is the EAS codelist value for email
const SchemeIDEmail = "EM"
// TaxSchemeVAT is the tax scheme code for VAT
const TaxSchemeVAT = "VAT"
// SupplierParty represents the supplier party in a transaction
type SupplierParty struct {
Party *Party `xml:"cac:Party"`
}
// CustomerParty represents the customer party in a transaction
type CustomerParty struct {
Party *Party `xml:"cac:Party"`
}
// ServiceProviderParty represents a service provider party in a transaction
type ServiceProviderParty struct {
Party *Party `xml:"cac:Party"`
}
// Party represents a party involved in a transaction
type Party struct {
EndpointID *EndpointID `xml:"cbc:EndpointID"`
PartyIdentification []Identification `xml:"cac:PartyIdentification"`
PartyName *PartyName `xml:"cac:PartyName"`
PostalAddress *PostalAddress `xml:"cac:PostalAddress"`
PartyTaxScheme []PartyTaxScheme `xml:"cac:PartyTaxScheme"`
PartyLegalEntity *PartyLegalEntity `xml:"cac:PartyLegalEntity"`
Contact *Contact `xml:"cac:Contact"`
ServiceProviderParty *ServiceProviderParty `xml:"cac:ServiceProviderParty"`
}
// EndpointID represents an endpoint identifier
type EndpointID struct {
SchemeID string `xml:"schemeID,attr"`
Value string `xml:",chardata"`
}
// Identification represents an identification
type Identification struct {
ID *IDType `xml:"cbc:ID"`
}
// PartyName represents the name of a party
type PartyName struct {
Name string `xml:"cbc:Name"`
}
// PostalAddress represents a postal address
type PostalAddress struct {
ID *IDType `xml:"cbc:ID,omitempty"`
AddressFormatCode *IDType `xml:"cbc:AddressFormatCode"`
Postbox *string `xml:"cbc:Postbox,omitempty"`
StreetName *string `xml:"cbc:StreetName"`
AdditionalStreetName *string `xml:"cbc:AdditionalStreetName"`
BuildingNumber *string `xml:"cbc:BuildingNumber,omitempty"`
PlotIdentification *string `xml:"cbc:PlotIdentification,omitempty"`
CitySubdivisionName *string `xml:"cbc:CitySubdivisionName,omitempty"`
CityName *string `xml:"cbc:CityName"`
PostalZone *string `xml:"cbc:PostalZone"`
CountrySubentity *string `xml:"cbc:CountrySubentity"`
Region *string `xml:"cbc:Region,omitempty"`
District *string `xml:"cbc:District,omitempty"`
AddressLine []AddressLine `xml:"cac:AddressLine"`
Country *Country `xml:"cac:Country"`
LocationCoordinate *LocationCoordinate `xml:"cac:LocationCoordinate"`
}
// LocationCoordinate represents a location coordinate
type LocationCoordinate struct {
LatitudeDegreesMeasure *string `xml:"cbc:LatitudeDegreesMeasure"`
LatitudeMinutesMeasure *string `xml:"cbc:LatitudeMinutesMeasure"`
LongitudeDegreesMeasure *string `xml:"cbc:LongitudeDegreesMeasure"`
LongitudeMinutesMeasure *string `xml:"cbc:LongitudeMinutesMeasure"`
}
// AddressLine represents a line in an address
type AddressLine struct {
Line string `xml:"cbc:Line"`
}
// Country represents a country
type Country struct {
IdentificationCode string `xml:"cbc:IdentificationCode"`
}
// PartyTaxScheme represents a party's tax scheme
type PartyTaxScheme struct {
CompanyID *IDType `xml:"cbc:CompanyID"`
TaxScheme *TaxScheme `xml:"cac:TaxScheme"`
}
// TaxScheme represents a tax scheme
type TaxScheme struct {
ID IDType `xml:"cbc:ID"`
Name *string `xml:"cbc:Name"`
TaxTypeCode *IDType `xml:"cbc:TaxTypeCode,omitempty"`
}
// PartyLegalEntity represents the legal entity of a party
type PartyLegalEntity struct {
RegistrationName *string `xml:"cbc:RegistrationName"`
CompanyID *IDType `xml:"cbc:CompanyID"`
CompanyLegalForm *string `xml:"cbc:CompanyLegalForm"`
}
// Contact represents contact information
type Contact struct {
ID *string `xml:"cbc:ID"`
Name *string `xml:"cbc:Name"`
Telephone *string `xml:"cbc:Telephone"`
ElectronicMail *string `xml:"cbc:ElectronicMail"`
}
// CountryCode tries to determine the most appropriate tax country code
// for the party.
func (p *Party) CountryCode() string {
if pa := p.PostalAddress; pa != nil {
if c := pa.Country; c != nil {
return c.IdentificationCode
}
}
return ""
}
func newParty(party *org.Party, ctx Context) *Party { //nolint:gocyclo
if party == nil {
return nil
}
p := &Party{
PostalAddress: newAddress(party.Addresses, ctx),
}
// Only add PartyName if name is not empty
if party.Name != "" {
p.PartyName = &PartyName{
Name: party.Name,
}
// Only add PartyLegalEntity if name is not empty
p.PartyLegalEntity = &PartyLegalEntity{
RegistrationName: &party.Name,
}
}
contact := &Contact{}
if tID := party.TaxID; tID != nil && party.TaxID.Code != "" {
code := party.TaxID.String()
// Norwegian VAT numbers require the MVA suffix on the wire
// (PEPPOL-EN16931 NO-R-001), which GOBL normalization may strip.
if tID.Country.Code() == l10n.NO && !strings.HasSuffix(code, "MVA") {
code += "MVA"
}
if ctx.Is(ContextZATCA) {
code = code[2:]
}
id := tID.GetScheme()
if id == cbc.CodeEmpty {
// Peppol default
id = TaxSchemeVAT
}
taxScheme := PartyTaxScheme{
CompanyID: &IDType{Value: code},
TaxScheme: &TaxScheme{
ID: IDType{Value: id.String()},
},
}
p.PartyTaxScheme = []PartyTaxScheme{taxScheme}
// Override the company address's country code
if p.PostalAddress == nil {
p.PostalAddress = new(PostalAddress)
}
p.PostalAddress.Country = &Country{
IdentificationCode: tID.Country.String(),
}
}
if len(party.Emails) > 0 {
contact.ElectronicMail = &party.Emails[0].Address
}
if len(party.Telephones) > 0 {
contact.Telephone = &party.Telephones[0].Number
}
if len(party.People) > 0 {
n := contactName(party.People[0].Name)
if n != "" {
contact.Name = &n
}
}
if contact.Name != nil || contact.Telephone != nil || contact.ElectronicMail != nil {
p.Contact = contact
}
if len(party.Inboxes) > 0 {
ib := party.Inboxes[0]
if ib.Email != "" {
p.EndpointID = &EndpointID{
SchemeID: SchemeIDEmail,
Value: ib.Email,
}
} else if ib.Scheme != "" {
p.EndpointID = &EndpointID{
SchemeID: ib.Scheme.String(),
Value: ib.Code.String(),
}
}
}
if party.Alias != "" {
p.PartyName = &PartyName{
Name: party.Alias,
}
}
if len(party.Identities) > 0 {
// First pass: Handle legal scope identities
// First legal identity goes to PartyLegalEntity.CompanyID
firstLegalIdx := -1
for i, id := range party.Identities {
if id.Scope == org.IdentityScopeLegal {
// Ensure PartyLegalEntity exists before setting CompanyID
if p.PartyLegalEntity == nil {
p.PartyLegalEntity = &PartyLegalEntity{}
}
code := id.Code.String()
p.PartyLegalEntity.CompanyID = &IDType{
Value: code,
}
if s := id.Ext.Get(iso.ExtKeySchemeID).String(); s != "" {
p.PartyLegalEntity.CompanyID.SchemeID = &s
}
firstLegalIdx = i
break
}
}
// Second pass: Handle tax scope identities -> PartyTaxScheme
for _, id := range party.Identities {
if id.Scope == org.IdentityScopeTax {
code := id.Code.String()
taxScheme := PartyTaxScheme{
CompanyID: &IDType{Value: code},
TaxScheme: &TaxScheme{
ID: IDType{Value: id.Type.String()},
},
}
p.PartyTaxScheme = append(p.PartyTaxScheme, taxScheme)
}
}
// Third pass: Handle remaining identities -> PartyIdentification array
// This includes non-scoped identities and additional legal identities after the first
for i, id := range party.Identities {
// Skip the first legal identity (already in CompanyID)
if id.Scope == org.IdentityScopeLegal && i == firstLegalIdx {
continue
}
// Skip tax scope identities (already in PartyTaxScheme)
if id.Scope == org.IdentityScopeTax {
continue
}
// Add to PartyIdentification array
idType := &IDType{
Value: id.Code.String(),
}
if s := id.Ext.Get(iso.ExtKeySchemeID).String(); s != "" {
idType.SchemeID = &s
} else if id.Ext.IsZero() {
// ZATCA has very specific identities that do not
// require an ISO extension and are only described with type
if t := id.Type.String(); t != "" {
idType.SchemeID = &t
}
}
p.PartyIdentification = append(p.PartyIdentification, Identification{
ID: idType,
})
}
}
return p
}
// newDeliveryParty creates a Party structure for delivery parties
// according to UBL rules:
// - UBL-CR-394: A UBL invoice should not include the DeliveryParty PostalAddress
// (it's already in DeliveryLocation)
func newDeliveryParty(party *org.Party) *Party {
if party == nil {
return nil
}
p := &Party{}
hasContent := false
// Only add PartyName if name is not empty
if party.Name != "" {
p.PartyName = &PartyName{
Name: party.Name,
}
// Only add PartyLegalEntity if name is not empty
p.PartyLegalEntity = &PartyLegalEntity{
RegistrationName: &party.Name,
}
hasContent = true
}
// Note: Intentionally NOT including PostalAddress per UBL-CR-394
// The address is already in DeliveryLocation
contact := &Contact{}
if len(party.Emails) > 0 {
contact.ElectronicMail = &party.Emails[0].Address
}
if len(party.Telephones) > 0 {
contact.Telephone = &party.Telephones[0].Number
}
if len(party.People) > 0 {
n := contactName(party.People[0].Name)
if n != "" {
contact.Name = &n
}
}
if contact.Name != nil || contact.Telephone != nil || contact.ElectronicMail != nil {
p.Contact = contact
hasContent = true
}
// Return nil if party would be completely empty to avoid empty XML elements
if !hasContent {
return nil
}
return p
}
func newAddress(addresses []*org.Address, ctx Context) *PostalAddress {
if len(addresses) == 0 {
return nil
}
// Only return the first a
a := addresses[0]
addr := &PostalAddress{}
if a.Street != "" {
l := a.LineOne()
addr.StreetName = &l
}
if a.StreetExtra != "" {
l := a.LineTwo()
addr.AdditionalStreetName = &l
}
if a.Locality != "" {
addr.CityName = &a.Locality
}
if a.Region != "" {
addr.CountrySubentity = &a.Region
}
if a.Code != cbc.CodeEmpty {
code := a.Code.String()
addr.PostalZone = &code
}
if a.Block != "" {
addr.PlotIdentification = &a.Block
}
if a.Country != "" {
addr.Country = &Country{IdentificationCode: string(a.Country)}
}
if a.Coordinates != nil {
lat := strconv.FormatFloat(*a.Coordinates.Latitude, 'f', -1, 64)
lon := strconv.FormatFloat(*a.Coordinates.Longitude, 'f', -1, 64)
addr.LocationCoordinate = &LocationCoordinate{
LatitudeDegreesMeasure: &lat,
LongitudeDegreesMeasure: &lon,
}
}
if ctx.Is(ContextZATCA) {
l := a.LineTwo()
addr.CitySubdivisionName = &l
addr.AdditionalStreetName = nil
addr.BuildingNumber = &a.Number
}
return addr
}
func contactName(n *org.Name) string {
if n == nil {
return ""
}
given := n.Given
surname := n.Surname
if given == "" && surname == "" {
return ""
}
if given == "" {
return surname
}
if surname == "" {
return given
}
return fmt.Sprintf("%s %s", given, surname)
}