Skip to content

Commit

Permalink
be able to parse minimal invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
jstaerk committed Oct 7, 2024
1 parent a152672 commit f71b59a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
30 changes: 30 additions & 0 deletions library/src/main/java/org/mustangproject/CalculatedInvoice.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ public String getHolder() {
*/
public String getAmount() {

TransactionCalculator ic=new TransactionCalculator(importedInvoice);

return ic.getGrandTotal().toPlainString();
return importedInvoice.getGrandTotal().toPlainString();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileAttachment> fileAttachments = new ArrayList<>();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit f71b59a

Please sign in to comment.