xrechnungimporter to accept filename and inputstring, zugferdimporter to accept xml files

This commit is contained in:
jstaerk
2024-03-31 20:20:51 +02:00
parent 3c5d8d5b46
commit 15122e809d
5 changed files with 101 additions and 18 deletions

View File

@@ -111,10 +111,10 @@ public class Contact implements IZUGFeRDExportableContact {
Node currentItemNode = nodes.item(nodeIndex);
if (currentItemNode.getLocalName() != null) {
if (currentItemNode.getLocalName().equals("PersonName")) {
if (currentItemNode.getLocalName().equals("PersonName")/*CII*/||currentItemNode.getLocalName().equals("Name")/*UBL*/) {
setName(currentItemNode.getFirstChild().getNodeValue());
}
if (currentItemNode.getLocalName().equals("TelephoneUniversalCommunication")) {
if (currentItemNode.getLocalName().equals("TelephoneUniversalCommunication")) { /*CII*/
NodeList tel = currentItemNode.getChildNodes();
for (int telChildIndex = 0; telChildIndex < tel.getLength(); telChildIndex++) {
if (tel.item(telChildIndex).getLocalName() != null) {
@@ -123,8 +123,10 @@ public class Contact implements IZUGFeRDExportableContact {
}
}
}
} else if (currentItemNode.getLocalName().equals("Telephone")) { /* UBL */
setPhone(currentItemNode.getTextContent());
}
if (currentItemNode.getLocalName().equals("EmailURIUniversalCommunication")) {
if (currentItemNode.getLocalName().equals("EmailURIUniversalCommunication")) { /* CII */
NodeList email = currentItemNode.getChildNodes();
for (int emailChildIndex = 0; emailChildIndex < email.getLength(); emailChildIndex++) {
if (email.item(emailChildIndex).getLocalName() != null) {
@@ -133,6 +135,8 @@ public class Contact implements IZUGFeRDExportableContact {
}
}
}
} else if (currentItemNode.getLocalName().equals("ElectronicMail")) { /* UBL */
setEMail(currentItemNode.getTextContent());
}
}
}

View File

@@ -1,6 +1,12 @@
package org.mustangproject.ZUGFeRD;
import org.mustangproject.XMLTools;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -18,4 +24,31 @@ public class XRechnungImporter extends ZUGFeRDImporter {
}
}
public XRechnungImporter(String filename) {
super();
try {
setRawXML(Files.readAllBytes(Paths.get(filename)));
containsMeta = true;
} catch (final IOException e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
throw new ZUGFeRDExportException(e);
}
}
public XRechnungImporter(InputStream fileinput) {
super();
try {
setRawXML(XMLTools.getBytesFromStream(fileinput));
containsMeta = true;
} catch (final IOException e) {
Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e);
throw new ZUGFeRDExportException(e);
}
}
}

View File

@@ -14,6 +14,7 @@ package org.mustangproject.ZUGFeRD;
* @author jstaerk
*/
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -49,6 +50,7 @@ import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
import org.mustangproject.EStandard;
import org.mustangproject.Item;
import org.mustangproject.Product;
import org.mustangproject.XMLTools;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -106,9 +108,18 @@ public class ZUGFeRDImporter {
/**
* Extracts a ZUGFeRD invoice from a PDF document represented by an input stream. Errors are reported via exception handling.
*
* @param pdfStream a inputstream of a pdf file
* @param inStream a inputstream of a pdf file
*/
private void extractLowLevel(InputStream pdfStream) throws IOException {
private void extractLowLevel(InputStream inStream) throws IOException {
BufferedInputStream pdfStream=new BufferedInputStream(inStream);
byte[] pad = new byte[4];
pdfStream.mark(0);
pdfStream.read(pad);
pdfStream.reset();
byte[] pdfSignature = { '%', 'P', 'D', 'F' };
if (pad.equals(pdfSignature)) { // we have a pdf
try (PDDocument doc = PDDocument.load(pdfStream)) {
// PDDocumentInformation info = doc.getDocumentInformation();
final PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
@@ -142,6 +153,12 @@ public class ZUGFeRDImporter {
}
}
}
} else {
// no PDF probably XML
containsMeta = true;
setRawXML(XMLTools.getBytesFromStream(pdfStream));
}
}

View File

@@ -125,10 +125,16 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
number = extractString("//*[local-name()=\"Invoice\"]/*[local-name()=\"ID\"]").trim();
issueDate=new SimpleDateFormat("yyyy-MM-dd")
.parse(extractString("//*[local-name()=\"Invoice\"]/*[local-name()=\"IssueDate\"]").trim());
String dueDt=extractString("//*[local-name()=\"Invoice\"]/*[local-name()=\"DueDate\"]").trim();
if (dueDt.length()>0) {
dueDate=new SimpleDateFormat("yyyy-MM-dd")
.parse(extractString("//*[local-name()=\"Invoice\"]/*[local-name()=\"DueDate\"]").trim());
.parse(dueDt);
}
String deliveryDt=extractString("//*[local-name()=\"Delivery\"]/*[local-name()=\"ActualDeliveryDate\"]").trim();
if (deliveryDt.length()>0) {
deliveryDate=new SimpleDateFormat("yyyy-MM-dd")
.parse(extractString("//*[local-name()=\"Delivery\"]/*[local-name()=\"ActualDeliveryDate\"]").trim());
.parse(deliveryDt);
}
}
xpr = xpath.compile("//*[local-name()=\"ApplicableHeaderTradeDelivery\"]");
NodeList headerTradeDeliveryNodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET);

View File

@@ -173,6 +173,29 @@ public class ZF2ZInvoiceImporterTest extends ResourceCase {
assertEquals("recipient@test.org", invoice.getRecipient().getEmail());
assertEquals("28934", invoice.getBuyerOrderReferencedDocumentID());
}
public void testEdgeInvoiceImportUBL() {
File UBLinputFile = getResourceAsFile("01.01a-INVOICE_ubl.xml");
boolean hasExceptions = false;
ZUGFeRDInvoiceImporter zii = null;
Invoice invoice = null;
try {
zii = new ZUGFeRDInvoiceImporter(new FileInputStream(UBLinputFile));
invoice = zii.extractInvoice();
} catch (XPathExpressionException | ParseException | FileNotFoundException e) {
hasExceptions = true;
}
assertFalse(hasExceptions);
// Reading ZUGFeRD
assertEquals(new BigDecimal("288.79"), invoice.getZFItems()[0].getPrice());
assertEquals("04011000-12345-03", invoice.getReferenceNumber());
assertEquals("seller@email.de", invoice.getSender().getContact().getEMail());
assertEquals("12345", invoice.getRecipient().getZIP());
invoice.getPaymentTerms().get
assertEquals("DE75512108001245126199", invoice.getDeliveryAddress().getBankDetails().getIBAN());
}
public void testZF1Import() {