From 6126a19bb7014dfb0e7a4422b51475773eb3be33 Mon Sep 17 00:00:00 2001 From: m3ta-chiron Date: Wed, 11 Nov 2026 18:32:20 +0100 Subject: [PATCH] fix(validator): accept zero quantity on text-only line items A line with quantity 0 was always reported as a missing required field. Real invoices carry text-only positions (e.g. a delivery note reference) with quantity 0 and line total 0. Quantity 0 is now an error only when the line bills an amount. Co-Authored-By: Claude Fable 5.2 --- src/validator.py | 5 +++- tests/test_validator.py | 59 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/validator.py b/src/validator.py index 75e5b1a..4522ff0 100644 --- a/src/validator.py +++ b/src/validator.py @@ -88,7 +88,10 @@ def validate_pflichtfelder(xml_data: XmlData) -> list[ErrorDetail]: if not item.description or not item.description.strip(): add_error(f"{field_prefix}.description", "critical") - if item.quantity == 0: + # Quantity 0 is legitimate on text-only positions (e.g. a delivery + # note reference) that carry no amount; it is only a defect when + # the line bills something. + if item.quantity == 0 and item.line_total != 0: add_error(f"{field_prefix}.quantity", "critical") if item.unit_price is None: diff --git a/tests/test_validator.py b/tests/test_validator.py index c8eb3ff..68e64fd 100644 --- a/tests/test_validator.py +++ b/tests/test_validator.py @@ -424,6 +424,65 @@ class TestValidatePflichtfelder: assert not any(e.field == "line_items[0].unit_price" for e in errors) assert not any(e.field == "line_items[0].line_total" for e in errors) + def test_zero_quantity_text_position_allowed(self): + """Text-only position (quantity 0, amount 0) must not be an error. + + Regression: real invoice with a 'Lieferschein ... vom ...' reference + line was rejected as missing line_items[0].quantity. + """ + xml_data = 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="Lieferschein 4711 vom 15.01.2024", + quantity=0.0, + unit="H87", + unit_price=0.0, + line_total=0.0, + vat_rate=19.0, + ), + LineItem( + position=2, + description="Test Product", + quantity=1.0, + unit="H87", + unit_price=9.96, + line_total=9.96, + vat_rate=19.0, + ), + ], + totals=Totals(line_total_sum=9.96, net=9.96, vat_total=1.89, gross=11.85), + ) + errors = validate_pflichtfelder(xml_data) + assert errors == [] + + def test_zero_euro_line_item_amounts_allowed(self): + """Zero-valued line item prices should count as present.""" + xml_data = 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=0.0, + line_total=0.0, + ) + ], + totals=Totals(line_total_sum=10.0, net=10.0, vat_total=19.0, gross=29.0), + ) + errors = validate_pflichtfelder(xml_data) + assert not any(e.field == "line_items[0].unit_price" for e in errors) + assert not any(e.field == "line_items[0].line_total" for e in errors) + def test_missing_line_item_unit_price_critical(self): """Missing line_item.unit_price should produce critical error.""" xml_data = XmlData(