Skip to content

Commit a8d237a

Browse files
committed
Add Belgian OGM-VCS
1 parent a1fdd5d commit a8d237a

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Available formats
130130
be.eid
131131
be.iban
132132
be.nn
133+
be.ogm_vcs
133134
be.ssn
134135
be.vat
135136
bg.egn

docs/stdnum.be.ogm_vcs.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
stdnum.be.ogm_vcs
2+
=================
3+
4+
.. automodule:: stdnum.be.ogm_vcs
5+
:members:

stdnum/be/ogm_vcs.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
>>> checksum('0108068171')
50+
83
51+
>>> calc_check_digit('0108068171')
52+
'83'
53+
"""
54+
55+
from __future__ import annotations
56+
57+
from stdnum.exceptions import (
58+
InvalidChecksum, InvalidFormat, InvalidLength, ValidationError)
59+
from stdnum.util import clean, isdigits
60+
61+
62+
def compact(number: str) -> str:
63+
"""Convert the number to the minimal representation. This strips the number
64+
of any invalid separators and removes surrounding whitespace."""
65+
return clean(number, ' +/').strip()
66+
67+
68+
def checksum(number: str) -> int:
69+
"""Calculate the checksum."""
70+
return (int(number) % 97) or 97
71+
72+
73+
def calc_check_digit(number: str) -> str:
74+
"""Calculate the check digit that should be added."""
75+
return '%02d' % checksum(number)
76+
77+
78+
def validate(number: str) -> str:
79+
"""Check if the number is a valid OGM-VCS."""
80+
number = compact(number)
81+
if not isdigits(number) or int(number) <= 0:
82+
raise InvalidFormat()
83+
if len(number) != 12:
84+
raise InvalidLength()
85+
if checksum(number[:-2]) != int(number[-2:]):
86+
raise InvalidChecksum()
87+
return number
88+
89+
90+
def is_valid(number: str) -> bool:
91+
"""Check if the number is a valid VAT number."""
92+
try:
93+
return bool(validate(number))
94+
except ValidationError:
95+
return False
96+
97+
98+
def format(number: str) -> str:
99+
"""Format the number provided for output."""
100+
number = compact(number)
101+
number = number.rjust(12, '0')
102+
return f'{number[:3]}/{number[3:7]}/{number[7:]}'

0 commit comments

Comments
 (0)