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 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 18:32:20 +01:00
co-authored by Claude Fable 5.2
parent fead1534c7
commit 6126a19bb7
2 changed files with 63 additions and 1 deletions
+4 -1
View File
@@ -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:
+59
View File
@@ -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(