1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
# handelsregisternummer.py - functions for handling German company registry id
# coding: utf-8
#
# Copyright (C) 2015 Holvi Payment Services Oy
# Copyright (C) 2018-2024 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
"""Handelsregisternummer (German company register number).
The number consists of the court where the company has registered, the type
of register and the registration number.
The type of the register is either HRA or HRB where the letter "B" stands for
HR section B, where limited liability companies and corporations are entered
(GmbH's and AG's). There is also a section HRA for business partnerships
(OHG's, KG's etc.). In other words: businesses in section HRB are limited
liability companies, while businesses in HRA have personally liable partners.
More information:
* https://www.handelsregister.de/
* https://en.wikipedia.org/wiki/German_Trade_Register
* https://offeneregister.de/
>>> validate('Aachen HRA 11223')
'Aachen HRA 11223'
>>> validate('Frankfurt/Oder GnR 11223', company_form='e.G.')
'Frankfurt/Oder GnR 11223'
>>> validate('Aachen HRC 44123')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> validate('Aachen HRA 44123', company_form='GmbH')
Traceback (most recent call last):
...
InvalidComponent: ...
"""
import re
import unicodedata
from stdnum.exceptions import *
from stdnum.util import clean, to_unicode
# The known courts that have a Handelsregister
GERMAN_COURTS = (
'Aachen',
'Altenburg',
'Amberg',
'Ansbach',
'Apolda',
'Arnsberg',
'Arnstadt Zweigstelle Ilmenau',
'Arnstadt',
'Aschaffenburg',
'Augsburg',
'Aurich',
'Bad Hersfeld',
'Bad Homburg v.d.H.',
'Bad Kreuznach',
'Bad Oeynhausen',
'Bad Salzungen',
'Bamberg',
'Bayreuth',
'Berlin (Charlottenburg)',
'Bielefeld',
'Bochum',
'Bonn',
'Braunschweig',
'Bremen',
'Chemnitz',
'Coburg',
'Coesfeld',
'Cottbus',
'Darmstadt',
'Deggendorf',
'Dortmund',
'Dresden',
'Duisburg',
'Düren',
'Düsseldorf',
'Eisenach',
'Erfurt',
'Eschwege',
'Essen',
'Flensburg',
'Frankfurt am Main',
'Frankfurt/Oder',
'Freiburg',
'Friedberg',
'Fritzlar',
'Fulda',
'Fürth',
'Gelsenkirchen',
'Gera',
'Gießen',
'Gotha',
'Greiz',
'Göttingen',
'Gütersloh',
'Hagen',
'Hamburg',
'Hamm',
'Hanau',
'Hannover',
'Heilbad Heiligenstadt',
'Hildburghausen',
'Hildesheim',
'Hof',
'Homburg',
'Ingolstadt',
'Iserlohn',
'Jena',
'Kaiserslautern',
'Kassel',
'Kempten (Allgäu)',
'Kiel',
'Kleve',
'Koblenz',
'Korbach',
'Krefeld',
'Köln',
'Königstein',
'Landau',
'Landshut',
'Langenfeld',
'Lebach',
'Leipzig',
'Lemgo',
'Limburg',
'Ludwigshafen a.Rhein (Ludwigshafen)',
'Lübeck',
'Lüneburg',
'Mainz',
'Mannheim',
'Marburg',
'Meiningen',
'Memmingen',
'Merzig',
'Montabaur',
'Mönchengladbach',
'Mühlhausen',
'München',
'Münster',
'Neubrandenburg',
'Neunkirchen',
'Neuruppin',
'Neuss',
'Nordhausen',
'Nürnberg',
'Offenbach am Main',
'Oldenburg (Oldenburg)',
'Osnabrück',
'Ottweiler',
'Paderborn',
'Passau',
'Pinneberg',
'Potsdam',
'Pößneck Zweigstelle Bad Lobenstein',
'Pößneck',
'Recklinghausen',
'Regensburg',
'Rostock',
'Rudolstadt Zweigstelle Saalfeld',
'Rudolstadt',
'Saarbrücken',
'Saarlouis',
'Schweinfurt',
'Schwerin',
'Siegburg',
'Siegen',
'Sondershausen',
'Sonneberg',
'St. Ingbert (St Ingbert)',
'St. Wendel (St Wendel)',
'Stadthagen',
'Stadtroda',
'Steinfurt',
'Stendal',
'Stralsund',
'Straubing',
'Stuttgart',
'Suhl',
'Sömmerda',
'Tostedt',
'Traunstein',
'Ulm',
'Völklingen',
'Walsrode',
'Weiden i. d. OPf.',
'Weimar',
'Wetzlar',
'Wiesbaden',
'Wittlich',
'Wuppertal',
'Würzburg',
'Zweibrücken',
)
def _to_min(court):
"""Convert the court name for quick comparison without encoding issues."""
return ''.join(
x for x in unicodedata.normalize('NFD', to_unicode(court).lower())
if x in 'abcdefghijklmnopqrstuvwxyz')
# Build a dictionary for lookup up courts
_courts = dict(
(_to_min(court), court) for court in GERMAN_COURTS)
_courts.update(
(_to_min(alias), court) for alias, court in (
('Allgäu', 'Kempten (Allgäu)'),
('Bad Homburg', 'Bad Homburg v.d.H.'),
('Berlin', 'Berlin (Charlottenburg)'),
('Charlottenburg', 'Berlin (Charlottenburg)'),
('Charlottenburg (Berlin)', 'Berlin (Charlottenburg)'),
('Kaln', 'Köln'), # for encoding issues
('Kempten', 'Kempten (Allgäu)'),
('Ludwigshafen am Rhein (Ludwigshafen)', 'Ludwigshafen a.Rhein (Ludwigshafen)'),
('Ludwigshafen am Rhein', 'Ludwigshafen a.Rhein (Ludwigshafen)'),
('Ludwigshafen', 'Ludwigshafen a.Rhein (Ludwigshafen)'),
('Oldenburg', 'Oldenburg (Oldenburg)'),
('St. Ingbert', 'St. Ingbert (St Ingbert)'),
('St. Wendel', 'St. Wendel (St Wendel)'),
('Weiden in der Oberpfalz', 'Weiden i. d. OPf.'),
('Weiden', 'Weiden i. d. OPf.'),
('Paderborn früher Höxter', 'Paderborn'),
))
# The known registry types
REGISTRY_TYPES = (
'HRA',
'HRB',
'PR',
'GnR',
'VR',
)
COMPANY_FORM_REGISTRY_TYPES = {
'e.K.': 'HRA',
'e.V.': 'VR',
'Verein': 'VR',
'OHG': 'HRA',
'KG': 'HRA',
'KGaA': 'HRB',
'Vor-GmbH': 'HRB',
'GmbH': 'HRB',
'UG': 'HRB',
'UG i.G.': 'HRB',
'AG': 'HRB',
'e.G.': 'GnR',
'PartG': 'PR',
}
# possible formats the number can be specified in
_court_re = r'(?P<court>.*)'
_registry_re = r'(?P<registry>%s)' % '|'.join(REGISTRY_TYPES)
_number_re = r'(?P<nr>[1-9][0-9]{0,5})(\s*(?P<x>[A-ZÖ]{1,3}))?'
_formats = [
_registry_re + r'\s+' + _number_re + r',?\s+' + _court_re + '$',
_court_re + r',?\s+' + _registry_re + r'\s+' + _number_re + '$',
]
def _split(number):
"""Split the number into a court, registry, register number and
optionally qualifier."""
number = clean(number).strip()
for fmt in _formats:
m = re.match(fmt, number, flags=re.I | re.U)
if m:
return m.group('court').strip(), m.group('registry'), m.group('nr'), m.group('x')
raise InvalidFormat()
def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
court, registry, number, qualifier = _split(number)
return ' '.join(x for x in [court, registry, number, qualifier] if x)
def validate(number, company_form=None):
"""Check if the number is a valid company registry number. If a
company_form (eg. GmbH or PartG) is given, the number is validated to
have the correct registry type."""
court, registry, number, qualifier = _split(number)
court = _courts.get(_to_min(court))
if not court:
raise InvalidComponent()
if not isinstance(court, type(number)): # pragma: no cover (Python 2 code)
court = court.decode('utf-8')
if company_form and COMPANY_FORM_REGISTRY_TYPES.get(company_form) != registry:
raise InvalidComponent()
return ' '.join(x for x in [court, registry, number, qualifier] if x)
def is_valid(number):
"""Check if the number is a valid company registry number."""
try:
return bool(validate(number))
except ValidationError:
return False
# The base URL for performing lookups
_offeneregister_url = 'https://db.offeneregister.de/openregister.json'
def check_offeneregister(number, timeout=30, verify=True): # pragma: no cover (not part of normal test suite)
"""Retrieve registration information from the OffeneRegister.de web site.
The `timeout` argument specifies the network timeout in seconds.
The `verify` argument is either a boolean that determines whether the
server's certificate is validate or a string which must be a path the CA
certificate bundle to use for verification.
This basically returns the JSON response from the web service as a dict.
It will contain something like the following::
{
'companyId': 'U1206_HRB14011',
'courtCode': 'U1206',
'courtName': 'Chemnitz',
'globalId': 'sn_293298',
'isCurrent': 'True',
'name': 'Internet hier GmbH Presentation Provider',
'nativeReferenceNumber': 'Chemnitz HRB 14011',
'referenceNumberFirstSeen': '2020-06-12',
'stdRefNo': 'U1206_HRB14011',
'validFrom': '2020-06-12',
'validTill': '',
}
Will return None if the number is invalid or unknown.
"""
# this function isn't automatically tested because it would require
# network access for the tests and unnecessarily load the web service
import requests
court, registry, number, qualifier = _split(number)
response = requests.get(
_offeneregister_url,
params={
'sql': '''
select *
from ReferenceNumbers
join Names on ReferenceNumbers.companyId = Names.companyId
where nativeReferenceNumber = :p0
limit 1
''',
'p0': '%s %s %s' % (court, registry, number)},
timeout=timeout,
verify=verify)
response.raise_for_status()
try:
json = response.json()
return dict(zip(json['columns'], json['rows'][0]))
except (KeyError, IndexError) as e: # noqa: F841
return # number not found
|