be able to parse minimal invoices

This commit is contained in:
jstaerk
2024-10-07 10:21:01 +02:00
parent a15267272b
commit f71b59a11c
4 changed files with 40 additions and 6 deletions

View File

@@ -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;
}
}

View File

@@ -394,9 +394,7 @@ public class ZUGFeRDImporter extends ZUGFeRDInvoiceImporter {
*/ */
public String getAmount() { public String getAmount() {
TransactionCalculator ic=new TransactionCalculator(importedInvoice); return importedInvoice.getGrandTotal().toPlainString();
return ic.getGrandTotal().toPlainString();
} }

View File

@@ -67,7 +67,7 @@ public class ZUGFeRDInvoiceImporter {
*/ */
protected boolean parseAutomatically = true; protected boolean parseAutomatically = true;
protected Integer version; protected Integer version;
protected Invoice importedInvoice = null; protected CalculatedInvoice importedInvoice = null;
protected boolean recalcPrice = false; protected boolean recalcPrice = false;
protected boolean ignoreCalculationErrors = false; protected boolean ignoreCalculationErrors = false;
protected ArrayList<FileAttachment> fileAttachments = new ArrayList<>(); protected ArrayList<FileAttachment> fileAttachments = new ArrayList<>();
@@ -239,7 +239,7 @@ public class ZUGFeRDInvoiceImporter {
document = builder.parse(is); document = builder.parse(is);
if (parseAutomatically) { if (parseAutomatically) {
try { try {
importedInvoice = new Invoice(); importedInvoice = new CalculatedInvoice();
extractInto(importedInvoice); extractInto(importedInvoice);
} catch (XPathExpressionException e) { } catch (XPathExpressionException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -281,6 +281,12 @@ public class ZUGFeRDInvoiceImporter {
NodeList totalNodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET); NodeList totalNodes = (NodeList) xpr.evaluate(getDocument(), XPathConstants.NODESET);
if (totalNodes.getLength() > 0) { if (totalNodes.getLength() > 0) {
expectedGrandTotal = new BigDecimal(totalNodes.item(0).getTextContent()); 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; Date issueDate = null;

View File

@@ -136,7 +136,7 @@ public class ProfilesMinimumBasicWLTest extends TestCase {
ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF_FX_MINIMUM); ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF_FX_MINIMUM);
// Reading ZUGFeRD // Reading ZUGFeRD
assertEquals("145.37",zi.getAmount()); assertEquals("146.37",zi.getAmount());
// assertEquals(zi.getBIC(), ownBIC); // assertEquals(zi.getBIC(), ownBIC);
// assertEquals(zi.getIBAN(), ownIBAN); // assertEquals(zi.getIBAN(), ownIBAN);
assertEquals(ownOrgName, zi.getHolder()); assertEquals(ownOrgName, zi.getHolder());