From 3ff6caafc714f2e985aa300979cfeaea8a4db662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20St=C3=A4rk?= Date: Thu, 11 Dec 2025 14:36:45 +0100 Subject: [PATCH] closes #985 --- History.md | 2 +- .../ZUGFeRD/TransactionCalculator.java | 87 +++++++------------ .../ZUGFeRD/VATrelatedTest.java | 2 +- 3 files changed, 35 insertions(+), 56 deletions(-) diff --git a/History.md b/History.md index 410c3a70..4835a7c2 100644 --- a/History.md +++ b/History.md @@ -4,7 +4,7 @@ - #969 - #984 - #983 -- #979 +- #979, #985 2.20.0 ======= diff --git a/library/src/main/java/org/mustangproject/ZUGFeRD/TransactionCalculator.java b/library/src/main/java/org/mustangproject/ZUGFeRD/TransactionCalculator.java index f8c80127..3778a7cc 100644 --- a/library/src/main/java/org/mustangproject/ZUGFeRD/TransactionCalculator.java +++ b/library/src/main/java/org/mustangproject/ZUGFeRD/TransactionCalculator.java @@ -172,7 +172,6 @@ public class TransactionCalculator implements IAbsoluteValueProvider { * @return item sum +- charges/allowances */ public BigDecimal getTaxBasis() { - BigDecimal debug_1=getTotal(); return getTotal().add(getChargesForPercent(null).setScale(2, RoundingMode.HALF_UP)) .subtract(getAllowancesForPercent(null).setScale(2, RoundingMode.HALF_UP)) .setScale(2, RoundingMode.HALF_UP); @@ -252,57 +251,45 @@ public class TransactionCalculator implements IAbsoluteValueProvider { return hm; } - protected List getVATAmountList() - { + protected List getVATAmountList() { final List vatAmounts = new ArrayList<>(); final String vatDueDateTypeCode = this.trans.getVATDueDateTypeCode(); - for (final IZUGFeRDExportableItem currentItem : this.trans.getZFItems()) - { + for (final IZUGFeRDExportableItem currentItem : this.trans.getZFItems()) { BigDecimal percent = null; - if (currentItem.getProduct() != null) - { + if (currentItem.getProduct() != null) { percent = currentItem.getProduct().getVATPercent(); } - if (percent != null) - { - final LineCalculator lc = currentItem.getCalculation(); - final VATAmount itemVATAmount = new VATAmount(lc.getItemTotalNetAmount(), lc.getItemTotalVATAmount(), - currentItem.getProduct().getTaxCategoryCode(), vatDueDateTypeCode, percent); - final String reasonText = currentItem.getProduct().getTaxExemptionReason(); - if (reasonText != null) - { - itemVATAmount.setVatExemptionReasonText(reasonText); - } - final Optional currentVatAmount = this.getCurrentVatAmount(vatAmounts, currentItem.getProduct().getTaxCategoryCode(), percent); - if (currentVatAmount.isEmpty()) - { - vatAmounts.add(itemVATAmount); - } - else - { - this.mergeAdding(currentVatAmount.get(), itemVATAmount); - } + if (percent == null) { + percent = ZERO; + } + final LineCalculator lc = currentItem.getCalculation(); + final VATAmount itemVATAmount = new VATAmount(lc.getItemTotalNetAmount(), lc.getItemTotalVATAmount(), + currentItem.getProduct().getTaxCategoryCode(), vatDueDateTypeCode, percent); + final String reasonText = currentItem.getProduct().getTaxExemptionReason(); + if (reasonText != null) { + itemVATAmount.setVatExemptionReasonText(reasonText); + } + final Optional currentVatAmount = this.getCurrentVatAmount(vatAmounts, currentItem.getProduct().getTaxCategoryCode(), percent); + if (currentVatAmount.isEmpty()) { + vatAmounts.add(itemVATAmount); + } else { + this.mergeAdding(currentVatAmount.get(), itemVATAmount); } } final IZUGFeRDAllowanceCharge[] charges = this.trans.getZFCharges(); if (charges != null) { - for (final IZUGFeRDAllowanceCharge currentCharge : charges) - { + for (final IZUGFeRDAllowanceCharge currentCharge : charges) { final BigDecimal taxPercent = currentCharge.getTaxPercent(); - if (taxPercent != null) - { + if (taxPercent != null) { final String vatCategoryCode = currentCharge.getCategoryCode() != null ? currentCharge.getCategoryCode() : "S"; final Optional currentChargeVatAmount = this.getCurrentVatAmount(vatAmounts, vatCategoryCode, taxPercent); final BigDecimal chargeBasis = currentCharge.getTotalAmount(this); final VATAmount chargeVatAmount = new VATAmount(chargeBasis, chargeBasis.multiply(taxPercent.divide(new BigDecimal(100))), vatCategoryCode, vatDueDateTypeCode, taxPercent); - if (currentChargeVatAmount.isEmpty()) - { + if (currentChargeVatAmount.isEmpty()) { vatAmounts.add(chargeVatAmount); - } - else - { + } else { this.mergeAdding(currentChargeVatAmount.get(), chargeVatAmount); } } @@ -310,11 +297,9 @@ public class TransactionCalculator implements IAbsoluteValueProvider { } final IZUGFeRDAllowanceCharge[] allowances = this.trans.getZFAllowances(); if (allowances != null) { - for (final IZUGFeRDAllowanceCharge currentAllowance : allowances) - { + for (final IZUGFeRDAllowanceCharge currentAllowance : allowances) { final BigDecimal taxPercent = currentAllowance.getTaxPercent(); - if (taxPercent != null) - { + if (taxPercent != null) { final String vatCategoryCode = currentAllowance.getCategoryCode() != null ? currentAllowance.getCategoryCode() : "S"; final Optional currentAllowanceVatAmount = this.getCurrentVatAmount(vatAmounts, vatCategoryCode, taxPercent); final BigDecimal allowanceNegativeBasis = currentAllowance.getTotalAmount(this).multiply(BigDecimal.valueOf(-1)); @@ -322,12 +307,9 @@ public class TransactionCalculator implements IAbsoluteValueProvider { allowanceNegativeBasis.multiply(taxPercent.divide(new BigDecimal(100))), currentAllowance.getCategoryCode() != null ? currentAllowance.getCategoryCode() : "S", vatDueDateTypeCode, taxPercent); - if (currentAllowanceVatAmount.isEmpty()) - { + if (currentAllowanceVatAmount.isEmpty()) { vatAmounts.add(allowanceVATAmount); - } - else - { + } else { this.mergeAdding(currentAllowanceVatAmount.get(), allowanceVATAmount); } } @@ -335,19 +317,17 @@ public class TransactionCalculator implements IAbsoluteValueProvider { } return vatAmounts; } - - public void mergeAdding(VATAmount vatAmount, VATAmount toAdd) - { + + public void mergeAdding(VATAmount vatAmount, VATAmount toAdd) { vatAmount.setBasis(vatAmount.getBasis().add(toAdd.getBasis())); vatAmount.setCalculated(vatAmount.getCalculated().add(toAdd.getCalculated())); - if (toAdd.getVatExemptionReasonText() != null && !toAdd.getVatExemptionReasonText().isBlank()) - { + if (toAdd.getVatExemptionReasonText() != null && !toAdd.getVatExemptionReasonText().isBlank()) { Optional.ofNullable(vatAmount.getVatExemptionReasonText()).filter(reasonText -> !reasonText.equals(toAdd.getVatExemptionReasonText())).ifPresentOrElse( text -> vatAmount.setVatExemptionReasonText(String.join(", ", text, toAdd.getVatExemptionReasonText())), () -> vatAmount.setVatExemptionReasonText(toAdd.getVatExemptionReasonText())); } } - + @Override public BigDecimal getValue() { return getTotal(); @@ -360,13 +340,12 @@ public class TransactionCalculator implements IAbsoluteValueProvider { public BigDecimal getAllowanceTotal() { return getAllowancesForPercent(null).setScale(2, RoundingMode.HALF_UP); } - - private Optional getCurrentVatAmount(List vatAmounts, String vatCategoryCode, BigDecimal percentage) - { + + private Optional getCurrentVatAmount(List vatAmounts, String vatCategoryCode, BigDecimal percentage) { return vatAmounts.stream() .filter(va -> Objects.equals(vatCategoryCode, va.getCategoryCode()) && Optional.ofNullable(percentage).map(p -> va.getApplicablePercent() == null && p == null || p.compareTo(va.getApplicablePercent()) == 0) - .orElse(true)) + .orElse(true)) .findFirst(); } diff --git a/library/src/test/java/org/mustangproject/ZUGFeRD/VATrelatedTest.java b/library/src/test/java/org/mustangproject/ZUGFeRD/VATrelatedTest.java index 7cd41ead..4c51d321 100644 --- a/library/src/test/java/org/mustangproject/ZUGFeRD/VATrelatedTest.java +++ b/library/src/test/java/org/mustangproject/ZUGFeRD/VATrelatedTest.java @@ -132,7 +132,7 @@ public class VATrelatedTest extends ResourceCase { assertTrue(theXML.contains("