From aa416d32b71fe3376dbd4a706a6942854c359d07 Mon Sep 17 00:00:00 2001 From: m3ta-chiron Date: Thu, 26 Nov 2026 07:29:29 +0100 Subject: [PATCH] fix(validator): accept genuine zero totals in the required-field check pflichtfelder rejected every invoice whose net, VAT or gross total was 0. The extractor reads an absent total as 0, so this could not tell a missing value from a real zero and rejected tax-exempt / reverse-charge invoices (VAT 0, e.g. customs agent invoices) and zero-sum invoices. A zero total now counts as missing only when the rest of the invoice implies another value: net vs. the line items, VAT total vs. the VAT breakdown, gross vs. net plus VAT. Closes zugferd-esf. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 +++-- src/validator.py | 44 +++++++++++++++++++++++++----- tests/test_validator.py | 59 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 100 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 01c3275..e113c04 100644 --- a/README.md +++ b/README.md @@ -316,12 +316,14 @@ The service supports four validation checks: Validates that all critical invoice fields are present and non-empty: -- **Critical errors:** invoice_number, invoice_date, supplier.name, supplier.vat_id (or supplier.tax_number), buyer.name, totals.net, totals.gross, totals.vat_total (each must be non-zero), line_items array, and per line item: description, quantity, unit_price, line_total +- **Critical errors:** invoice_number, invoice_date, supplier.name, supplier.vat_id (or supplier.tax_number), buyer.name, totals.net, totals.gross, totals.vat_total (a total of 0 counts as missing only when the invoice implies another value, see below), line_items array, and per line item: description, quantity, unit_price, line_total - **Critical on XRechnung only:** buyer_reference (BT-10, the Leitweg-ID), when `profile` is an `XRECHNUNG_*` profile - **Warnings:** due_date or payment_terms.iban present but empty, line item without vat_rate A line item quantity of 0 is accepted on text-only positions whose line total is 0 (e.g. a delivery note reference). It is an error only when the line bills an amount. +The extractor reads an absent total as 0, so a total of 0 is only reported as missing when the rest of the invoice contradicts it: `totals.net` when the line items do not sum to 0, `totals.vat_total` when the VAT breakdown does not sum to 0, `totals.gross` when net plus VAT is not 0. Tax-exempt and reverse-charge invoices (VAT 0) and zero-sum invoices therefore pass. + ### 2. betraege (Amount Calculations) Verifies all monetary calculations are correct: @@ -531,7 +533,7 @@ Check that all required fields are present: - invoice_date (YYYY-MM-DD format) - supplier.name and supplier.vat_id (or supplier.tax_number — EN 16931 BR-CO-26 requires one of both) - buyer.name -- Non-zero totals (net, gross, vat_total) +- Totals (net, gross, vat_total) that are 0 although line items, VAT breakdown or net plus VAT imply another value - At least one line item with description, unit_price and line_total; quantity may only be 0 when the line total is 0 **VAT ID validation fails** diff --git a/src/validator.py b/src/validator.py index 5498bc9..91df9cf 100644 --- a/src/validator.py +++ b/src/validator.py @@ -94,14 +94,46 @@ def validate_pflichtfelder( ), ) - if xml_data.totals.net == 0: - add_error("totals.net", "critical") + # The extractor reads an absent total as 0.0, so 0 alone cannot tell + # "missing" from a genuine zero (tax-exempt or reverse-charge VAT, a + # zero-sum invoice). A zero total only counts as missing when the rest of + # the invoice implies a non-zero value. + totals = xml_data.totals + line_total_sum = sum( + item.line_total for item in xml_data.line_items if item.line_total is not None + ) + vat_breakdown_sum = sum(vb.amount for vb in totals.vat_breakdown) - if xml_data.totals.gross == 0: - add_error("totals.gross", "critical") + if totals.net == 0 and not amounts_match(line_total_sum, 0.0): + add_error( + "totals.net", + "critical", + message=( + "Net total (BT-109) is missing or 0 although the line items " + f"sum to {line_total_sum:.2f}" + ), + ) - if xml_data.totals.vat_total == 0: - add_error("totals.vat_total", "critical") + if totals.vat_total == 0 and not amounts_match(vat_breakdown_sum, 0.0): + add_error( + "totals.vat_total", + "critical", + message=( + "VAT total (BT-110) is missing or 0 although the VAT breakdown " + f"sums to {vat_breakdown_sum:.2f}" + ), + ) + + expected_gross = totals.net + totals.vat_total + if totals.gross == 0 and not amounts_match(expected_gross, 0.0): + add_error( + "totals.gross", + "critical", + message=( + "Gross total (BT-112) is missing or 0 although net plus VAT " + f"is {expected_gross:.2f}" + ), + ) # Warning fields if xml_data.due_date is not None and not xml_data.due_date.strip(): diff --git a/tests/test_validator.py b/tests/test_validator.py index 271df15..acdb80c 100644 --- a/tests/test_validator.py +++ b/tests/test_validator.py @@ -288,12 +288,69 @@ class TestValidatePflichtfelder: line_total=10.0, ) ], - totals=Totals(line_total_sum=10.0, net=10.0, vat_total=0.0, gross=10.0), + totals=Totals( + line_total_sum=10.0, + net=10.0, + vat_total=0.0, + gross=11.9, + vat_breakdown=[VatBreakdown(rate=19.0, base=10.0, amount=1.9)], + ), ) errors = validate_pflichtfelder(xml_data) assert any(e.field == "totals.vat_total" for e in errors) assert any(e.severity == "critical" for e in errors) + @staticmethod + def _zero_amount_invoice(totals: Totals, line_total: float = 10.0) -> XmlData: + return XmlData( + invoice_number="INV001", + invoice_date="2024-01-15", + supplier=Supplier(name="Test Supplier GmbH", vat_id="DE123456789"), + buyer=Buyer(name="Test Buyer AG"), + line_items=[ + LineItem( + position=1, + description="Test Product", + quantity=1.0, + unit="Stück", + unit_price=line_total, + line_total=line_total, + ) + ], + totals=totals, + ) + + def test_zero_vat_total_on_tax_exempt_invoice_is_valid(self): + """0 EUR VAT is a real value on exempt / reverse-charge invoices.""" + xml_data = self._zero_amount_invoice( + Totals( + line_total_sum=10.0, + net=10.0, + vat_total=0.0, + gross=10.0, + vat_breakdown=[VatBreakdown(rate=0.0, base=10.0, amount=0.0)], + ) + ) + errors = validate_pflichtfelder(xml_data) + assert not any(e.field and e.field.startswith("totals") for e in errors) + + def test_zero_vat_total_without_breakdown_is_valid(self): + """A missing BT-110 is read as 0 and accepted when nothing contradicts it.""" + xml_data = self._zero_amount_invoice( + Totals(line_total_sum=10.0, net=10.0, vat_total=0.0, gross=10.0) + ) + errors = validate_pflichtfelder(xml_data) + assert not any(e.field == "totals.vat_total" for e in errors) + + def test_zero_net_and_gross_on_zero_invoice_are_valid(self): + """A zero-sum invoice legitimately carries net = gross = 0.""" + xml_data = self._zero_amount_invoice( + Totals(line_total_sum=0.0, net=0.0, vat_total=0.0, gross=0.0), + line_total=0.0, + ) + errors = validate_pflichtfelder(xml_data) + assert not any(e.field and e.field.startswith("totals") for e in errors) + def test_missing_due_date_warning(self): """Missing due_date should produce warning, not critical error.""" xml_data = XmlData(