feat: extract due date and buyer reference, require it on XRechnung
- CII: due_date is read from the payment due date (BT-9) instead of the delivery terms description, normalized to YYYY-MM-DD - new field buyer_reference (BT-10, the Leitweg-ID) for CII and UBL - /validate accepts an optional profile; on XRECHNUNG_* profiles a missing buyer reference is a critical error (BR-DE-15) Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
+38
-3
@@ -15,8 +15,30 @@ from src.models import (
|
||||
from src.utils import amounts_match
|
||||
|
||||
|
||||
def validate_pflichtfelder(xml_data: XmlData) -> list[ErrorDetail]:
|
||||
"""Check required fields are present."""
|
||||
def is_xrechnung_profile(profile: str | None) -> bool:
|
||||
"""Check whether a profile name denotes an XRechnung invoice.
|
||||
|
||||
Args:
|
||||
profile: Profile as returned by /extract (e.g. 'XRECHNUNG_3.0')
|
||||
|
||||
Returns:
|
||||
True for XRechnung profiles
|
||||
"""
|
||||
return bool(profile) and profile.strip().upper().startswith("XRECHNUNG")
|
||||
|
||||
|
||||
def validate_pflichtfelder(
|
||||
xml_data: XmlData, profile: str | None = None
|
||||
) -> list[ErrorDetail]:
|
||||
"""Check required fields are present.
|
||||
|
||||
Args:
|
||||
xml_data: Parsed invoice data
|
||||
profile: Invoice profile; enables profile-specific required fields
|
||||
|
||||
Returns:
|
||||
List of errors and warnings for missing fields
|
||||
"""
|
||||
errors = []
|
||||
|
||||
def add_error(field: str, severity: str, message: str | None = None) -> None:
|
||||
@@ -58,6 +80,19 @@ def validate_pflichtfelder(xml_data: XmlData) -> list[ErrorDetail]:
|
||||
if not xml_data.buyer.name or not xml_data.buyer.name.strip():
|
||||
add_error("buyer.name", "critical")
|
||||
|
||||
# XRechnung BR-DE-15: the buyer reference (BT-10, Leitweg-ID) is mandatory
|
||||
if is_xrechnung_profile(profile) and not (
|
||||
xml_data.buyer_reference and xml_data.buyer_reference.strip()
|
||||
):
|
||||
add_error(
|
||||
"buyer_reference",
|
||||
"critical",
|
||||
message=(
|
||||
"XRechnung requires a buyer reference (BT-10, Leitweg-ID) "
|
||||
"in 'buyer_reference'"
|
||||
),
|
||||
)
|
||||
|
||||
if xml_data.totals.net == 0:
|
||||
add_error("totals.net", "critical")
|
||||
|
||||
@@ -337,7 +372,7 @@ def validate_invoice(request: ValidateRequest) -> ValidationResult:
|
||||
check_errors: list[ErrorDetail] = []
|
||||
|
||||
if check_name == "pflichtfelder":
|
||||
check_errors = validate_pflichtfelder(xml_data)
|
||||
check_errors = validate_pflichtfelder(xml_data, request.profile)
|
||||
checks_run += 1
|
||||
elif check_name == "betraege":
|
||||
check_errors = validate_betraege(xml_data)
|
||||
|
||||
Reference in New Issue
Block a user