Merge branch 'master' into subtotal_validation

This commit is contained in:
Jochen Staerk
2025-12-11 14:39:01 +01:00
committed by GitHub
9 changed files with 246 additions and 55 deletions

View File

@@ -2,6 +2,9 @@
- #954 - #954
- #947 - #947
- #969 - #969
- #984
- #983
- #979, #985
2.20.0 2.20.0
======= =======

View File

@@ -194,6 +194,12 @@ public class Invoice implements IExportableTransaction {
} }
public Invoice setCreditNote() { public Invoice setCreditNote() {
documentCode = DocumentCodeTypeConstants.CREDITNOTE; // this value should somewhen be changed to Selfbilling
return this;
}
public Invoice setCreditNote(String number) {
setInvoiceReferencedDocumentID(number);
documentCode = DocumentCodeTypeConstants.CREDITNOTE; documentCode = DocumentCodeTypeConstants.CREDITNOTE;
return this; return this;
} }

View File

@@ -233,6 +233,9 @@ public class Product implements IZUGFeRDExportableProduct {
*/ */
public Product setReverseCharge() { public Product setReverseCharge() {
isReverseCharge = true; isReverseCharge = true;
if ((getTaxExemptionReason()==null)||(getTaxExemptionReason().isEmpty())) {
setTaxExemptionReason("Reverse charge");
}
setVATPercent(BigDecimal.ZERO); setVATPercent(BigDecimal.ZERO);
return this; return this;
} }

View File

@@ -120,7 +120,7 @@ public interface IZUGFeRDExportableProduct {
return TaxCategoryCodeTypeConstants.INTRACOMMUNITY;// "K"; // within europe return TaxCategoryCodeTypeConstants.INTRACOMMUNITY;// "K"; // within europe
} else if (isReverseCharge()) { } else if (isReverseCharge()) {
return TaxCategoryCodeTypeConstants.REVERSECHARGE;// "AE"; // to out of europe... return TaxCategoryCodeTypeConstants.REVERSECHARGE;// "AE"; // to out of europe...
} else if (getVATPercent().compareTo(BigDecimal.ZERO) == 0) { } else if ((getVATPercent()==null)||(getVATPercent().compareTo(BigDecimal.ZERO) == 0)) {
return TaxCategoryCodeTypeConstants.ZEROTAXPRODUCTS; // "Z"; // zero rated goods return TaxCategoryCodeTypeConstants.ZEROTAXPRODUCTS; // "Z"; // zero rated goods
} else { } else {
return TaxCategoryCodeTypeConstants.STANDARDRATE;// "S"; // one of the "standard" rates (not return TaxCategoryCodeTypeConstants.STANDARDRATE;// "S"; // one of the "standard" rates (not

View File

@@ -175,7 +175,6 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
* @return item sum +- charges/allowances * @return item sum +- charges/allowances
*/ */
public BigDecimal getTaxBasis() { public BigDecimal getTaxBasis() {
BigDecimal debug_1=getTotal();
return getTotal().add(getChargesForPercent(null).setScale(2, RoundingMode.HALF_UP)) return getTotal().add(getChargesForPercent(null).setScale(2, RoundingMode.HALF_UP))
.subtract(getAllowancesForPercent(null).setScale(2, RoundingMode.HALF_UP)) .subtract(getAllowancesForPercent(null).setScale(2, RoundingMode.HALF_UP))
.setScale(2, RoundingMode.HALF_UP); .setScale(2, RoundingMode.HALF_UP);
@@ -259,8 +258,7 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
return hm; return hm;
} }
protected List<VATAmount> getVATAmountList() protected List<VATAmount> getVATAmountList() {
{
final List<VATAmount> vatAmounts = new ArrayList<>(); final List<VATAmount> vatAmounts = new ArrayList<>();
final String vatDueDateTypeCode = this.trans.getVATDueDateTypeCode(); final String vatDueDateTypeCode = this.trans.getVATDueDateTypeCode();
for (final IZUGFeRDExportableItem currentItem : this.trans.getZFItems()) for (final IZUGFeRDExportableItem currentItem : this.trans.getZFItems())
@@ -270,50 +268,40 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
continue; continue;
} }
BigDecimal percent = null; BigDecimal percent = null;
if (currentItem.getProduct() != null) if (currentItem.getProduct() != null) {
{
percent = currentItem.getProduct().getVATPercent(); percent = currentItem.getProduct().getVATPercent();
} }
if (percent != null) if (percent == null) {
{ percent = ZERO;
final LineCalculator lc = currentItem.getCalculation(); }
final VATAmount itemVATAmount = new VATAmount(lc.getItemTotalNetAmount(), lc.getItemTotalVATAmount(), final LineCalculator lc = currentItem.getCalculation();
currentItem.getProduct().getTaxCategoryCode(), vatDueDateTypeCode, percent); final VATAmount itemVATAmount = new VATAmount(lc.getItemTotalNetAmount(), lc.getItemTotalVATAmount(),
final String reasonText = currentItem.getProduct().getTaxExemptionReason(); currentItem.getProduct().getTaxCategoryCode(), vatDueDateTypeCode, percent);
if (reasonText != null) final String reasonText = currentItem.getProduct().getTaxExemptionReason();
{ if (reasonText != null) {
itemVATAmount.setVatExemptionReasonText(reasonText); itemVATAmount.setVatExemptionReasonText(reasonText);
} }
final Optional<VATAmount> currentVatAmount = this.getCurrentVatAmount(vatAmounts, currentItem.getProduct().getTaxCategoryCode(), percent); final Optional<VATAmount> currentVatAmount = this.getCurrentVatAmount(vatAmounts, currentItem.getProduct().getTaxCategoryCode(), percent);
if (currentVatAmount.isEmpty()) if (currentVatAmount.isEmpty()) {
{ vatAmounts.add(itemVATAmount);
vatAmounts.add(itemVATAmount); } else {
} this.mergeAdding(currentVatAmount.get(), itemVATAmount);
else
{
this.mergeAdding(currentVatAmount.get(), itemVATAmount);
}
} }
} }
final IZUGFeRDAllowanceCharge[] charges = this.trans.getZFCharges(); final IZUGFeRDAllowanceCharge[] charges = this.trans.getZFCharges();
if (charges != null) { if (charges != null) {
for (final IZUGFeRDAllowanceCharge currentCharge : charges) for (final IZUGFeRDAllowanceCharge currentCharge : charges) {
{
final BigDecimal taxPercent = currentCharge.getTaxPercent(); final BigDecimal taxPercent = currentCharge.getTaxPercent();
if (taxPercent != null) if (taxPercent != null) {
{
final String vatCategoryCode = currentCharge.getCategoryCode() != null ? currentCharge.getCategoryCode() : "S"; final String vatCategoryCode = currentCharge.getCategoryCode() != null ? currentCharge.getCategoryCode() : "S";
final Optional<VATAmount> currentChargeVatAmount = this.getCurrentVatAmount(vatAmounts, vatCategoryCode, taxPercent); final Optional<VATAmount> currentChargeVatAmount = this.getCurrentVatAmount(vatAmounts, vatCategoryCode, taxPercent);
final BigDecimal chargeBasis = currentCharge.getTotalAmount(this); final BigDecimal chargeBasis = currentCharge.getTotalAmount(this);
final VATAmount chargeVatAmount = new VATAmount(chargeBasis, chargeBasis.multiply(taxPercent.divide(new BigDecimal(100))), vatCategoryCode, final VATAmount chargeVatAmount = new VATAmount(chargeBasis, chargeBasis.multiply(taxPercent.divide(new BigDecimal(100))), vatCategoryCode,
vatDueDateTypeCode, taxPercent); vatDueDateTypeCode, taxPercent);
if (currentChargeVatAmount.isEmpty()) if (currentChargeVatAmount.isEmpty()) {
{
vatAmounts.add(chargeVatAmount); vatAmounts.add(chargeVatAmount);
} } else {
else
{
this.mergeAdding(currentChargeVatAmount.get(), chargeVatAmount); this.mergeAdding(currentChargeVatAmount.get(), chargeVatAmount);
} }
} }
@@ -321,11 +309,9 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
} }
final IZUGFeRDAllowanceCharge[] allowances = this.trans.getZFAllowances(); final IZUGFeRDAllowanceCharge[] allowances = this.trans.getZFAllowances();
if (allowances != null) { if (allowances != null) {
for (final IZUGFeRDAllowanceCharge currentAllowance : allowances) for (final IZUGFeRDAllowanceCharge currentAllowance : allowances) {
{
final BigDecimal taxPercent = currentAllowance.getTaxPercent(); final BigDecimal taxPercent = currentAllowance.getTaxPercent();
if (taxPercent != null) if (taxPercent != null) {
{
final String vatCategoryCode = currentAllowance.getCategoryCode() != null ? currentAllowance.getCategoryCode() : "S"; final String vatCategoryCode = currentAllowance.getCategoryCode() != null ? currentAllowance.getCategoryCode() : "S";
final Optional<VATAmount> currentAllowanceVatAmount = this.getCurrentVatAmount(vatAmounts, vatCategoryCode, taxPercent); final Optional<VATAmount> currentAllowanceVatAmount = this.getCurrentVatAmount(vatAmounts, vatCategoryCode, taxPercent);
final BigDecimal allowanceNegativeBasis = currentAllowance.getTotalAmount(this).multiply(BigDecimal.valueOf(-1)); final BigDecimal allowanceNegativeBasis = currentAllowance.getTotalAmount(this).multiply(BigDecimal.valueOf(-1));
@@ -333,12 +319,9 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
allowanceNegativeBasis.multiply(taxPercent.divide(new BigDecimal(100))), allowanceNegativeBasis.multiply(taxPercent.divide(new BigDecimal(100))),
currentAllowance.getCategoryCode() != null ? currentAllowance.getCategoryCode() : "S", currentAllowance.getCategoryCode() != null ? currentAllowance.getCategoryCode() : "S",
vatDueDateTypeCode, taxPercent); vatDueDateTypeCode, taxPercent);
if (currentAllowanceVatAmount.isEmpty()) if (currentAllowanceVatAmount.isEmpty()) {
{
vatAmounts.add(allowanceVATAmount); vatAmounts.add(allowanceVATAmount);
} } else {
else
{
this.mergeAdding(currentAllowanceVatAmount.get(), allowanceVATAmount); this.mergeAdding(currentAllowanceVatAmount.get(), allowanceVATAmount);
} }
} }
@@ -347,12 +330,10 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
return vatAmounts; return vatAmounts;
} }
public void mergeAdding(VATAmount vatAmount, VATAmount toAdd) public void mergeAdding(VATAmount vatAmount, VATAmount toAdd) {
{
vatAmount.setBasis(vatAmount.getBasis().add(toAdd.getBasis())); vatAmount.setBasis(vatAmount.getBasis().add(toAdd.getBasis()));
vatAmount.setCalculated(vatAmount.getCalculated().add(toAdd.getCalculated())); 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( Optional.ofNullable(vatAmount.getVatExemptionReasonText()).filter(reasonText -> !reasonText.equals(toAdd.getVatExemptionReasonText())).ifPresentOrElse(
text -> vatAmount.setVatExemptionReasonText(String.join(", ", text, toAdd.getVatExemptionReasonText())), text -> vatAmount.setVatExemptionReasonText(String.join(", ", text, toAdd.getVatExemptionReasonText())),
() -> vatAmount.setVatExemptionReasonText(toAdd.getVatExemptionReasonText())); () -> vatAmount.setVatExemptionReasonText(toAdd.getVatExemptionReasonText()));
@@ -372,12 +353,11 @@ public class TransactionCalculator implements IAbsoluteValueProvider {
return getAllowancesForPercent(null).setScale(2, RoundingMode.HALF_UP); return getAllowancesForPercent(null).setScale(2, RoundingMode.HALF_UP);
} }
private Optional<VATAmount> getCurrentVatAmount(List<VATAmount> vatAmounts, String vatCategoryCode, BigDecimal percentage) private Optional<VATAmount> getCurrentVatAmount(List<VATAmount> vatAmounts, String vatCategoryCode, BigDecimal percentage) {
{
return vatAmounts.stream() return vatAmounts.stream()
.filter(va -> Objects.equals(vatCategoryCode, va.getCategoryCode()) .filter(va -> Objects.equals(vatCategoryCode, va.getCategoryCode())
&& Optional.ofNullable(percentage).map(p -> va.getApplicablePercent() == null && p == null || p.compareTo(va.getApplicablePercent()) == 0) && Optional.ofNullable(percentage).map(p -> va.getApplicablePercent() == null && p == null || p.compareTo(va.getApplicablePercent()) == 0)
.orElse(true)) .orElse(true))
.findFirst(); .findFirst();
} }

View File

@@ -538,8 +538,15 @@ public class ZUGFeRD2PullProvider implements IXMLProvider {
xml += "<ram:ExemptionReason>" + XMLTools.encodeXML(currentItem.getProduct().getTaxExemptionReason()) + "</ram:ExemptionReason>"; xml += "<ram:ExemptionReason>" + XMLTools.encodeXML(currentItem.getProduct().getTaxExemptionReason()) + "</ram:ExemptionReason>";
} }
xml += "<ram:CategoryCode>" + currentItem.getProduct().getTaxCategoryCode() + "</ram:CategoryCode>"; xml += "<ram:CategoryCode>" + currentItem.getProduct().getTaxCategoryCode() + "</ram:CategoryCode>";
if (!currentItem.getProduct().getTaxCategoryCode().equals(TaxCategoryCodeTypeConstants.UNTAXEDSERVICE)) { BigDecimal vatValue=BigDecimal.ZERO;
xml += "<ram:RateApplicablePercent>" + vatFormat(currentItem.getProduct().getVATPercent()) + "</ram:RateApplicablePercent>"; if (currentItem.getProduct().getTaxCategoryCode().equals(TaxCategoryCodeTypeConstants.ZEROTAXPRODUCTS)) {
vatValue=BigDecimal.ZERO;
} else {
vatValue=currentItem.getProduct().getVATPercent();
}
if ((!currentItem.getProduct().getTaxCategoryCode().equals(TaxCategoryCodeTypeConstants.UNTAXEDSERVICE)) ) {
xml += "<ram:RateApplicablePercent>" + vatFormat(vatValue) + "</ram:RateApplicablePercent>";
} }
xml += "</ram:ApplicableTradeTax>"; xml += "</ram:ApplicableTradeTax>";

View File

@@ -23,5 +23,6 @@ public class DocumentCodeTypeConstants {
public static final String CREDITNOTE = "381"; public static final String CREDITNOTE = "381";
public static final String DEBITNOTE = "84"; public static final String DEBITNOTE = "84";
public static final String CORRECTEDINVOICE = "384"; public static final String CORRECTEDINVOICE = "384";
public static final String SELFBILLING = "389";
public static final String PARTIAL_BILLING = "326"; public static final String PARTIAL_BILLING = "326";
} }

View File

@@ -0,0 +1,153 @@
/** **********************************************************************
*
* Copyright 2019 Jochen Staerk
*
* Use is subject to license terms.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*********************************************************************** */
package org.mustangproject.ZUGFeRD;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.mustangproject.*;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Date;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class VATrelatedTest extends ResourceCase {
final String TARGET_PDF_REVERSE = "./target/testout-ReverseCharge.pdf";
final String TARGET_PDF_Z = "./target/testout-TaxcodeZ.pdf";
public void testReverseCharge() {
String orgname = "Test company";
String number = "123";
String priceStr = "3.00";
BigDecimal price = new BigDecimal(priceStr);
try {
InputStream SOURCE_PDF = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf");
ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1();
ze.ignorePDFAErrors().load(SOURCE_PDF);
ze.setProfile(Profiles.getByName("Extended"));
ze.setProducer("My Application").setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2).setProfile("extended");
// ze.setTransaction(new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date()).setSender(new TradeParty(orgname,"teststr", "55232","teststadt","DE")).setOwnTaxID("4711").setOwnVATID("DE0815").setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE")).setNumber(number)
// .addItem(new Item(new Product("Testprodukt", "", "H84", new BigDecimal(19)), amount, new BigDecimal(1.0)).addAllowance(new Allowance().setPercent(new BigDecimal(50)))));
Product p=new Product("Testprodukt", "", "H87", new BigDecimal(19));
p.setReverseCharge();
Invoice i = new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date())
.setSender(new TradeParty(orgname, "teststr", "55232", "teststadt", "DE").addTaxID("4711").addVATID("DE0815"))
.setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE")
.setContact(new Contact("contact testname", "123456", "contact.testemail@example.org").setFax("0911623562")).addVATID("DE0915"))
.setNumber(number)
.addCharge(new Charge(new BigDecimal(1)).setReasonCode("ABK").setReason("AReason").setTaxPercent(new BigDecimal(19)))
.addItem(new Item(p, price, new BigDecimal(1.0)));
ze.setTransaction(i);
String theXML = new String(ze.getProvider().getXML(), StandardCharsets.UTF_8);
assertTrue(theXML.contains("<rsm:CrossIndustryInvoice"));
ze.export(TARGET_PDF_REVERSE);
} catch (IOException e) {
fail("IOException should not be raised");
}
try {
// now check the contents (like MustangReaderTest)
ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF_REVERSE);
assertEquals("EUR", zi.getInvoiceCurrencyCode());
assertTrue(zi.getUTF8().contains("0911623562")); // fax number
// Reading ZUGFeRD
assertEquals("4.19", zi.getAmount());
assertEquals(orgname, zi.getHolder());
assertEquals(number, zi.getForeignReference());
assertEquals(zi.getVersion(), 2);
} catch (Exception e) {
fail("Exception "+e.getMessage()+" should not be raised");
}
}
public void testTaxcodeZ() {
String orgname = "Test company";
String number = "123";
String priceStr = "3.00";
BigDecimal price = new BigDecimal(priceStr);
try {
InputStream SOURCE_PDF = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf");
ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1();
ze.ignorePDFAErrors().load(SOURCE_PDF);
ze.setProfile(Profiles.getByName("Extended"));
ze.setProducer("My Application").setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2).setProfile("extended");
// ze.setTransaction(new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date()).setSender(new TradeParty(orgname,"teststr", "55232","teststadt","DE")).setOwnTaxID("4711").setOwnVATID("DE0815").setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE")).setNumber(number)
// .addItem(new Item(new Product("Testprodukt", "", "H84", new BigDecimal(19)), amount, new BigDecimal(1.0)).addAllowance(new Allowance().setPercent(new BigDecimal(50)))));
Product p;
ObjectMapper mapper = new ObjectMapper();
String json="{\n" +
" \"unit\": \"H87\",\n" +
" \"name\": \"Joghurt Banane\",\n" +
" \"description\": \"\",\n" +
" \"taxCategoryCode\": \"Z\"\n" +
" }";// " \"vatpercent\": 0,\n" + missing, #979
p=mapper.readValue(json, Product.class);
Invoice i = new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date())
.setSender(new TradeParty(orgname, "teststr", "55232", "teststadt", "DE").addTaxID("4711").addVATID("DE0815"))
.setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE")
.setContact(new Contact("contact testname", "123456", "contact.testemail@example.org").setFax("0911623562")))
.setNumber(number)
.addCharge(new Charge(new BigDecimal(1)).setReasonCode("ABK").setReason("AReason").setTaxPercent(new BigDecimal(19)))
.addItem(new Item(p, price, new BigDecimal(1.0)));
ze.setTransaction(i);
String theXML = new String(ze.getProvider().getXML(), StandardCharsets.UTF_8);
assertTrue(theXML.contains("<rsm:CrossIndustryInvoice"));
ze.export(TARGET_PDF_Z);
} catch (IOException e) {
fail("IOException "+e.getMessage()+" should not be raised");
}
try {
// now check the contents (like MustangReaderTest)
ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF_Z);
assertEquals("EUR", zi.getInvoiceCurrencyCode());
assertTrue(zi.getUTF8().contains("0911623562")); // fax number
// Reading ZUGFeRD
assertEquals("4.19", zi.getAmount());
assertEquals(orgname, zi.getHolder());
assertEquals(number, zi.getForeignReference());
assertEquals(zi.getVersion(), 2);
} catch (Exception e) {
fail("Exception should not be raised");
}
}
}

View File

@@ -112,6 +112,44 @@ public class LibraryTest extends ResourceCase {
.isEqualTo("valid"); .isEqualTo("valid");
} }
public void testLibraryTaxcodeZ() {
File tempFile = new File("../library/target/testout-TaxcodeZ.pdf");
assertTrue(tempFile.exists());
ZUGFeRDValidator zfv = new ZUGFeRDValidator();
String res = zfv.validate(tempFile.getAbsolutePath());
assertThat(res).valueByXPath("/validation/pdf/summary/@status")
.isEqualTo("valid");
assertThat(res).valueByXPath("/validation/xml/summary/@status")
.isEqualTo("valid");
assertThat(res).valueByXPath("/validation/summary/@status")
.isEqualTo("valid");
}
public void testLibraryReverseCharge() {
File tempFile = new File("../library/target/testout-ReverseCharge.pdf");
assertTrue(tempFile.exists());
ZUGFeRDValidator zfv = new ZUGFeRDValidator();
String res = zfv.validate(tempFile.getAbsolutePath());
assertThat(res).valueByXPath("/validation/pdf/summary/@status")
.isEqualTo("valid");
assertThat(res).valueByXPath("/validation/xml/summary/@status")
.isEqualTo("valid");
assertThat(res).valueByXPath("/validation/summary/@status")
.isEqualTo("valid");
}
public void testLibraryPushAllowances() { public void testLibraryPushAllowances() {
File tempFile = new File("../library/target/testout-ZF2PushChargesAllowances.pdf"); File tempFile = new File("../library/target/testout-ZF2PushChargesAllowances.pdf");
assertTrue(tempFile.exists()); assertTrue(tempFile.exists());