xrechnung-validator 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xrechnung_validator/__init__.py +130 -0
- xrechnung_validator/__main__.py +18 -0
- xrechnung_validator-0.1.0.dist-info/METADATA +40 -0
- xrechnung_validator-0.1.0.dist-info/RECORD +8 -0
- xrechnung_validator-0.1.0.dist-info/WHEEL +5 -0
- xrechnung_validator-0.1.0.dist-info/entry_points.txt +2 -0
- xrechnung_validator-0.1.0.dist-info/licenses/LICENSE +13 -0
- xrechnung_validator-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Zero-dependency German XRechnung (UBL 2.1 & CII) validator against EN 16931 + BR-DE rules."""
|
|
2
|
+
import re
|
|
3
|
+
import xml.etree.ElementTree as ET
|
|
4
|
+
from decimal import Decimal, InvalidOperation
|
|
5
|
+
|
|
6
|
+
_IBAN_RE = re.compile(r"^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$")
|
|
7
|
+
_LEITWEG_RE = re.compile(r"^[0-9]{2,12}(-[A-Za-z0-9]{1,30}){1,2}(-[0-9]{2})?$")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _dec(v) -> Decimal:
|
|
11
|
+
try:
|
|
12
|
+
return Decimal(str(v or "0").strip().replace(",", "."))
|
|
13
|
+
except InvalidOperation:
|
|
14
|
+
return Decimal("0")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _local(t: str) -> str:
|
|
18
|
+
return t.split("}", 1)[1] if "}" in t else t
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _first(root, name: str):
|
|
22
|
+
if root is None:
|
|
23
|
+
return None
|
|
24
|
+
for el in root.iter():
|
|
25
|
+
if _local(el.tag) == name:
|
|
26
|
+
return el
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _txt(root, name: str) -> str:
|
|
31
|
+
el = _first(root, name)
|
|
32
|
+
return (el.text or "").strip() if el is not None else ""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _child_txt(root, name: str) -> str:
|
|
36
|
+
if root is None:
|
|
37
|
+
return ""
|
|
38
|
+
for ch in root:
|
|
39
|
+
if _local(ch.tag) == name:
|
|
40
|
+
return (ch.text or "").strip()
|
|
41
|
+
return ""
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def validate_xrechnung(filename: str, raw: bytes) -> dict:
|
|
45
|
+
try:
|
|
46
|
+
root = ET.fromstring(raw)
|
|
47
|
+
except ET.ParseError as e:
|
|
48
|
+
return {"file": filename, "syntax": "INVALID", "valid": False, "errors": 1, "warnings": 0,
|
|
49
|
+
"meta": {}, "checks": [{"rule": "XML-PARSE", "severity": "ERROR", "status": "FAIL", "message": str(e)}]}
|
|
50
|
+
|
|
51
|
+
rtag = _local(root.tag)
|
|
52
|
+
if rtag in ("Invoice", "CreditNote"):
|
|
53
|
+
syntax = f"UBL 2.1 ({rtag})"
|
|
54
|
+
spec_id = _child_txt(root, "CustomizationID")
|
|
55
|
+
inv_id = _child_txt(root, "ID")
|
|
56
|
+
issue_date = _child_txt(root, "IssueDate")
|
|
57
|
+
currency = _child_txt(root, "DocumentCurrencyCode")
|
|
58
|
+
buyer_ref = _child_txt(root, "BuyerReference")
|
|
59
|
+
sup = _first(root, "AccountingSupplierParty")
|
|
60
|
+
cust = _first(root, "AccountingCustomerParty")
|
|
61
|
+
seller_name = _txt(sup, "RegistrationName") or _txt(sup, "Name")
|
|
62
|
+
seller_city, seller_zip = _txt(sup, "CityName"), _txt(sup, "PostalZone")
|
|
63
|
+
sct = _first(sup, "Contact")
|
|
64
|
+
sct_name, sct_tel, sct_mail = _txt(sct, "Name"), _txt(sct, "Telephone"), _txt(sct, "ElectronicMail")
|
|
65
|
+
buyer_name = _txt(cust, "RegistrationName") or _txt(cust, "Name")
|
|
66
|
+
buyer_city, buyer_zip = _txt(cust, "CityName"), _txt(cust, "PostalZone")
|
|
67
|
+
pm = _first(root, "PaymentMeans")
|
|
68
|
+
pm_code = _txt(pm, "PaymentMeansCode")
|
|
69
|
+
iban = _txt(_first(pm, "PayeeFinancialAccount"), "ID")
|
|
70
|
+
lmt = _first(root, "LegalMonetaryTotal")
|
|
71
|
+
net = _dec(_txt(lmt, "TaxExclusiveAmount") or _txt(lmt, "LineExtensionAmount"))
|
|
72
|
+
gross = _dec(_txt(lmt, "TaxInclusiveAmount"))
|
|
73
|
+
tax = _dec(_child_txt(_first(root, "TaxTotal"), "TaxAmount"))
|
|
74
|
+
elif rtag == "CrossIndustryInvoice":
|
|
75
|
+
syntax = "UN/CEFACT CII"
|
|
76
|
+
spec_id = _txt(_first(root, "ExchangedDocumentContext"), "ID")
|
|
77
|
+
doc = _first(root, "ExchangedDocument")
|
|
78
|
+
inv_id = _child_txt(doc, "ID")
|
|
79
|
+
rd = _txt(doc, "DateTimeString")
|
|
80
|
+
issue_date = f"{rd[:4]}-{rd[4:6]}-{rd[6:8]}" if len(rd) == 8 and rd.isdigit() else rd
|
|
81
|
+
agr = _first(root, "ApplicableHeaderTradeAgreement")
|
|
82
|
+
buyer_ref = _child_txt(agr, "BuyerReference")
|
|
83
|
+
sup, cust = _first(agr, "SellerTradeParty"), _first(agr, "BuyerTradeParty")
|
|
84
|
+
seller_name = _child_txt(sup, "Name")
|
|
85
|
+
seller_city, seller_zip = _txt(sup, "CityName"), _txt(sup, "PostcodeCode")
|
|
86
|
+
sct = _first(sup, "DefinedTradeContact")
|
|
87
|
+
sct_name = _txt(sct, "PersonName") or _txt(sct, "DepartmentName")
|
|
88
|
+
sct_tel, sct_mail = _txt(sct, "CompleteNumber"), _txt(sct, "URIID")
|
|
89
|
+
buyer_name = _child_txt(cust, "Name")
|
|
90
|
+
buyer_city, buyer_zip = _txt(cust, "CityName"), _txt(cust, "PostcodeCode")
|
|
91
|
+
st = _first(root, "ApplicableHeaderTradeSettlement")
|
|
92
|
+
currency = _child_txt(st, "InvoiceCurrencyCode")
|
|
93
|
+
pm = _first(st, "SpecifiedTradeSettlementPaymentMeans")
|
|
94
|
+
pm_code, iban = _child_txt(pm, "TypeCode"), _txt(pm, "IBANID")
|
|
95
|
+
sm = _first(st, "SpecifiedTradeSettlementHeaderMonetarySummation")
|
|
96
|
+
net, tax, gross = _dec(_child_txt(sm, "TaxBasisTotalAmount")), _dec(_child_txt(sm, "TaxTotalAmount")), _dec(_child_txt(sm, "GrandTotalAmount"))
|
|
97
|
+
else:
|
|
98
|
+
return {"file": filename, "syntax": f"UNKNOWN ({rtag})", "valid": False, "errors": 1, "warnings": 0,
|
|
99
|
+
"meta": {}, "checks": [{"rule": "XR-SYNTAX", "severity": "ERROR", "status": "FAIL", "message": f"Unsupported root <{rtag}>"}]}
|
|
100
|
+
|
|
101
|
+
checks = []
|
|
102
|
+
def chk(rule, cond, ok_m, fail_m, sev="ERROR"):
|
|
103
|
+
checks.append({"rule": rule, "severity": sev, "status": "PASS" if cond else "FAIL", "message": ok_m if cond else fail_m})
|
|
104
|
+
|
|
105
|
+
chk("BR-01", "xrechnung" in spec_id.lower() or "en16931" in spec_id.lower(), spec_id, "Missing XRechnung/EN16931 CustomizationID")
|
|
106
|
+
chk("BR-02", bool(inv_id), inv_id, "Missing Invoice ID")
|
|
107
|
+
chk("BR-03", bool(re.match(r"^\d{4}-\d{2}-\d{2}$", issue_date)), issue_date, "Invalid IssueDate")
|
|
108
|
+
chk("BR-05", bool(re.match(r"^[A-Z]{3}$", currency)), currency, "Invalid currency")
|
|
109
|
+
chk("BR-06", bool(seller_name), seller_name, "Missing Seller Name")
|
|
110
|
+
chk("BR-07", bool(buyer_name), buyer_name, "Missing Buyer Name")
|
|
111
|
+
chk("BR-DE-15", bool(buyer_ref), buyer_ref, "Missing BuyerReference / Leitweg-ID")
|
|
112
|
+
if buyer_ref and not _LEITWEG_RE.match(buyer_ref):
|
|
113
|
+
chk("BR-DE-15-FMT", False, "", f"Non-public-sector Leitweg-ID format: {buyer_ref}", "WARN")
|
|
114
|
+
chk("BR-DE-5", bool(sct_name), sct_name, "Missing Seller Contact Name")
|
|
115
|
+
chk("BR-DE-6", bool(sct_tel), sct_tel, "Missing Seller Contact Phone")
|
|
116
|
+
chk("BR-DE-7", bool(sct_mail and "@" in sct_mail), sct_mail, "Missing Seller Contact Email")
|
|
117
|
+
chk("BR-DE-3/4", bool(seller_city and seller_zip), f"{seller_zip} {seller_city}", "Missing Seller City/ZIP")
|
|
118
|
+
chk("BR-DE-8/9", bool(buyer_city and buyer_zip), f"{buyer_zip} {buyer_city}", "Missing Buyer City/ZIP")
|
|
119
|
+
chk("BR-DE-1", bool(pm_code), pm_code, "Missing PaymentMeansCode")
|
|
120
|
+
if pm_code in ("30", "58"):
|
|
121
|
+
ic = re.sub(r"\s+", "", iban).upper()
|
|
122
|
+
chk("BR-DE-19", bool(_IBAN_RE.match(ic)), ic, f"Invalid IBAN '{iban}'")
|
|
123
|
+
chk("BR-CO-15", abs((net + tax) - gross) <= Decimal("0.02"), f"{net:.2f} + {tax:.2f} = {gross:.2f}", f"Net+Tax != Gross ({net}+{tax}!={gross})")
|
|
124
|
+
|
|
125
|
+
errs = sum(1 for c in checks if c["status"] == "FAIL" and c["severity"] == "ERROR")
|
|
126
|
+
warns = sum(1 for c in checks if c["status"] == "FAIL" and c["severity"] == "WARN")
|
|
127
|
+
return {"file": filename, "syntax": syntax, "valid": errs == 0, "errors": errs, "warnings": warns,
|
|
128
|
+
"meta": {"invoice_id": inv_id, "issue_date": issue_date, "currency": currency, "buyer_reference": buyer_ref,
|
|
129
|
+
"seller": seller_name, "buyer": buyer_name, "net": float(net), "tax": float(tax), "gross": float(gross)},
|
|
130
|
+
"checks": checks}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from . import validate_xrechnung
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def main():
|
|
9
|
+
p = argparse.ArgumentParser(description="Validate German XRechnung (UBL 2.1 / CII) XML e-invoices.")
|
|
10
|
+
p.add_argument("input", help="Path to XRechnung XML file")
|
|
11
|
+
args = p.parse_args()
|
|
12
|
+
res = validate_xrechnung(args.input, Path(args.input).read_bytes())
|
|
13
|
+
print(json.dumps(res, indent=2, ensure_ascii=False))
|
|
14
|
+
sys.exit(0 if res["valid"] else 1)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
if __name__ == "__main__":
|
|
18
|
+
main()
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xrechnung-validator
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Zero-dependency Python validator & CLI for German XRechnung 2.x/3.0 XML e-invoices (UBL 2.1 & CII) against EN 16931 + BR-DE rules.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://radarbg.com/instant/xrechnung-validator?ref=pypi-xrechnung
|
|
7
|
+
Project-URL: XRechnung Validator Online, https://radarbg.com/instant/xrechnung-validator?ref=pypi-xrechnung
|
|
8
|
+
Project-URL: RadarBG Accounting Tools, https://radarbg.com/instant?ref=pypi
|
|
9
|
+
Keywords: xrechnung,en16931,einvoice,ubl,cii,leitweg-id,validator,germany,accounting
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Topic :: Office/Business :: Financial :: Accounting
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# xrechnung-validator
|
|
20
|
+
|
|
21
|
+
Zero-dependency Python validator and CLI for German **XRechnung 2.x / 3.0** XML e-invoices (**OASIS UBL 2.1** and **UN/CEFACT CII**) checking **EN 16931 + German CIUS `BR-DE` rules** (Leitweg-ID, Seller/Buyer contact & address, IBAN, tax & line-item arithmetic).
|
|
22
|
+
|
|
23
|
+
Need an instant browser validator with downloadable **Excel reports & printable HTML invoice views**?
|
|
24
|
+
Use **[radarbg.com/instant/xrechnung-validator](https://radarbg.com/instant/xrechnung-validator?ref=pypi-xrechnung)**
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
```bash
|
|
28
|
+
pip install xrechnung-validator
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
```python
|
|
33
|
+
from xrechnung_validator import validate_xrechnung
|
|
34
|
+
|
|
35
|
+
report = validate_xrechnung("invoice.xml", open("invoice.xml", "rb").read())
|
|
36
|
+
print("Valid:", report["valid"], "Errors:", report["errors"])
|
|
37
|
+
```
|
|
38
|
+
```bash
|
|
39
|
+
xrechnung-validator invoice.xml
|
|
40
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
xrechnung_validator/__init__.py,sha256=6WUcGAw6KJijgrS8RVyyJUgoW5hGR1V-JSVLhuiA26c,6889
|
|
2
|
+
xrechnung_validator/__main__.py,sha256=jWRJs4lAPlZiAFSB618rMgO9i80M4t-R0qAPZvwVe_4,513
|
|
3
|
+
xrechnung_validator-0.1.0.dist-info/licenses/LICENSE,sha256=qiCKcvG8iDmL2tU6dGRonI0nLWUc9wewO6osh3LjSXM,573
|
|
4
|
+
xrechnung_validator-0.1.0.dist-info/METADATA,sha256=xh1pe2dbvD4dRlC56TaFZKPK0fXkGKGIZxtAMRc0f98,1719
|
|
5
|
+
xrechnung_validator-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
xrechnung_validator-0.1.0.dist-info/entry_points.txt,sha256=NAs_2TUIL2KciCJu1WsHkd1vdqQZ-ODO1zh9ARRW2g4,74
|
|
7
|
+
xrechnung_validator-0.1.0.dist-info/top_level.txt,sha256=Gc6abZfgk9xhmIG16nLwL56UElJIOMTUvrCqCl6sH6s,20
|
|
8
|
+
xrechnung_validator-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RadarBG (radarbg.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
13
|
+
IMPLIED.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
xrechnung_validator
|