diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index 722167bd..fec66407 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -374,6 +374,9 @@ public class ZUGFeRDImporter extends ZUGFeRDInvoiceImporter { if (settlement instanceof IZUGFeRDTradeSettlementDebit) { return ((IZUGFeRDTradeSettlementDebit) settlement).getIBAN(); } + if (settlement instanceof IZUGFeRDTradeSettlementPayment) { + return ((IZUGFeRDTradeSettlementPayment) settlement).getOwnIBAN(); + } } return null; } @@ -401,7 +404,7 @@ public class ZUGFeRDImporter extends ZUGFeRDInvoiceImporter { * @return when the payment is due */ public String getDueDate() { - SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); return sdf.format(importedInvoice.getDueDate()); } diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java index cdd8ba1e..e46a57f6 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java @@ -62,11 +62,15 @@ public class ZUGFeRDInvoiceImporter { * parsed Document */ protected Document document; + /*** + * automatically parse into importedInvoice + */ + protected boolean parseAutomatically = true; protected Integer version; - protected Invoice importedInvoice=null; + protected Invoice importedInvoice = null; protected boolean recalcPrice = false; protected boolean ignoreCalculationErrors = false; - protected ArrayList fileAttachments=new ArrayList<>(); + protected ArrayList fileAttachments = new ArrayList<>(); protected ZUGFeRDInvoiceImporter() { @@ -93,7 +97,6 @@ public class ZUGFeRDInvoiceImporter { } - /*** * return the file names of all files embedded into the PDF * @see for XML embedded files please use ZUGFeRDInvoiceImporter.getFileAttachmentsXML @@ -104,7 +107,6 @@ public class ZUGFeRDInvoiceImporter { } - /** * Extracts a ZUGFeRD invoice from a PDF document represented by an input stream. Errors are reported via exception handling. * @@ -165,8 +167,6 @@ public class ZUGFeRDInvoiceImporter { } - - private void extractFiles(Map names) throws IOException { for (final String alias : names.keySet()) { @@ -199,16 +199,35 @@ public class ZUGFeRDInvoiceImporter { } } - public void setRawXML(byte[] rawXML) throws IOException { + /*** + * set the xml of a CII invoice + * @param rawXML the xml string + * @param doParse automatically parse input for zugferdImporter (not ZUGFeRDInvoiceImporter) + * @throws IOException + */ + public void setRawXML(byte[] rawXML, boolean doParse) throws IOException { this.containsMeta = true; this.rawXML = rawXML; this.version = null; + + parseAutomatically = doParse; + try { setDocument(); } catch (ParserConfigurationException | SAXException e) { LOGGER.error("Failed to parse XML", e); throw new ZUGFeRDExportException(e); } + + } + + /*** + * set the xml of a CII invoice, simple version + * @param rawXML the cii(?) as a string + * @throws IOException + */ + public void setRawXML(byte[] rawXML) throws IOException { + setRawXML(rawXML, true); } private void setDocument() throws ParserConfigurationException, IOException, SAXException { @@ -218,18 +237,19 @@ public class ZUGFeRDInvoiceImporter { final ByteArrayInputStream is = new ByteArrayInputStream(rawXML); /// is.skip(guessBOMSize(is)); document = builder.parse(is); - try { - importedInvoice=new Invoice(); - extractInto(importedInvoice); - } catch (XPathExpressionException e) { - throw new RuntimeException(e); - } catch (ParseException e) { - throw new RuntimeException(e); + if (parseAutomatically) { + try { + importedInvoice = new Invoice(); + extractInto(importedInvoice); + } catch (XPathExpressionException e) { + throw new RuntimeException(e); + } catch (ParseException e) { + throw new RuntimeException(e); + } } } - /*** * This will parse a XML into the given invoice object * @param zpp the invoice to be altered @@ -377,7 +397,7 @@ public class ZUGFeRDInvoiceImporter { } - String currency= extractString("//*[local-name()=\"ApplicableHeaderTradeSettlement\"]/*[local-name()=\"InvoiceCurrencyCode\"]|*[local-name()=\"DocumentCurrencyCode\"]"); + String currency = extractString("//*[local-name()=\"ApplicableHeaderTradeSettlement\"]/*[local-name()=\"InvoiceCurrencyCode\"]|*[local-name()=\"DocumentCurrencyCode\"]"); zpp.setCurrency(currency); xpr = xpath.compile("//*[local-name()=\"ApplicableHeaderTradeSettlement\"]|//*[local-name()=\"ApplicableSupplyChainTradeSettlement\"]"); @@ -503,7 +523,7 @@ public class ZUGFeRDInvoiceImporter { xpr = xpath.compile("//*[local-name()=\"AttachmentBinaryObject\"]|//*[local-name()=\"EmbeddedDocumentBinaryObject\"]"); NodeList attachmentNodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET); for (int i = 0; i < attachmentNodes.getLength(); i++) { - FileAttachment fa=new FileAttachment(attachmentNodes.item(i).getAttributes().getNamedItem("filename").getNodeValue(),attachmentNodes.item(i).getAttributes().getNamedItem("mimeCode").getNodeValue(),"Data", Base64.getDecoder().decode(attachmentNodes.item(i).getTextContent())); + FileAttachment fa = new FileAttachment(attachmentNodes.item(i).getAttributes().getNamedItem("filename").getNodeValue(), attachmentNodes.item(i).getAttributes().getNamedItem("mimeCode").getNodeValue(), "Data", Base64.getDecoder().decode(attachmentNodes.item(i).getTextContent())); fileAttachments.add(fa); // filename = "Aufmass.png" mimeCode = "image/png" //EmbeddedDocumentBinaryObject cbc:EmbeddedDocumentBinaryObject mimeCode="image/png" filename="Aufmass.png" @@ -581,6 +601,7 @@ public class ZUGFeRDInvoiceImporter { } TransactionCalculator tc = new TransactionCalculator(zpp); + String expectedStringTotalGross = tc.getGrandTotal().toPlainString(); EStandard whichType; try { @@ -600,12 +621,12 @@ public class ZUGFeRDInvoiceImporter { return zpp; } + protected Document getDocument() { return document; } - protected String extractString(String xpathStr) { if (!containsMeta) { throw new ZUGFeRDExportException("No suitable data/ZUGFeRD file could be found."); diff --git a/library/src/test/java/org/mustangproject/ZUGFeRD/XRTest.java b/library/src/test/java/org/mustangproject/ZUGFeRD/XRTest.java index 56dfeb5e..0607cd09 100644 --- a/library/src/test/java/org/mustangproject/ZUGFeRD/XRTest.java +++ b/library/src/test/java/org/mustangproject/ZUGFeRD/XRTest.java @@ -125,13 +125,12 @@ public class XRTest extends TestCase { Invoice readInvoice = new Invoice(); ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter(); try { - - zii.setRawXML(zf2p.getXML()); + zii.setRawXML(zf2p.getXML(), false); zii.extractInto(readInvoice); } catch (ParseException | XPathExpressionException xp) { - fail("Exception not expected"); + fail("ParseException not expected"); } catch (IOException e) { - throw new RuntimeException(e); + fail("IOException not expected"); } List attachedFiles=zii.getFileAttachmentsXML(); assertNotNull(attachedFiles);