|
| 1 | +# leitweg.py - functions for handling Leitweg-ID |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2025 Holvi Payment Services Oy |
| 5 | +# |
| 6 | +# This library is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU Lesser General Public |
| 8 | +# License as published by the Free Software Foundation; either |
| 9 | +# version 2.1 of the License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# This library is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public |
| 17 | +# License along with this library; if not, write to the Free Software |
| 18 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | +# 02110-1301 USA |
| 20 | + |
| 21 | +"""Leitweg-ID, a buyer reference or routing identifier for electronic invoices. |
| 22 | +
|
| 23 | +For the successful transmission of an electronic invoice as invoicing party or |
| 24 | +sender, a unique identification and addressing of the invoice recipient is |
| 25 | +required. The Leitweg-ID must be transmitted as a mandatory requirement for |
| 26 | +electronic invoicing to public contracting authorities in the federal |
| 27 | +administration. |
| 28 | +
|
| 29 | +More information: |
| 30 | +
|
| 31 | +* https://leitweg-id.de/ |
| 32 | +* https://de.wikipedia.org/wiki/Leitweg-ID |
| 33 | +* https://xeinkauf.de/app/uploads/2022/11/Leitweg-ID-Formatspezifikation-v2-0-2-1.pdf |
| 34 | +
|
| 35 | +>>> validate('991-03730-19') |
| 36 | +'991-03730-19' |
| 37 | +>>> validate('1-03730-19') |
| 38 | +Traceback (most recent call last): |
| 39 | + ... |
| 40 | +InvalidFormat: ... |
| 41 | +""" |
| 42 | + |
| 43 | +from __future__ import annotations |
| 44 | + |
| 45 | +import re |
| 46 | + |
| 47 | +from stdnum.exceptions import * |
| 48 | +from stdnum.iso7064 import mod_97_10 |
| 49 | + |
| 50 | + |
| 51 | +_pattern = re.compile(r'[0-9]{2,12}(-[0-9A-Z]{,30})?-[0-9]{2}') |
| 52 | + |
| 53 | + |
| 54 | +def compact(number: str) -> str: |
| 55 | + """ |
| 56 | + Convert the number to the minimal representation. This strips the |
| 57 | + number of any valid separators and removes surrounding whitespace. |
| 58 | + """ |
| 59 | + return number.strip().upper() # no valid separators, dashes part of the format |
| 60 | + |
| 61 | + |
| 62 | +def validate(number: str) -> str: |
| 63 | + """ |
| 64 | + Check if the number provided is valid. This checks the format, state or |
| 65 | + federal government code, and check digits. |
| 66 | + """ |
| 67 | + if not isinstance(number, str): |
| 68 | + raise InvalidFormat() |
| 69 | + number = compact(number) |
| 70 | + # 2.1 Bestandteile der Leitweg-ID |
| 71 | + if not 5 <= len(number) <= 46: |
| 72 | + raise InvalidLength() |
| 73 | + # 2.1 Bestandteile der Leitweg-ID |
| 74 | + if not re.fullmatch(_pattern, number): |
| 75 | + raise InvalidFormat() |
| 76 | + # 2.2.1 Kennzahl des Bundeslandes/des Bundes |
| 77 | + if not number[:2] in { |
| 78 | + '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', |
| 79 | + '11', '12', '13', '14', '15', '16', '99', |
| 80 | + }: |
| 81 | + raise InvalidComponent() |
| 82 | + # 2.4 Prüfziffer |
| 83 | + mod_97_10.validate(number.replace('-', '')) |
| 84 | + return number |
| 85 | + |
| 86 | + |
| 87 | +def is_valid(number: str) -> bool: |
| 88 | + """Check if the number provided is valid. This checks the length and |
| 89 | + check digit.""" |
| 90 | + try: |
| 91 | + return bool(validate(number)) |
| 92 | + except ValidationError: |
| 93 | + return False |
0 commit comments