|
| 1 | +# ogm_vcs.py - functions for handling Belgian OGM-VCS |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2025 Cédric Krier |
| 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 | +"""Belgian OGM-VCS. |
| 22 | +
|
| 23 | +The OGM-VCS is used in bank transfer as structured communication. |
| 24 | +
|
| 25 | +* https://febelfin.be/en/publications/2023/febelfin-banking-standards-for-online-banking |
| 26 | +
|
| 27 | +>>> compact('+++010/8068/17183+++') |
| 28 | +'010806817183' |
| 29 | +>>> validate('+++010/8068/17183+++') |
| 30 | +'010806817183' |
| 31 | +>>> validate('foo') |
| 32 | +Traceback (most recent call last): |
| 33 | + ... |
| 34 | +InvalidFormat: ... |
| 35 | +>>> validate('010/8068/1718') |
| 36 | +Traceback (most recent call last): |
| 37 | + ... |
| 38 | +InvalidLength: ... |
| 39 | +>>> validate('010/8068/17180') |
| 40 | +Traceback (most recent call last): |
| 41 | + ... |
| 42 | +InvalidChecksum: ... |
| 43 | +>>> is_valid('010/8068/17183') |
| 44 | +True |
| 45 | +>>> is_valid('010/8068/17180') |
| 46 | +False |
| 47 | +>>> format('010806817183') |
| 48 | +'010/8068/17183' |
| 49 | +""" |
| 50 | + |
| 51 | +from __future__ import annotations |
| 52 | + |
| 53 | +from stdnum.exceptions import ( |
| 54 | + InvalidChecksum, InvalidFormat, InvalidLength, ValidationError) |
| 55 | +from stdnum.util import clean, isdigits |
| 56 | + |
| 57 | + |
| 58 | +def compact(number: str) -> str: |
| 59 | + """Convert the number to the minimal representation. This strips the number |
| 60 | + of any invalid separators and removes surrounding whitespace.""" |
| 61 | + return clean(number, ' +/').strip() |
| 62 | + |
| 63 | + |
| 64 | +def checksum(number: str) -> int: |
| 65 | + """Calculate the checksum.""" |
| 66 | + return (int(number[:-2]) % 97) - int(number[-2:]) |
| 67 | + |
| 68 | + |
| 69 | +def validate(number: str) -> str: |
| 70 | + """Check if the number is a valid OGM-VCS.""" |
| 71 | + number = compact(number) |
| 72 | + if not isdigits(number) or int(number) <= 0: |
| 73 | + raise InvalidFormat() |
| 74 | + if len(number) != 12: |
| 75 | + raise InvalidLength() |
| 76 | + if checksum(number) != 0: |
| 77 | + raise InvalidChecksum() |
| 78 | + return number |
| 79 | + |
| 80 | + |
| 81 | +def is_valid(number: str) -> bool: |
| 82 | + """Check if the number is a valid VAT number.""" |
| 83 | + try: |
| 84 | + return bool(validate(number)) |
| 85 | + except ValidationError: |
| 86 | + return False |
| 87 | + |
| 88 | + |
| 89 | +def format(number: str) -> str: |
| 90 | + """Format the number provided for output.""" |
| 91 | + number = compact(number) |
| 92 | + number = number.rjust(12, '0') |
| 93 | + return f'{number[:3]}/{number[3:7]}/{number[7:]}' |
0 commit comments