From f71b59a11c4c3b296684353c1ab762fe1216fa53 Mon Sep 17 00:00:00 2001 From: jstaerk Date: Mon, 7 Oct 2024 10:21:01 +0200 Subject: [PATCH] be able to parse minimal invoices --- .../org/mustangproject/CalculatedInvoice.java | 30 +++++++++++++++++++ .../ZUGFeRD/ZUGFeRDImporter.java | 4 +-- .../ZUGFeRD/ZUGFeRDInvoiceImporter.java | 10 +++++-- .../ZUGFeRD/ProfilesMinimumBasicWLTest.java | 2 +- 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 library/src/main/java/org/mustangproject/CalculatedInvoice.java diff --git a/library/src/main/java/org/mustangproject/CalculatedInvoice.java b/library/src/main/java/org/mustangproject/CalculatedInvoice.java new file mode 100644 index 00000000..729d0f9b --- /dev/null +++ b/library/src/main/java/org/mustangproject/CalculatedInvoice.java @@ -0,0 +1,30 @@ +// Copyright (c) 2023 Jochen Stärk, see LICENSE file +package org.mustangproject; + + +import org.mustangproject.ZUGFeRD.TransactionCalculator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Serializable; +import java.math.BigDecimal; + +public class CalculatedInvoice extends Invoice implements Serializable { + + protected BigDecimal grandTotal=null; + + public void calculate() { + TransactionCalculator tc=new TransactionCalculator(this); + grandTotal=tc.getGrandTotal(); + } + public BigDecimal getGrandTotal() { + if (grandTotal==null) { + calculate(); + } + return grandTotal; + } + public CalculatedInvoice setGrandTotal(BigDecimal grand) { + grandTotal=grand; + return this; + } +} diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java index fec66407..53343fc9 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDImporter.java @@ -394,9 +394,7 @@ public String getHolder() { */ public String getAmount() { - TransactionCalculator ic=new TransactionCalculator(importedInvoice); - - return ic.getGrandTotal().toPlainString(); + return importedInvoice.getGrandTotal().toPlainString(); } diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java index e46a57f6..36fdb57d 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java @@ -67,7 +67,7 @@ public class ZUGFeRDInvoiceImporter { */ protected boolean parseAutomatically = true; protected Integer version; - protected Invoice importedInvoice = null; + protected CalculatedInvoice importedInvoice = null; protected boolean recalcPrice = false; protected boolean ignoreCalculationErrors = false; protected ArrayList fileAttachments = new ArrayList<>(); @@ -239,7 +239,7 @@ private void setDocument() throws ParserConfigurationException, IOException, SAX document = builder.parse(is); if (parseAutomatically) { try { - importedInvoice = new Invoice(); + importedInvoice = new CalculatedInvoice(); extractInto(importedInvoice); } catch (XPathExpressionException e) { throw new RuntimeException(e); @@ -281,6 +281,12 @@ public Invoice extractInto(Invoice zpp) throws XPathExpressionException, ParseEx NodeList totalNodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET); if (totalNodes.getLength() > 0) { expectedGrandTotal = new BigDecimal(totalNodes.item(0).getTextContent()); + if (zpp instanceof CalculatedInvoice) { + // usually we would re-calculate the invoice to get expectedGrandTotal + // however, for "minimal" invoices or other invoices without lines + // this will not work + ((CalculatedInvoice) zpp).setGrandTotal(expectedGrandTotal); + } } Date issueDate = null; diff --git a/library/src/test/java/org/mustangproject/ZUGFeRD/ProfilesMinimumBasicWLTest.java b/library/src/test/java/org/mustangproject/ZUGFeRD/ProfilesMinimumBasicWLTest.java index 016dd9f2..4feab5a4 100644 --- a/library/src/test/java/org/mustangproject/ZUGFeRD/ProfilesMinimumBasicWLTest.java +++ b/library/src/test/java/org/mustangproject/ZUGFeRD/ProfilesMinimumBasicWLTest.java @@ -136,7 +136,7 @@ public void testMinimumInvoice() { ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF_FX_MINIMUM); // Reading ZUGFeRD - assertEquals("145.37",zi.getAmount()); + assertEquals("146.37",zi.getAmount()); // assertEquals(zi.getBIC(), ownBIC); // assertEquals(zi.getIBAN(), ownIBAN); assertEquals(ownOrgName, zi.getHolder());