forked from invopop/gobl.ubl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice_parse_test.go
More file actions
348 lines (290 loc) · 10.9 KB
/
invoice_parse_test.go
File metadata and controls
348 lines (290 loc) · 10.9 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
package ubl_test
import (
"testing"
ubl "github.com/invopop/gobl.ubl"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/tax"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseInvoiceTypes(t *testing.T) {
t.Run("standard invoice (380)", func(t *testing.T) {
e, err := testParseInvoice("peppol/base-example.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, bill.InvoiceTypeStandard, inv.Type)
assert.Empty(t, inv.Tags)
})
t.Run("credit note (381)", func(t *testing.T) {
e, err := testParseInvoice("peppol/base-creditnote-correction.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, bill.InvoiceTypeCreditNote, inv.Type)
assert.Empty(t, inv.Tags)
})
t.Run("proforma invoice (325)", func(t *testing.T) {
e, err := testParseInvoice("peppol/proforma-invoice.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, bill.InvoiceTypeProforma, inv.Type)
assert.Empty(t, inv.Tags)
})
t.Run("self-billed invoice (389)", func(t *testing.T) {
e, err := testParseInvoice("peppol/self-billed-invoice.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, bill.InvoiceTypeStandard, inv.Type)
assert.True(t, inv.HasTags(tax.TagSelfBilled), "should have self-billed tag")
})
t.Run("partial invoice (326)", func(t *testing.T) {
e, err := testParseInvoice("peppol/partial-invoice.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, bill.InvoiceTypeStandard, inv.Type)
assert.True(t, inv.HasTags(tax.TagPartial), "should have partial tag")
})
t.Run("self-billed credit note (261)", func(t *testing.T) {
e, err := testParseInvoice("peppol/self-billed-creditnote.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, bill.InvoiceTypeCreditNote, inv.Type)
assert.True(t, inv.HasTags(tax.TagSelfBilled), "should have self-billed tag")
})
}
func TestParseInvoiceTags(t *testing.T) {
t.Run("invoice with self-billed tag", func(t *testing.T) {
e, err := testParseInvoice("peppol/self-billed-invoice.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.True(t, inv.HasTags(tax.TagSelfBilled), "should have self-billed tag")
})
t.Run("invoice with partial tag", func(t *testing.T) {
e, err := testParseInvoice("peppol/partial-invoice.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.True(t, inv.HasTags(tax.TagPartial), "should have partial tag")
})
t.Run("credit note with self-billed tag", func(t *testing.T) {
e, err := testParseInvoice("peppol/self-billed-creditnote.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.True(t, inv.HasTags(tax.TagSelfBilled), "should have self-billed tag")
})
t.Run("standard invoice without tags", func(t *testing.T) {
e, err := testParseInvoice("peppol/base-example.xml")
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.False(t, inv.HasTags(tax.TagSelfBilled), "standard invoice should not have self-billed tag")
assert.False(t, inv.HasTags(tax.TagPartial), "standard invoice should not have partial tag")
})
}
func TestParseInvoiceTypeAndTagCombinations(t *testing.T) {
tests := []struct {
name string
filename string
expectedType string
expectedTags []string
}{
{
name: "standard invoice (380)",
filename: "peppol/base-example.xml",
expectedType: string(bill.InvoiceTypeStandard),
expectedTags: nil,
},
{
name: "credit note (381)",
filename: "peppol/base-creditnote-correction.xml",
expectedType: string(bill.InvoiceTypeCreditNote),
expectedTags: nil,
},
{
name: "proforma (325)",
filename: "peppol/proforma-invoice.xml",
expectedType: string(bill.InvoiceTypeProforma),
expectedTags: nil,
},
{
name: "self-billed standard (389)",
filename: "peppol/self-billed-invoice.xml",
expectedType: string(bill.InvoiceTypeStandard),
expectedTags: []string{string(tax.TagSelfBilled)},
},
{
name: "partial standard (326)",
filename: "peppol/partial-invoice.xml",
expectedType: string(bill.InvoiceTypeStandard),
expectedTags: []string{string(tax.TagPartial)},
},
{
name: "self-billed credit note (261)",
filename: "peppol/self-billed-creditnote.xml",
expectedType: string(bill.InvoiceTypeCreditNote),
expectedTags: []string{string(tax.TagSelfBilled)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e, err := testParseInvoice(tt.filename)
require.NoError(t, err)
inv, ok := e.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, tt.expectedType, string(inv.Type), "invoice type mismatch")
if tt.expectedTags == nil {
assert.False(t, inv.HasTags(tax.TagSelfBilled), "should not have self-billed tag")
assert.False(t, inv.HasTags(tax.TagPartial), "should not have partial tag")
} else {
for _, tag := range tt.expectedTags {
assert.True(t, inv.HasTags(cbc.Key(tag)), "missing expected tag: %s", tag)
}
}
})
}
}
func TestParseUUID(t *testing.T) {
t.Run("parses UUID from OIOUBL XML", func(t *testing.T) {
xmlInput := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<cbc:CustomizationID>urn:fdc:oioubl.dk:trns:billing:invoice:3.0</cbc:CustomizationID>
<cbc:ProfileID>urn:fdc:oioubl.dk:bis:billing_with_response:3</cbc:ProfileID>
<cbc:ID>TEST-UUID-001</cbc:ID>
<cbc:UUID>019cde16-3215-75a1-84f9-4d410281281f</cbc:UUID>
<cbc:IssueDate>2026-01-01</cbc:IssueDate>
<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Test Supplier</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Test Customer</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:LegalMonetaryTotal>
<cbc:PayableAmount currencyID="DKK">100.00</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
</Invoice>`)
parsed, err := ubl.Parse(xmlInput)
require.NoError(t, err)
inv, ok := parsed.(*ubl.Invoice)
require.True(t, ok)
assert.Equal(t, "019cde16-3215-75a1-84f9-4d410281281f", inv.UUID)
env, err := inv.Convert()
require.NoError(t, err)
goblInv, ok := env.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, "019cde16-3215-75a1-84f9-4d410281281f", goblInv.UUID.String(),
"UUID from UBL XML should be preserved in GOBL invoice")
})
t.Run("round-trips UUID through OIOUBL30", func(t *testing.T) {
doc, err := testInvoiceFrom("oioubl30-invoice-example.json")
require.NoError(t, err)
require.NotEmpty(t, doc.UUID)
data, err := ubl.Bytes(doc)
require.NoError(t, err)
parsed, err := ubl.Parse(data)
require.NoError(t, err)
inv, ok := parsed.(*ubl.Invoice)
require.True(t, ok)
assert.Equal(t, doc.UUID, inv.UUID, "UUID should survive XML round-trip")
env, err := inv.Convert()
require.NoError(t, err)
goblInv, ok := env.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, doc.UUID, goblInv.UUID.String(),
"UUID should survive full round-trip through GOBL")
})
}
func TestParseCopyIndicator(t *testing.T) {
t.Run("true CopyIndicator is stored in meta", func(t *testing.T) {
xmlInput := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<cbc:ID>TEST-COPY</cbc:ID>
<cbc:CopyIndicator>true</cbc:CopyIndicator>
<cbc:IssueDate>2026-01-01</cbc:IssueDate>
<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Test</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Test</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:LegalMonetaryTotal>
<cbc:PayableAmount currencyID="DKK">100.00</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
</Invoice>`)
parsed, err := ubl.Parse(xmlInput)
require.NoError(t, err)
inv, ok := parsed.(*ubl.Invoice)
require.True(t, ok)
assert.True(t, inv.CopyIndicator)
env, err := inv.Convert()
require.NoError(t, err)
goblInv, ok := env.Extract().(*bill.Invoice)
require.True(t, ok)
assert.Equal(t, "true", goblInv.Meta["copy"],
"CopyIndicator should be preserved as meta key 'copy'")
})
t.Run("false CopyIndicator does not set meta", func(t *testing.T) {
xmlInput := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<cbc:ID>TEST-NO-COPY</cbc:ID>
<cbc:CopyIndicator>false</cbc:CopyIndicator>
<cbc:IssueDate>2026-01-01</cbc:IssueDate>
<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Test</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Test</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:LegalMonetaryTotal>
<cbc:PayableAmount currencyID="DKK">100.00</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
</Invoice>`)
parsed, err := ubl.Parse(xmlInput)
require.NoError(t, err)
inv, ok := parsed.(*ubl.Invoice)
require.True(t, ok)
env, err := inv.Convert()
require.NoError(t, err)
goblInv, ok := env.Extract().(*bill.Invoice)
require.True(t, ok)
_, hasCopy := goblInv.Meta["copy"]
assert.False(t, hasCopy, "false CopyIndicator should not create meta entry")
})
}