Merge pull request #209 from weclapp-dev/Improvements

thank you very much
This commit is contained in:
Jochen Staerk
2020-12-10 09:47:03 +01:00
committed by GitHub
2 changed files with 48 additions and 59 deletions

View File

@@ -1,8 +1,11 @@
package org.mustangproject.ZUGFeRD; package org.mustangproject.ZUGFeRD;
import static java.math.BigDecimal.ZERO;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.HashMap; import java.util.HashMap;
import java.util.stream.Stream;
/*** /***
* The Transactioncalculator e.g. adds the line totals and applies VAT on whole invoices * The Transactioncalculator e.g. adds the line totals and applies VAT on whole invoices
@@ -27,7 +30,7 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
if (trans.getTotalPrepaidAmount() == null) { if (trans.getTotalPrepaidAmount() == null) {
return BigDecimal.ZERO; return BigDecimal.ZERO;
} else { } else {
return trans.getTotalPrepaidAmount(); return trans.getTotalPrepaidAmount().setScale(2, RoundingMode.HALF_UP);
} }
} }
@@ -37,13 +40,11 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
*/ */
public BigDecimal getGrandTotal() { public BigDecimal getGrandTotal() {
BigDecimal res = getTaxBasis(); final BigDecimal res = getTaxBasis();
HashMap<BigDecimal, VATAmount> VATPercentAmountMap = getVATPercentAmountMap(); return getVATPercentAmountMap().values().stream()
for (BigDecimal currentTaxPercent : VATPercentAmountMap.keySet()) { .map(VATAmount::getCalculated)
VATAmount amount = VATPercentAmountMap.get(currentTaxPercent); .map(p -> p.setScale(2, RoundingMode.HALF_UP))
res = res.add(amount.getCalculated()); .reduce(BigDecimal.ZERO, BigDecimal::add).add(res);
}
return res.setScale(2, RoundingMode.HALF_UP);
} }
/*** /***
@@ -52,16 +53,20 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
* @return the total amount * @return the total amount
*/ */
protected BigDecimal getChargesForPercent(BigDecimal percent) { protected BigDecimal getChargesForPercent(BigDecimal percent) {
BigDecimal res = BigDecimal.ZERO;
IZUGFeRDAllowanceCharge[] charges = trans.getZFCharges(); IZUGFeRDAllowanceCharge[] charges = trans.getZFCharges();
if ((charges != null) && (charges.length > 0)) { return sumAllowanceCharge(percent, charges);
for (IZUGFeRDAllowanceCharge currentCharge : charges) { }
if ((percent==null)||(currentCharge.getTaxPercent().compareTo(percent)==0)) {
res = res.add(currentCharge.getTotalAmount(this)); private BigDecimal sumAllowanceCharge(BigDecimal percent, IZUGFeRDAllowanceCharge[] charges) {
} BigDecimal res = BigDecimal.ZERO;
} if ((charges != null) && (charges.length > 0)) {
} for (IZUGFeRDAllowanceCharge currentCharge : charges) {
return res; if ((percent==null)||(currentCharge.getTaxPercent().compareTo(percent)==0)) {
res = res.add(currentCharge.getTotalAmount(this));
}
}
}
return res;
} }
/*** /***
@@ -70,43 +75,37 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
* @return the space separated String * @return the space separated String
*/ */
protected String getChargeReasonForPercent(BigDecimal percent) { protected String getChargeReasonForPercent(BigDecimal percent) {
String res = " ";
IZUGFeRDAllowanceCharge[] charges = trans.getZFCharges(); IZUGFeRDAllowanceCharge[] charges = trans.getZFCharges();
if ((charges != null) && (charges.length > 0)) { String res = getAllowanceChargeReasonForPercent(percent, charges);
for (IZUGFeRDAllowanceCharge currentCharge : charges) { if ("".equals(res)) {
if ((percent==null)||(currentCharge.getTaxPercent().compareTo(percent)==0)) {
if (currentCharge.getReason()!=null) {
res = res+currentCharge.getReason()+" ";
}
}
}
}
res=res.substring(0,res.length()-1);
if (res.equals("")) {
res="Charges"; res="Charges";
} }
return res; return res;
} }
private String getAllowanceChargeReasonForPercent(BigDecimal percent, IZUGFeRDAllowanceCharge[] charges) {
String res = " ";
if ((charges != null) && (charges.length > 0)) {
for (IZUGFeRDAllowanceCharge currentCharge : charges) {
if ((percent==null)||(currentCharge.getTaxPercent().compareTo(percent)==0)
&& currentCharge.getReason()!=null) {
res += currentCharge.getReason()+" ";
}
}
}
res=res.substring(0,res.length()-1);
return res;
}
/*** /***
* returns a (potentially concatenated) string of allowance reasons, or "Allowances", if none are defined * returns a (potentially concatenated) string of allowance reasons, or "Allowances", if none are defined
* @param percent a specific rate, or null for any rate * @param percent a specific rate, or null for any rate
* @return the space separated String * @return the space separated String
*/ */
protected String getAllowanceReasonForPercent(BigDecimal percent) { protected String getAllowanceReasonForPercent(BigDecimal percent) {
String res = " ";
IZUGFeRDAllowanceCharge[] allowances = trans.getZFAllowances(); IZUGFeRDAllowanceCharge[] allowances = trans.getZFAllowances();
if ((allowances != null) && (allowances.length > 0)) { String res = getAllowanceChargeReasonForPercent(percent, allowances);
for (IZUGFeRDAllowanceCharge currentAllowance : allowances) { if ("".equals(res)) {
if ((percent==null)||(currentAllowance.getTaxPercent().compareTo(percent)==0)) {
if (currentAllowance.getReason()!=null) {
res = res+currentAllowance.getReason()+" ";
}
}
}
}
res=res.substring(0,res.length()-1);
if (res.equals("")) {
res="Allowances"; res="Allowances";
} }
return res; return res;
@@ -119,16 +118,8 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
* @return the total amount * @return the total amount
*/ */
protected BigDecimal getAllowancesForPercent(BigDecimal percent) { protected BigDecimal getAllowancesForPercent(BigDecimal percent) {
BigDecimal res = BigDecimal.ZERO;
IZUGFeRDAllowanceCharge[] allowances = trans.getZFAllowances(); IZUGFeRDAllowanceCharge[] allowances = trans.getZFAllowances();
if ((allowances != null) && (allowances.length > 0)) { return sumAllowanceCharge(percent, allowances);
for (IZUGFeRDAllowanceCharge currentAllowance : allowances) {
if ((percent==null)||(currentAllowance.getTaxPercent().compareTo(percent)==0)) {
res = res.add(currentAllowance.getTotalAmount(this));
}
}
}
return res;
} }
/*** /***
@@ -136,12 +127,10 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
* @return item sum * @return item sum
*/ */
protected BigDecimal getTotal() { protected BigDecimal getTotal() {
BigDecimal res = BigDecimal.ZERO; return Stream.of(trans.getZFItems())
for (IZUGFeRDExportableItem currentItem : trans.getZFItems()) { .map(LineCalculator::new)
LineCalculator lc = new LineCalculator(currentItem); .map(LineCalculator::getItemTotalNetAmount)
res = res.add(lc.getItemTotalNetAmount()); .reduce(ZERO, BigDecimal::add);
}
return res;
} }
/*** /***
@@ -151,7 +140,7 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
*/ */
protected BigDecimal getTaxBasis() { protected BigDecimal getTaxBasis() {
BigDecimal res = getTotal().add(getChargesForPercent(null)).subtract(getAllowancesForPercent(null)); BigDecimal res = getTotal().add(getChargesForPercent(null)).subtract(getAllowancesForPercent(null));
return res; return res.setScale(2, RoundingMode.HALF_UP);
} }
/** /**
@@ -208,7 +197,6 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
} }
} }
return hm; return hm;
} }

View File

@@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@@ -612,7 +613,7 @@ public class ZUGFeRD2PullProvider implements IXMLProvider {
// // // //
// currencyID=\"EUR\" // currencyID=\"EUR\"
+ " <ram:TaxTotalAmount currencyID=\"" + trans.getCurrency() + "\">" + " <ram:TaxTotalAmount currencyID=\"" + trans.getCurrency() + "\">"
+ currencyFormat(calc.getGrandTotal().subtract(calc.getTaxBasis())) + "</ram:TaxTotalAmount>\n" + currencyFormat(calc.getGrandTotal().subtract(calc.getTaxBasis()))+ "</ram:TaxTotalAmount>\n"
+ " <ram:GrandTotalAmount>" + currencyFormat(calc.getGrandTotal()) + "</ram:GrandTotalAmount>\n" //$NON-NLS-2$ + " <ram:GrandTotalAmount>" + currencyFormat(calc.getGrandTotal()) + "</ram:GrandTotalAmount>\n" //$NON-NLS-2$
// // // //
// currencyID=\"EUR\" // currencyID=\"EUR\"