-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathCreateInvoiceFromScratchFuncTest.java
337 lines (272 loc) · 12.5 KB
/
CreateInvoiceFromScratchFuncTest.java
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
/*
* Copyright (C) 2014-2025 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.ubl21;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import com.helger.commons.datetime.PDTFactory;
import com.helger.commons.state.ESuccess;
import com.helger.jaxb.GenericJAXBMarshaller;
import com.helger.xml.XMLFactory;
import com.helger.xml.namespace.MapBasedNamespaceContext;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.CustomerPartyType;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.InvoiceLineType;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.ItemType;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.MonetaryTotalType;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.SupplierPartyType;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.TaxCategoryType;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.TaxSchemeType;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.TaxSubtotalType;
import oasis.names.specification.ubl.schema.xsd.commonaggregatecomponents_21.TaxTotalType;
import oasis.names.specification.ubl.schema.xsd.commonbasiccomponents_21.IDType;
import oasis.names.specification.ubl.schema.xsd.commonextensioncomponents_21.ExtensionContentType;
import oasis.names.specification.ubl.schema.xsd.commonextensioncomponents_21.UBLExtensionType;
import oasis.names.specification.ubl.schema.xsd.commonextensioncomponents_21.UBLExtensionsType;
import oasis.names.specification.ubl.schema.xsd.invoice_21.InvoiceType;
/**
* Create a minimal UBL 2.1 invoice from scratch.
*
* @author Philip Helger
*/
public final class CreateInvoiceFromScratchFuncTest
{
@Test
public void testCreateInvoiceFromScratch ()
{
final String sCurrency = "EUR";
// Create domain object
final InvoiceType aInvoice = new InvoiceType ();
// Fill it
aInvoice.setID ("Dummy Invoice number");
aInvoice.setIssueDate (PDTFactory.getCurrentXMLOffsetDateUTC ());
final SupplierPartyType aSupplier = new SupplierPartyType ();
aInvoice.setAccountingSupplierParty (aSupplier);
final CustomerPartyType aCustomer = new CustomerPartyType ();
aInvoice.setAccountingCustomerParty (aCustomer);
final MonetaryTotalType aMT = new MonetaryTotalType ();
aMT.setPayableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aInvoice.setLegalMonetaryTotal (aMT);
final InvoiceLineType aLine = new InvoiceLineType ();
aLine.setID ("1");
final ItemType aItem = new ItemType ();
aLine.setItem (aItem);
aLine.setLineExtensionAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aInvoice.addInvoiceLine (aLine);
// Add some TaxTotal
/**
* <pre>
<cac:TaxSubtotal>
<cbc:TaxableAmount currencyID="EUR">10.00</cbc:TaxableAmount>
<cbc:TaxAmount currencyID="EUR">10.00</cbc:TaxAmount>
<cac:TaxCategory>
<cbc:ID schemeID="UNCL5305" schemeAgencyID="6">Z</cbc:ID>
<cbc:Percent>10.00</cbc:Percent>
<cac:TaxScheme>
<cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">VAT</cbc:ID>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
* </pre>
*/
{
final TaxSubtotalType aTaxSubtotal = new TaxSubtotalType ();
aTaxSubtotal.setTaxableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aTaxSubtotal.setTaxAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
final TaxCategoryType aTaxCategory = new TaxCategoryType ();
final IDType aTCID = new IDType ();
aTCID.setSchemeID ("UNCL5305");
aTCID.setSchemeAgencyID ("6");
aTCID.setValue ("Z");
aTaxCategory.setID (aTCID);
aTaxCategory.setPercent (BigDecimal.TEN);
final TaxSchemeType aTaxScheme = new TaxSchemeType ();
final IDType aTSID = new IDType ();
aTSID.setSchemeID ("UNCL5305");
aTSID.setSchemeAgencyID ("6");
aTSID.setValue ("VAT");
aTaxScheme.setID (aTSID);
aTaxCategory.setTaxScheme (aTaxScheme);
aTaxSubtotal.setTaxCategory (aTaxCategory);
final TaxTotalType aTaxTotal = new TaxTotalType ();
aTaxTotal.setTaxAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aTaxTotal.addTaxSubtotal (aTaxSubtotal);
aInvoice.addTaxTotal (aTaxTotal);
}
// Write to disk
final ESuccess eSuccess = UBL21Marshaller.invoice ().write (aInvoice, new File ("target/dummy-invoice.xml"));
assertTrue (eSuccess.isSuccess ());
}
/**
* This is an example that creates an XML schema compliant invoice <b>AND</b>
* adds an extension as requested in issue #10
*/
@Test
public void testCreateInvoiceFromScratchWithExtension ()
{
final String sCurrency = "EUR";
// Create domain object
final InvoiceType aInvoice = new InvoiceType ();
// Fill it
aInvoice.setID ("Dummy Invoice number");
aInvoice.setIssueDate (PDTFactory.getCurrentXMLOffsetDateUTC ());
final SupplierPartyType aSupplier = new SupplierPartyType ();
aInvoice.setAccountingSupplierParty (aSupplier);
final CustomerPartyType aCustomer = new CustomerPartyType ();
aInvoice.setAccountingCustomerParty (aCustomer);
final MonetaryTotalType aMT = new MonetaryTotalType ();
aMT.setPayableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aInvoice.setLegalMonetaryTotal (aMT);
final InvoiceLineType aLine = new InvoiceLineType ();
aLine.setID ("1");
final ItemType aItem = new ItemType ();
aLine.setItem (aItem);
aLine.setLineExtensionAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aInvoice.addInvoiceLine (aLine);
// Example extension content from issue #10:
/**
* <pre>
<aife:FactureExtension xmlns:aife="urn:AIFE:Facture:Extension">
<aife:CategoryCode>XX</aife:CategoryCode>
</aife:FactureExtension>
* </pre>
*/
final Document aDoc = XMLFactory.newDocument ();
final String sNamespaceURI = "urn:AIFE:Facture:Extension";
final Node eRoot = aDoc.appendChild (aDoc.createElementNS (sNamespaceURI, "FactureExtension"));
final Node eCategoryCode = eRoot.appendChild (aDoc.createElementNS (sNamespaceURI, "CategoryCode"));
eCategoryCode.appendChild (aDoc.createTextNode ("XX"));
// Now add the extension
final UBLExtensionsType aExtensions = new UBLExtensionsType ();
final UBLExtensionType aExtension = new UBLExtensionType ();
final ExtensionContentType aExtensionContent = new ExtensionContentType ();
// Add the root element - NEVER use the whole document!
aExtensionContent.setAny (aDoc.getDocumentElement ());
aExtension.setExtensionContent (aExtensionContent);
aExtensions.addUBLExtension (aExtension);
aInvoice.setUBLExtensions (aExtensions);
// Write to disk ("as is")
ESuccess eSuccess = UBL21Marshaller.invoice ()
.write (aInvoice, new File ("target/dummy-invoice-with-extension.xml"));
assertTrue (eSuccess.isSuccess ());
// doesn't work as expected yet
if (false)
{
// Write to disk with a custom namespace prefix mapping
final GenericJAXBMarshaller <InvoiceType> aBuilder = UBL21Marshaller.invoice ();
final MapBasedNamespaceContext aCtx = (MapBasedNamespaceContext) aBuilder.getNamespaceContext ();
aCtx.addMapping ("aife", sNamespaceURI);
eSuccess = aBuilder.write (aInvoice, new File ("target/dummy-invoice-with-extension-and-ns.xml"));
assertTrue (eSuccess.isSuccess ());
}
}
@Test
public void testCreateInvoiceFromScratchWithEmptyExtensionIssue38 ()
{
final String sCurrency = "EUR";
// Create domain object
final InvoiceType aInvoice = new InvoiceType ();
// Fill it
aInvoice.setID ("Dummy Invoice number");
aInvoice.setIssueDate (PDTFactory.getCurrentXMLOffsetDateUTC ());
final SupplierPartyType aSupplier = new SupplierPartyType ();
aInvoice.setAccountingSupplierParty (aSupplier);
final CustomerPartyType aCustomer = new CustomerPartyType ();
aInvoice.setAccountingCustomerParty (aCustomer);
final MonetaryTotalType aMT = new MonetaryTotalType ();
aMT.setPayableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aInvoice.setLegalMonetaryTotal (aMT);
final InvoiceLineType aLine = new InvoiceLineType ();
aLine.setID ("1");
final ItemType aItem = new ItemType ();
aLine.setItem (aItem);
aLine.setLineExtensionAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aInvoice.addInvoiceLine (aLine);
// Now add the extension
final UBLExtensionsType aExtensions = new UBLExtensionsType ();
final UBLExtensionType aExtension = new UBLExtensionType ();
final ExtensionContentType aExtensionContent = new ExtensionContentType ();
// Extension is empty - not allowed according to XML Schema
aExtension.setExtensionContent (aExtensionContent);
aExtensions.addUBLExtension (aExtension);
aInvoice.setUBLExtensions (aExtensions);
// Write to disk ("as is")
final ESuccess eSuccess = UBL21Marshaller.invoice ()
.setFormattedOutput (true)
.setUseSchema (false)
.write (aInvoice,
new File ("target/dummy-invoice-with-empty-extension.xml"));
assertTrue (eSuccess.isSuccess ());
}
@Test
public void testCreateInvoiceFromScratchNoSecondFractionIssue40 ()
{
final String sCurrency = "EUR";
// Create domain object
final InvoiceType aInvoice = new InvoiceType ();
// Fill it
aInvoice.setID ("Dummy Invoice number");
aInvoice.setIssueDate (PDTFactory.getCurrentXMLOffsetDateUTC ());
aInvoice.setIssueTime (PDTFactory.getCurrentLocalTime ().truncatedTo (ChronoUnit.SECONDS));
final SupplierPartyType aSupplier = new SupplierPartyType ();
aInvoice.setAccountingSupplierParty (aSupplier);
final CustomerPartyType aCustomer = new CustomerPartyType ();
aInvoice.setAccountingCustomerParty (aCustomer);
final MonetaryTotalType aMT = new MonetaryTotalType ();
aMT.setPayableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aInvoice.setLegalMonetaryTotal (aMT);
final InvoiceLineType aLine = new InvoiceLineType ();
aLine.setID ("1");
final ItemType aItem = new ItemType ();
aLine.setItem (aItem);
aLine.setLineExtensionAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aInvoice.addInvoiceLine (aLine);
// Add some TaxTotal
{
final TaxSubtotalType aTaxSubtotal = new TaxSubtotalType ();
aTaxSubtotal.setTaxableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aTaxSubtotal.setTaxAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
final TaxCategoryType aTaxCategory = new TaxCategoryType ();
final IDType aTCID = new IDType ();
aTCID.setSchemeID ("UNCL5305");
aTCID.setSchemeAgencyID ("6");
aTCID.setValue ("Z");
aTaxCategory.setID (aTCID);
aTaxCategory.setPercent (BigDecimal.TEN);
final TaxSchemeType aTaxScheme = new TaxSchemeType ();
final IDType aTSID = new IDType ();
aTSID.setSchemeID ("UNCL5305");
aTSID.setSchemeAgencyID ("6");
aTSID.setValue ("VAT");
aTaxScheme.setID (aTSID);
aTaxCategory.setTaxScheme (aTaxScheme);
aTaxSubtotal.setTaxCategory (aTaxCategory);
final TaxTotalType aTaxTotal = new TaxTotalType ();
aTaxTotal.setTaxAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
aTaxTotal.addTaxSubtotal (aTaxSubtotal);
aInvoice.addTaxTotal (aTaxTotal);
}
// Write to disk
final ESuccess eSuccess = UBL21Marshaller.invoice ()
.write (aInvoice,
new File ("target/dummy-invoice-no-second-fraction.xml"));
assertTrue (eSuccess.isSuccess ());
}
}