simplest possible UBL despatch advice

This commit is contained in:
jstaerk
2022-05-09 21:38:26 +02:00
parent c4ab3c2b4d
commit aa5944326f
7 changed files with 254 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
package org.mustangproject;
public enum EStandard {
facturx, orderx, deliveradvice, zugferd, cii, ubl
facturx, orderx, despatchadvice, ubldespatchadvice, zugferd, cii, ubl
}

View File

@@ -39,7 +39,7 @@ public class DAPullProvider extends ZUGFeRD2PullProvider implements IXMLProvider
protected IExportableTransaction trans;
private String paymentTermsDescription;
protected Profile profile = Profiles.getByName(EStandard.deliveradvice,"pilot", 1);
protected Profile profile = Profiles.getByName(EStandard.despatchadvice,"pilot", 1);
@Override

View File

@@ -0,0 +1,76 @@
/**
* *********************************************************************
* <p>
* Copyright 2018 Jochen Staerk
* <p>
* Use is subject to license terms.
* <p>
* 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.
* <p>
* 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.
* <p>
* See the License for the specific language governing permissions and
* limitations under the License.
* <p>
* **********************************************************************
*/
package org.mustangproject.ZUGFeRD;
import org.apache.pdfbox.cos.*;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.apache.pdfbox.preflight.PreflightDocument;
import org.apache.pdfbox.preflight.exception.ValidationException;
import org.apache.pdfbox.preflight.parser.PreflightParser;
import org.apache.pdfbox.preflight.utils.ByteArrayDataSource;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.AdobePDFSchema;
import org.apache.xmpbox.schema.DublinCoreSchema;
import org.apache.xmpbox.schema.PDFAIdentificationSchema;
import org.apache.xmpbox.schema.XMPBasicSchema;
import org.apache.xmpbox.type.BadFieldValueException;
import org.apache.xmpbox.xml.XmpSerializer;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.xml.transform.TransformerException;
import java.io.*;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
public class EinLieferscheinExporter implements IExporter {
IXMLProvider xmlProvider;
IExportableTransaction trans;
public EinLieferscheinExporter() {
xmlProvider=new UBLDAPullProvider();
}
@Override
public IExporter setTransaction(IExportableTransaction trans) throws IOException {
this.trans=trans;
return this;
}
@Override
public void export(String ZUGFeRDfilename) throws IOException {
export(new FileOutputStream(new File(ZUGFeRDfilename)));
}
@Override
public void export(OutputStream output) throws IOException {
xmlProvider.generateXML(trans);
byte[] bytes=xmlProvider.getXML();
output.write(bytes, 0, bytes.length);
}
}

View File

@@ -63,7 +63,7 @@ public class Profiles {
throw new RuntimeException("Profile not found");
}
return result;
} else if (standard == EStandard.deliveradvice) {
} else if (standard == EStandard.despatchadvice) {
Profile result = null;
result = dx1Map.get(name.toUpperCase());
if (result == null) {

View File

@@ -0,0 +1,137 @@
/**
* *********************************************************************
* <p>
* Copyright 2018 Jochen Staerk
* <p>
* Use is subject to license terms.
* <p>
* 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.
* <p>
* 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.
* <p>
* See the License for the specific language governing permissions and
* limitations under the License.
* <p>
* **********************************************************************
*/
package org.mustangproject.ZUGFeRD;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.mustangproject.EStandard;
import org.mustangproject.FileAttachment;
import org.mustangproject.XMLTools;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.mustangproject.ZUGFeRD.ZUGFeRDDateFormat.DATE;
import static org.mustangproject.ZUGFeRD.model.DocumentCodeTypeConstants.CORRECTEDINVOICE;
import static org.mustangproject.ZUGFeRD.model.TaxCategoryCodeTypeConstants.CATEGORY_CODES_WITH_EXEMPTION_REASON;
public class UBLDAPullProvider implements IXMLProvider {
protected IExportableTransaction trans;
protected TransactionCalculator calc;
byte[] ublData;
protected Profile profile = Profiles.getByName(EStandard.ubldespatchadvice, "basic", 1);
@Override
public void generateXML(IExportableTransaction trans) {
this.trans = trans;
this.calc = new TransactionCalculator(trans);
final SimpleDateFormat ublDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<DespatchAdvice xmlns=\"urn:oasis:names:specification:ubl:schema:xsd:DespatchAdvice-2\" xmlns:cac=\"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2\" xmlns:cbc=\"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2\" xmlns:cec=\"urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2\" xmlns:csc=\"urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2\">\n" +
" <cbc:ID>" + XMLTools.encodeXML(trans.getNumber()) + "</cbc:ID>\n" +
" <cbc:IssueDate>"+ublDateFormat.format(trans.getIssueDate())+"</cbc:IssueDate>\n" +
" <cac:DespatchSupplierParty/>\n" +
" <cac:DeliveryCustomerParty/>\n";
int i = 1;
for (IZUGFeRDExportableItem item : trans.getZFItems()) {
xml +=
" <cac:DespatchLine>\n" +
" <cbc:ID>" + XMLTools.encodeXML(Integer.toString(i++)) + "</cbc:ID>\n" +
" <cac:OrderLineReference>\n" +
" <cbc:LineID>" + XMLTools.encodeXML(item.getBuyerOrderReferencedDocumentLineID()) + "</cbc:LineID>\n" +
" </cac:OrderLineReference>\n" +
" <cac:Item/>\n" +
" </cac:DespatchLine>\n";
}
xml += "</DespatchAdvice>\n";
final byte[] ublRaw;
try {
ublRaw = xml.getBytes("UTF-8");
ublData = XMLTools.removeBOM(ublRaw);
} catch (final UnsupportedEncodingException e) {
Logger.getLogger(UBLDAPullProvider.class.getName()).log(Level.SEVERE, null, e);
}
}
@Override
public byte[] getXML() {
byte[] res = ublData;
final StringWriter sw = new StringWriter();
Document document = null;
try {
document = DocumentHelper.parseText(new String(ublData));
} catch (final DocumentException e1) {
Logger.getLogger(ZUGFeRD2PullProvider.class.getName()).log(Level.SEVERE, null, e1);
}
try {
final OutputFormat format = OutputFormat.createPrettyPrint();
format.setTrimText(false);
final XMLWriter writer = new XMLWriter(sw, format);
writer.write(document);
res = sw.toString().getBytes(StandardCharsets.UTF_8);
} catch (final IOException e) {
Logger.getLogger(ZUGFeRD2PullProvider.class.getName()).log(Level.SEVERE, null, e);
}
return res;
}
@Override
public void setTest() {
}
@Override
public void setProfile(Profile p) {
profile = p;
}
@Override
public Profile getProfile() {
return profile;
}
}

View File

@@ -324,7 +324,7 @@ public class DXTest extends MustangReaderTestCase implements IExportableTransact
Invoice i = createDA(recipient);
DAPullProvider zf2p = new DAPullProvider();
zf2p.setProfile(Profiles.getByName(EStandard.deliveradvice,"Pilot",1));
zf2p.setProfile(Profiles.getByName(EStandard.despatchadvice,"Pilot",1));
zf2p.generateXML(i);
String theXML = new String(zf2p.getXML(), StandardCharsets.UTF_8);
try {

View File

@@ -21,16 +21,24 @@
*/
package org.mustangproject.ZUGFeRD;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.mustangproject.*;
import org.mustangproject.CII.CIIToUBL;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UBLTest extends ResourceCase {
final String TARGET_XML = "./target/testout-1Lieferschein.xml";
public void testUBLBasic() {
@@ -47,11 +55,39 @@ public class UBLTest extends ResourceCase {
expected = ResourceUtilities.readFile(StandardCharsets.UTF_8, expectedFile.getAbsolutePath()).replaceAll("\r\n", "\n");
result = ResourceUtilities.readFile(StandardCharsets.UTF_8, tempFile.getAbsolutePath()).replaceAll("\r\n", "\n");
} catch (final IOException e) {
fail("Exception should not happen: "+e.getMessage());
fail("Exception should not happen: " + e.getMessage());
}
assertNotNull(result);
assertEquals(expected, result);
}
public void test1Lieferschein() {
EinLieferscheinExporter oe = new EinLieferscheinExporter();
Invoice i = new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date())
.setSender(new TradeParty("Test company", "teststr", "55232", "teststadt", "DE").addTaxID("DE4711").addVATID("DE0815").setContact(new Contact("Hans Test", "+49123456789", "test@example.org")).addBankDetails(new BankDetails("DE12500105170648489890", "COBADEFXXX")))
.setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE"))
.setReferenceNumber("991-01484-64")//leitweg-id
.setNumber("123").addItem(new Item(new Product("Testprodukt", "", "C62", BigDecimal.ZERO), /*price*/ new BigDecimal("1.0"), /*qty*/ new BigDecimal("1.0")).addReferencedLineID("A12"));
UBLDAPullProvider zf2p = new UBLDAPullProvider();
zf2p.setProfile(Profiles.getByName(EStandard.despatchadvice, "Pilot", 1));
zf2p.generateXML(i);
try {
oe.setTransaction(i);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
oe.export(baos);
String theXML = baos.toString("UTF-8");
assertTrue(theXML.contains("<DespatchAdvice"));
Files.write(Paths.get(TARGET_XML), theXML.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
}