fix: extract tax number, price basis quantity, spaced VAT IDs and contact email

- supplier/buyer: extract FC tax registration (BT-32/BT-49) into new
  tax_number field; previously only VA (USt-IdNr.) was read
- pflichtfelder: critical error only when neither vat_id nor tax_number
  is present (EN 16931 BR-CO-26)
- ustid: tolerate formatting whitespace inside VAT IDs (e.g. 'DE 140 978 617')
- line items: normalize unit price by basis quantity (BT-149), so a
  price per 100 units no longer inflates line totals by factor 100
- supplier email: also look in DefinedTradeContact (BT-42)
- rewrite xrechnung fallback test to assert only our own behavior,
  independent of factur-x version specifics
This commit is contained in:
2026-09-11 16:05:41 +02:00
parent e2e1d8c046
commit 7fcb3ff21d
7 changed files with 294 additions and 21 deletions
+32 -1
View File
@@ -104,11 +104,23 @@ def parse_supplier(xml_root: etree._Element) -> Supplier:
"ram:ID[@schemeID='VA']/text()",
namespaces=NAMESPACES,
)
tax_number = xml_root.xpath(
"//ram:ApplicableHeaderTradeAgreement/ram:SellerTradeParty/ram:SpecifiedTaxRegistration/"
"ram:ID[@schemeID='FC']/text()",
namespaces=NAMESPACES,
)
email = xml_root.xpath(
"//ram:ApplicableHeaderTradeAgreement/ram:SellerTradeParty/"
"ram:URIUniversalCommunication/ram:URIID/text()",
namespaces=NAMESPACES,
)
# Fallback: email address from the seller's defined trade contact (BT-42)
if not email:
email = xml_root.xpath(
"//ram:ApplicableHeaderTradeAgreement/ram:SellerTradeParty/"
"ram:DefinedTradeContact/ram:EmailURIUniversalCommunication/ram:URIID/text()",
namespaces=NAMESPACES,
)
return Supplier(
name=name[0] if name else "",
@@ -117,6 +129,7 @@ def parse_supplier(xml_root: etree._Element) -> Supplier:
city=city[0] if city else None,
country=country[0] if country else None,
vat_id=vat_id[0] if vat_id else None,
tax_number=tax_number[0] if tax_number else None,
email=email[0] if email else None,
)
@@ -159,6 +172,11 @@ def parse_buyer(xml_root: etree._Element) -> Buyer:
"ram:ID[@schemeID='VA']/text()",
namespaces=NAMESPACES,
)
tax_number = xml_root.xpath(
"//ram:ApplicableHeaderTradeAgreement/ram:BuyerTradeParty/ram:SpecifiedTaxRegistration/"
"ram:ID[@schemeID='FC']/text()",
namespaces=NAMESPACES,
)
return Buyer(
name=name[0] if name else "",
@@ -167,6 +185,7 @@ def parse_buyer(xml_root: etree._Element) -> Buyer:
city=city[0] if city else None,
country=country[0] if country else None,
vat_id=vat_id[0] if vat_id else None,
tax_number=tax_number[0] if tax_number else None,
)
@@ -209,6 +228,11 @@ def parse_line_items(xml_root: etree._Element) -> list[LineItem]:
"./ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/ram:ChargeAmount/text()",
namespaces=NAMESPACES,
)
basis_quantity = item_node.xpath(
"./ram:SpecifiedLineTradeAgreement/ram:NetPriceProductTradePrice/"
"ram:BasisQuantity/text()",
namespaces=NAMESPACES,
)
line_total = item_node.xpath(
"./ram:SpecifiedLineTradeSettlement/ram:SpecifiedTradeSettlementLineMonetarySummation/"
"ram:LineTotalAmount/text()",
@@ -225,6 +249,13 @@ def parse_line_items(xml_root: etree._Element) -> list[LineItem]:
unit = unit_code[0] if unit_code else "Stück"
# Basis quantity (BT-149): charge amount applies to N units (e.g. price per 100)
effective_unit_price = float(unit_price[0]) if unit_price else None
if effective_unit_price is not None and basis_quantity:
base = float(basis_quantity[0])
if base > 0:
effective_unit_price = effective_unit_price / base
items.append(
LineItem(
position=position,
@@ -235,7 +266,7 @@ def parse_line_items(xml_root: etree._Element) -> list[LineItem]:
description=description[0] if description else "",
quantity=float(quantity[0]) if quantity else 0.0,
unit=unit,
unit_price=float(unit_price[0]) if unit_price else None,
unit_price=effective_unit_price,
line_total=float(line_total[0]) if line_total else None,
vat_rate=float(vat_rate[0]) if vat_rate else None,
vat_amount=float(vat_amount[0]) if vat_amount else None,
+6
View File
@@ -24,6 +24,9 @@ class Supplier(BaseModel):
city: str | None = Field(default=None, description="City")
country: str | None = Field(default=None, description="Country code")
vat_id: str | None = Field(default=None, description="VAT ID")
tax_number: str | None = Field(
default=None, description="Tax number (Steuernummer, BT-32)"
)
email: str | None = Field(default=None, description="Email address")
@@ -36,6 +39,9 @@ class Buyer(BaseModel):
city: str | None = Field(default=None, description="City")
country: str | None = Field(default=None, description="Country code")
vat_id: str | None = Field(default=None, description="VAT ID")
tax_number: str | None = Field(
default=None, description="Tax number (Steuernummer, BT-49)"
)
class VatBreakdown(BaseModel):
+18 -5
View File
@@ -19,13 +19,13 @@ def validate_pflichtfelder(xml_data: XmlData) -> list[ErrorDetail]:
"""Check required fields are present."""
errors = []
def add_error(field: str, severity: str) -> None:
def add_error(field: str, severity: str, message: str | None = None) -> None:
errors.append(
ErrorDetail(
check="pflichtfelder",
field=field,
error_code="missing_required",
message=f"Required field '{field}' is missing or empty",
message=message or f"Required field '{field}' is missing or empty",
severity=severity,
)
)
@@ -40,8 +40,20 @@ def validate_pflichtfelder(xml_data: XmlData) -> list[ErrorDetail]:
if not xml_data.supplier.name or not xml_data.supplier.name.strip():
add_error("supplier.name", "critical")
if not xml_data.supplier.vat_id or not xml_data.supplier.vat_id.strip():
add_error("supplier.vat_id", "critical")
# EN 16931 BR-CO-26: seller identified by VAT ID (BT-31) OR tax number (BT-32)
has_vat_id = bool(xml_data.supplier.vat_id and xml_data.supplier.vat_id.strip())
has_tax_number = bool(
xml_data.supplier.tax_number and xml_data.supplier.tax_number.strip()
)
if not has_vat_id and not has_tax_number:
add_error(
"supplier.vat_id",
"critical",
message=(
"Supplier must have a VAT ID (supplier.vat_id) "
"or a tax number (supplier.tax_number)"
),
)
if not xml_data.buyer.name or not xml_data.buyer.name.strip():
add_error("buyer.name", "critical")
@@ -163,7 +175,8 @@ def validate_ustid(vat_id: str) -> ErrorDetail | None:
severity="critical",
)
vat_id = vat_id.strip()
# Whitespace inside VAT IDs is formatting only (e.g. "DE 140 978 617")
vat_id = re.sub(r"\s+", "", vat_id)
# German VAT ID: DE followed by 9 digits
if vat_id.startswith("DE"):