Skip to content

Adding support for Slovenian Corporate Registration number (matična številka) #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ Available formats
sg.uen
si.ddv
si.emso
si.maticna
sk.dph
sk.rc
sm.coe
Expand Down
5 changes: 5 additions & 0 deletions docs/stdnum.si.maticna.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
stdnum.si.maticna
=================

.. automodule:: stdnum.si.maticna
:members:
1 change: 1 addition & 0 deletions stdnum/si/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
# provide vat as an alias
from stdnum.si import ddv as vat # noqa: F401
from stdnum.si import emso as personalid # noqa: F401
from stdnum.si import maticna as businessid # noqa: F401
101 changes: 101 additions & 0 deletions stdnum/si/maticna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# maticna.py - functions for handling Slovenian Corporate Registration Numbers
# coding: utf-8
#
# Copyright (C) 2023 Blaž Bregar
#
# 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

"""Matična številka poslovnega registra (Corporate Registration Number)

The Corporate registration number represent a unique identification of
each unit of the business register, assigned by the registry administrator
at the time of entry in the business register, which shall not be changed.

The number consists of 10 digits and includes. First 6 digits represent a
unique number for each unit of company, followed by a check digit.
Last 3 digits represent an additional business unit of the company, starting
at 001. When a company consists of more than 1000 units, a letter is used
instead of the first digit in the business unit. Unit 000 always represents
the main registered address.

More information:

* http://www.pisrs.si/Pis.web/pregledPredpisa?id=URED7599

>>> validate('9331310000')
'9331310000'
>>> validate('9331320000')
Traceback (most recent call last):
...
InvalidChecksum: ...
"""
import re

from stdnum.exceptions import *
from stdnum.util import clean, isdigits


def compact(number):
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
number = clean(number, ' ').strip()
if len(number) == 10 and number.endswith('000'):
return number[0:7]
return number


def calc_check_digit(number):
"""Calculate the check digit."""
weights = (7, 6, 5, 4, 3, 2)
total = sum(int(n) * w for n, w in zip(number[:6], weights))
remainder = total % 11
if remainder == 1:
return str(0)
elif remainder == 0:
return False
check = 11 - remainder
return str(check)


def validate(number):
"""Check if the number is a valid Corporate Registration number. This
checks the length and check digit."""
number = clean(number)
if not (len(number) == 7 or len(number) == 10):
raise InvalidLength()
if not isdigits(number[:6]):
raise InvalidFormat()
if calc_check_digit(number) != number[6]:
raise InvalidChecksum()
if len(number) == 10:
if not bool(re.match(r'^[A-Za-z0-9]\d{2}$', number[7:10])):
raise InvalidFormat()

return number.upper()


def is_valid(number):
"""Check if provided is valid ID. This checks the length,
formatting and check digit."""
try:
return bool(validate(number))
except ValidationError:
return False


def format(number):
"""Reformat the number to the standard presentation format."""
return compact(number)
122 changes: 122 additions & 0 deletions tests/test_si_maticna.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
test_si_maticna.doctest - more detailed doctests for the stdnum.si.maticna module

Copyright (C) 2023 Blaž Bregar

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


This file contains more detailed doctests for the stdnum.si.emso. It
tries to validate a number of numbers that have been found online.

>>> from stdnum.si import maticna
>>> from stdnum.exceptions import *


Tests for some corner cases.

>>> maticna.validate('9331310000')
'9331310000'
>>> maticna.format('9331310000')
'9331310'
>>> maticna.format('9331310255')
'9331310255'
>>> maticna.format('9331310 000')
'9331310'
>>> maticna.validate('9331310')
'9331310'
>>> maticna.validate('9331310255')
'9331310255'
>>> maticna.validate('8982279A00')
'8982279A00'
>>> maticna.validate('8982279J00')
'8982279J00'
>>> maticna.validate('8982279z00')
'8982279Z00'
>>> maticna.validate('8982279f00')
'8982279F00'
>>> maticna.validate('12345')
Traceback (most recent call last):
...
InvalidLength: ...
>>> maticna.validate('9331320000')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> maticna.validate('933A310100')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> maticna.validate('93313100A0')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> maticna.validate('9331310AA0')
Traceback (most recent call last):
...
InvalidFormat: ...
>>> maticna.validate('5491710')
Traceback (most recent call last):
...
InvalidChecksum: ...

# Edge cases (excluded numbers)
>>> maticna.validate('9015310')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> maticna.validate('2961970')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> maticna.validate('5015170')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> maticna.validate('3919110')
Traceback (most recent call last):
...
InvalidChecksum: ...

These have been found online and should all be valid numbers.

>>> numbers = '''
...
... 7282664000
... 9331310
... 3501264000
... 8071993000
... 5491711
... 8982279000
... 5300231000
... 5147174000
... 5860571000
... 5263565019
... 1876031010
... 5860571255
... 5860571084
... 5300231136
... 5300231150
... 5860580000
... 5860580150
... 5464943000
... 5464943608
... 5464943003
... 8339414000
... 3490360000
...
... '''
>>> [x for x in numbers.splitlines() if x and not maticna.is_valid(x)]
[]