diff --git a/library/src/main/java/org/mustangproject/Contact.java b/library/src/main/java/org/mustangproject/Contact.java index 4ecc1b6a..f690d0d8 100644 --- a/library/src/main/java/org/mustangproject/Contact.java +++ b/library/src/main/java/org/mustangproject/Contact.java @@ -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()); } } } diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/XRechnungImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/XRechnungImporter.java index 0855b919..069c6e57 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/XRechnungImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/XRechnungImporter.java @@ -1,21 +1,54 @@ 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; public class XRechnungImporter extends ZUGFeRDImporter { - public XRechnungImporter(byte[] rawXml) { - super(); + public XRechnungImporter(byte[] rawXml) { + super(); - try { - setRawXML(rawXml); - containsMeta = true; - } catch (final IOException e) { - Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); - throw new ZUGFeRDExportException(e); + try { + setRawXML(rawXml); + containsMeta = true; + } catch (final IOException e) { + Logger.getLogger(ZUGFeRDImporter.class.getName()).log(Level.SEVERE, null, e); + throw new ZUGFeRDExportException(e); + } } - } + + 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); + } + + + } + } diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index d637eece..5c2a38ce 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -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)); + + } } diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java index 6c939908..0e29aa06 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java @@ -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()); - dueDate=new SimpleDateFormat("yyyy-MM-dd") - .parse(extractString("//*[local-name()=\"Invoice\"]/*[local-name()=\"DueDate\"]").trim()); - deliveryDate=new SimpleDateFormat("yyyy-MM-dd") - .parse(extractString("//*[local-name()=\"Delivery\"]/*[local-name()=\"ActualDeliveryDate\"]").trim()); + String dueDt=extractString("//*[local-name()=\"Invoice\"]/*[local-name()=\"DueDate\"]").trim(); + if (dueDt.length()>0) { + dueDate=new SimpleDateFormat("yyyy-MM-dd") + .parse(dueDt); + } + String deliveryDt=extractString("//*[local-name()=\"Delivery\"]/*[local-name()=\"ActualDeliveryDate\"]").trim(); + if (deliveryDt.length()>0) { + deliveryDate=new SimpleDateFormat("yyyy-MM-dd") + .parse(deliveryDt); + } } xpr = xpath.compile("//*[local-name()=\"ApplicableHeaderTradeDelivery\"]"); NodeList headerTradeDeliveryNodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET); diff --git a/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java b/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java index 154b5990..d9bda954 100644 --- a/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java +++ b/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java @@ -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() {