Merge remote-tracking branch 'remotes/origin/master' into Improvements
# Conflicts: # library/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRDInvoiceImporter.java # library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java
This commit is contained in:
@@ -11,10 +11,18 @@ import java.util.HashMap;
|
||||
public class TransactionCalculator implements IAbsoluteValueProvider {
|
||||
protected IExportableTransaction trans;
|
||||
|
||||
/***
|
||||
*
|
||||
* @param trans the invoice (or IExportableTransaction) to be calculated
|
||||
*/
|
||||
public TransactionCalculator(IExportableTransaction trans) {
|
||||
this.trans=trans;
|
||||
}
|
||||
|
||||
/***
|
||||
* if something had already been paid in advance, this will get it from the transaction
|
||||
* @return prepaid amount
|
||||
*/
|
||||
protected BigDecimal getTotalPrepaid() {
|
||||
if (trans.getTotalPrepaidAmount() == null) {
|
||||
return BigDecimal.ZERO;
|
||||
@@ -23,7 +31,11 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
|
||||
}
|
||||
}
|
||||
|
||||
protected BigDecimal getTotalGross() {
|
||||
/***
|
||||
* the invoice total with VAT, corrected by prepaid amount, allowances and charges
|
||||
* @return the invoice total including taxes
|
||||
*/
|
||||
public BigDecimal getGrandTotal() {
|
||||
|
||||
BigDecimal res = getTaxBasis();
|
||||
HashMap<BigDecimal, VATAmount> VATPercentAmountMap = getVATPercentAmountMap();
|
||||
@@ -31,7 +43,7 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
|
||||
VATAmount amount = VATPercentAmountMap.get(currentTaxPercent);
|
||||
res = res.add(amount.getCalculated());
|
||||
}
|
||||
return res;
|
||||
return res.setScale(2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
/***
|
||||
@@ -119,6 +131,10 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
|
||||
return res;
|
||||
}
|
||||
|
||||
/***
|
||||
* returns the total net value of all items, without document level charges/allowances
|
||||
* @return item sum
|
||||
*/
|
||||
protected BigDecimal getTotal() {
|
||||
BigDecimal res = BigDecimal.ZERO;
|
||||
for (IZUGFeRDExportableItem currentItem : trans.getZFItems()) {
|
||||
@@ -128,6 +144,11 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
|
||||
return res;
|
||||
}
|
||||
|
||||
/***
|
||||
* returns the total net value of the invoice, including charges/allowances on document
|
||||
* level
|
||||
* @return item sum +- charges/allowances
|
||||
*/
|
||||
protected BigDecimal getTaxBasis() {
|
||||
BigDecimal res = getTotal().add(getChargesForPercent(null)).subtract(getAllowancesForPercent(null));
|
||||
return res;
|
||||
|
||||
@@ -305,12 +305,12 @@ public class ZUGFeRD1PullProvider extends ZUGFeRD2PullProvider implements IXMLPr
|
||||
// //
|
||||
// currencyID=\"EUR\"
|
||||
+ " <ram:TaxTotalAmount currencyID=\"" + trans.getCurrency() + "\">"
|
||||
+ currencyFormat(calc.getTotalGross().subtract(calc.getTotal())) + "</ram:TaxTotalAmount>\n"
|
||||
+ " <ram:GrandTotalAmount currencyID=\"" + trans.getCurrency() + "\">" + currencyFormat(calc.getTotalGross()) + "</ram:GrandTotalAmount>\n" //$NON-NLS-2$
|
||||
+ currencyFormat(calc.getGrandTotal().subtract(calc.getTotal())) + "</ram:TaxTotalAmount>\n"
|
||||
+ " <ram:GrandTotalAmount currencyID=\"" + trans.getCurrency() + "\">" + currencyFormat(calc.getGrandTotal()) + "</ram:GrandTotalAmount>\n" //$NON-NLS-2$
|
||||
// //
|
||||
// currencyID=\"EUR\"
|
||||
+ " <ram:TotalPrepaidAmount currencyID=\"" + trans.getCurrency() + "\">" + currencyFormat(calc.getTotalPrepaid()) + "</ram:TotalPrepaidAmount>\n"
|
||||
+ " <ram:DuePayableAmount currencyID=\"" + trans.getCurrency() + "\">" + currencyFormat(calc.getTotalGross().subtract(calc.getTotalPrepaid())) + "</ram:DuePayableAmount>\n" //$NON-NLS-2$
|
||||
+ " <ram:DuePayableAmount currencyID=\"" + trans.getCurrency() + "\">" + currencyFormat(calc.getGrandTotal().subtract(calc.getTotalPrepaid())) + "</ram:DuePayableAmount>\n" //$NON-NLS-2$
|
||||
// //
|
||||
// currencyID=\"EUR\"
|
||||
+ " </ram:SpecifiedTradeSettlementMonetarySummation>\n"
|
||||
@@ -439,7 +439,7 @@ public class ZUGFeRD1PullProvider extends ZUGFeRD2PullProvider implements IXMLPr
|
||||
if (discountTerms != null) {
|
||||
paymentTermsXml += "<ram:ApplicableTradePaymentDiscountTerms>";
|
||||
String currency = trans.getCurrency();
|
||||
String basisAmount = currencyFormat(calc.getTotalGross());
|
||||
String basisAmount = currencyFormat(calc.getGrandTotal());
|
||||
paymentTermsXml += "<ram:BasisAmount currencyID=\"" + currency + "\">" + basisAmount + "</ram:BasisAmount>";
|
||||
paymentTermsXml += "<ram:CalculationPercent>" + discountTerms.getCalculationPercentage().toString()
|
||||
+ "</ram:CalculationPercent>";
|
||||
|
||||
@@ -571,9 +571,12 @@ public class ZUGFeRD2PullProvider implements IXMLProvider {
|
||||
}
|
||||
|
||||
|
||||
if (trans.getPaymentTerms() == null) {
|
||||
xml = xml + " <ram:SpecifiedTradePaymentTerms>\n"
|
||||
+ " <ram:Description>" + paymentTermsDescription + "</ram:Description>\n";
|
||||
if ((trans.getPaymentTerms() == null)&&((paymentTermsDescription!=null)||(trans.getTradeSettlement()!=null)||(hasDueDate))) {
|
||||
xml = xml + "<ram:SpecifiedTradePaymentTerms>\n";
|
||||
|
||||
if (paymentTermsDescription!=null) {
|
||||
xml = xml + "<ram:Description>" + paymentTermsDescription + "</ram:Description>\n";
|
||||
}
|
||||
|
||||
if (trans.getTradeSettlement() != null) {
|
||||
for (IZUGFeRDTradeSettlement payment : trans.getTradeSettlement()) {
|
||||
@@ -607,12 +610,12 @@ public class ZUGFeRD2PullProvider implements IXMLProvider {
|
||||
// //
|
||||
// currencyID=\"EUR\"
|
||||
+ " <ram:TaxTotalAmount currencyID=\"" + trans.getCurrency() + "\">"
|
||||
+ currencyFormat(calc.getTotalGross().subtract(calc.getTaxBasis())) + "</ram:TaxTotalAmount>\n"
|
||||
+ " <ram:GrandTotalAmount>" + currencyFormat(calc.getTotalGross()) + "</ram:GrandTotalAmount>\n" //$NON-NLS-2$
|
||||
+ currencyFormat(calc.getGrandTotal().subtract(calc.getTaxBasis())) + "</ram:TaxTotalAmount>\n"
|
||||
+ " <ram:GrandTotalAmount>" + currencyFormat(calc.getGrandTotal()) + "</ram:GrandTotalAmount>\n" //$NON-NLS-2$
|
||||
// //
|
||||
// currencyID=\"EUR\"
|
||||
+ " <ram:TotalPrepaidAmount>" + currencyFormat(calc.getTotalPrepaid()) + "</ram:TotalPrepaidAmount>\n"
|
||||
+ " <ram:DuePayableAmount>" + currencyFormat(calc.getTotalGross().subtract(calc.getTotalPrepaid())) + "</ram:DuePayableAmount>\n" //$NON-NLS-2$
|
||||
+ " <ram:DuePayableAmount>" + currencyFormat(calc.getGrandTotal().subtract(calc.getTotalPrepaid())) + "</ram:DuePayableAmount>\n" //$NON-NLS-2$
|
||||
// //
|
||||
// currencyID=\"EUR\"
|
||||
+ " </ram:SpecifiedTradeSettlementHeaderMonetarySummation>\n"
|
||||
@@ -645,9 +648,13 @@ public class ZUGFeRD2PullProvider implements IXMLProvider {
|
||||
}
|
||||
|
||||
private String buildPaymentTermsXml() {
|
||||
String paymentTermsXml = "<ram:SpecifiedTradePaymentTerms>";
|
||||
|
||||
IZUGFeRDPaymentTerms paymentTerms = trans.getPaymentTerms();
|
||||
if (paymentTerms==null) {
|
||||
return "";
|
||||
}
|
||||
String paymentTermsXml = "<ram:SpecifiedTradePaymentTerms>";
|
||||
|
||||
IZUGFeRDPaymentDiscountTerms discountTerms = paymentTerms.getDiscountTerms();
|
||||
Date dueDate = paymentTerms.getDueDate();
|
||||
if (dueDate != null && discountTerms != null && discountTerms.getBaseDate() != null) {
|
||||
@@ -665,7 +672,7 @@ public class ZUGFeRD2PullProvider implements IXMLProvider {
|
||||
if (discountTerms != null) {
|
||||
paymentTermsXml += "<ram:ApplicableTradePaymentDiscountTerms>";
|
||||
String currency = trans.getCurrency();
|
||||
String basisAmount = currencyFormat(calc.getTotalGross());
|
||||
String basisAmount = currencyFormat(calc.getGrandTotal());
|
||||
paymentTermsXml += "<ram:BasisAmount currencyID=\"" + currency + "\">" + basisAmount + "</ram:BasisAmount>";
|
||||
paymentTermsXml += "<ram:CalculationPercent>" + discountTerms.getCalculationPercentage().toString()
|
||||
+ "</ram:CalculationPercent>";
|
||||
|
||||
@@ -260,7 +260,7 @@ public class ZUGFeRDInvoiceImporter extends ZUGFeRDImporter {
|
||||
}
|
||||
|
||||
TransactionCalculator tc = new TransactionCalculator(zpp);
|
||||
String expectedStringTotalGross = tc.getTotalGross().setScale(2, RoundingMode.HALF_UP).toPlainString();
|
||||
String expectedStringTotalGross = tc.getGrandTotal().toPlainString();
|
||||
if (!expectedStringTotalGross.equals(XMLTools.nDigitFormat(expectedGrandTotal, 2))) {
|
||||
throw new ParseException("Could not reproduce the invoice, this could mean that it could not be read properly", 0);
|
||||
}
|
||||
|
||||
@@ -20,21 +20,13 @@
|
||||
package org.mustangproject.ZUGFeRD;
|
||||
|
||||
import org.mustangproject.Invoice;
|
||||
import org.mustangproject.XMLTools;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
|
||||
/***
|
||||
@@ -82,7 +74,7 @@ public class ZF2ZInvoiceImporterTest extends TestCase {
|
||||
assertEquals("Stadthausen", invoice.getSender().getLocation());
|
||||
|
||||
TransactionCalculator tc=new TransactionCalculator(invoice);
|
||||
assertEquals("571.04", XMLTools.nDigitFormat(tc.getTotalGross(),2));
|
||||
assertEquals(new BigDecimal("571.04"),tc.getGrandTotal());
|
||||
|
||||
|
||||
// name street location zip country, contact name phone email, total amount
|
||||
@@ -103,7 +95,7 @@ public class ZF2ZInvoiceImporterTest extends TestCase {
|
||||
}
|
||||
assertFalse(hasExceptions);
|
||||
TransactionCalculator tc=new TransactionCalculator(invoice);
|
||||
assertEquals("18.33", XMLTools.nDigitFormat(tc.getTotalGross(),2));
|
||||
assertEquals(new BigDecimal("18.33"),tc.getGrandTotal());
|
||||
}
|
||||
|
||||
public void testAllowancesChargesImport() {
|
||||
@@ -119,7 +111,7 @@ public class ZF2ZInvoiceImporterTest extends TestCase {
|
||||
}
|
||||
assertFalse(hasExceptions);
|
||||
TransactionCalculator tc=new TransactionCalculator(invoice);
|
||||
assertEquals("11.07", XMLTools.nDigitFormat(tc.getTotalGross(),2));
|
||||
assertEquals(new BigDecimal("11.07"),tc.getGrandTotal());
|
||||
|
||||
|
||||
// name street location zip country, contact name phone email, total amount
|
||||
|
||||
Reference in New Issue
Block a user