Skip to content

Commit d534b3c

Browse files
cedkarthurdejong
authored andcommitted
Add Belgian OGM-VCS
Closes #487
1 parent 2cf475c commit d534b3c

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

stdnum/be/ogm_vcs.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
>>> validate('+++010/8068/17183+++')
28+
'010806817183'
29+
>>> validate('010/8068/17180')
30+
Traceback (most recent call last):
31+
...
32+
InvalidChecksum: ...
33+
>>> format('010806817183')
34+
'010/8068/17183'
35+
>>> calc_check_digits('0108068171')
36+
'83'
37+
"""
38+
39+
from __future__ import annotations
40+
41+
from stdnum.exceptions import *
42+
from stdnum.util import clean, isdigits
43+
44+
45+
def compact(number: str) -> str:
46+
"""Convert the number to the minimal representation. This strips the number
47+
of any invalid separators and removes surrounding whitespace."""
48+
return clean(number, ' +/').strip()
49+
50+
51+
def calc_check_digits(number: str) -> str:
52+
"""Calculate the check digit that should be added."""
53+
number = compact(number)
54+
return '%02d' % ((int(number[:10]) % 97) or 97)
55+
56+
57+
def validate(number: str) -> str:
58+
"""Check if the number is a valid OGM-VCS."""
59+
number = compact(number)
60+
if not isdigits(number) or int(number) <= 0:
61+
raise InvalidFormat()
62+
if len(number) != 12:
63+
raise InvalidLength()
64+
if calc_check_digits(number) != number[-2:]:
65+
raise InvalidChecksum()
66+
return number
67+
68+
69+
def is_valid(number: str) -> bool:
70+
"""Check if the number is a valid VAT number."""
71+
try:
72+
return bool(validate(number))
73+
except ValidationError:
74+
return False
75+
76+
77+
def format(number: str) -> str:
78+
"""Format the number provided for output."""
79+
number = compact(number)
80+
number = number.rjust(12, '0')
81+
return f'{number[:3]}/{number[3:7]}/{number[7:]}'

0 commit comments

Comments
 (0)