lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 2.1-10-ga1fdd5d

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 2.1-10-ga1fdd5d



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  a1fdd5d5f39ec3d92da8fb1ab976d9e7175a571a (commit)
      from  e741318e61f5c59c97ef0a0b6e1dbb3c2fd7b389 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=a1fdd5d5f39ec3d92da8fb1ab976d9e7175a571a

commit a1fdd5d5f39ec3d92da8fb1ab976d9e7175a571a
Author: Leandro Regueiro <leandro.regueiro@gmail.com>
Date:   Sat Sep 17 16:09:53 2022 +0200

    Add support for Azerbaijan TIN
    
    Thanks to Adam Handke for finding the weights for the check digit
    algorithm.
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/200
    CLoses https://github.com/arthurdejong/python-stdnum/pull/329

diff --git a/stdnum/az/__init__.py b/stdnum/az/__init__.py
new file mode 100644
index 0000000..ef2c5c1
--- /dev/null
+++ b/stdnum/az/__init__.py
@@ -0,0 +1,26 @@
+# __init__.py - collection of Azerbaijan numbers
+# coding: utf-8
+#
+# Copyright (C) 2022 Leandro Regueiro
+#
+# 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
+
+"""Collection of Azerbaijan numbers."""
+
+from __future__ import annotations
+
+# provide aliases
+from stdnum.az import voen as vat  # noqa: F401
diff --git a/stdnum/az/voen.py b/stdnum/az/voen.py
new file mode 100644
index 0000000..61604dc
--- /dev/null
+++ b/stdnum/az/voen.py
@@ -0,0 +1,91 @@
+# voen.py - functions for handling Azerbaijan VOEN numbers
+# coding: utf-8
+#
+# Copyright (C) 2022 Leandro Regueiro
+#
+# 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
+
+"""VÖEN (Vergi ödəyicisinin eyniləşdirmə nömrəsi, Azerbaijan tax number).
+
+The Vergi ödəyicisinin eyniləşdirmə nömrəsi is issued by the Azerbaijan state
+tax authorities to individuals and legal entities.
+
+This number consists of 10 digits. The first two digits are the code for the
+territorial administrative unit. The following six digits are a serial
+number. The ninth digit is a check digit. The tenth digit represents the
+legal status of a taxpayer: 1 for legal persons and 2 for natural persons.
+
+More information:
+
+* 
https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Azerbaijan-TIN.pdf
+* https://www.e-taxes.gov.az/ebyn/payerOrVoenChecker.jsp
+
+>>> validate('140 155 5071')
+'1401555071'
+>>> validate('140 155 5081')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+>>> validate('1400057424')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+"""  # noqa: E501
+
+from __future__ import annotations
+
+from stdnum.exceptions import *
+from stdnum.util import clean, isdigits
+
+
+def compact(number: str) -> str:
+    """Convert the number to the minimal representation.
+
+    This strips the number of any valid separators and removes surrounding
+    whitespace.
+    """
+    number = clean(number, ' ')
+    if len(number) == 9:
+        number = '0' + number
+    return number
+
+
+def _calc_check_digit(number: str) -> str:
+    """Calculate the check digit for the VÖEN."""
+    weights = [4, 1, 8, 6, 2, 7, 5, 3]
+    return str(sum(w * int(n) for w, n in zip(weights, number)) % 11)
+
+
+def validate(number: str) -> str:
+    """Check if the number is a valid Azerbaijan VÖEN number."""
+    number = compact(number)
+    if len(number) != 10:
+        raise InvalidLength()
+    if not isdigits(number):
+        raise InvalidFormat()
+    if number[-1] not in ('1', '2'):
+        raise InvalidComponent()
+    if number[-2:-1] != _calc_check_digit(number):
+        raise InvalidChecksum()
+    return number
+
+
+def is_valid(number: str) -> bool:
+    """Check if the number is a valid Azerbaijan VÖEN number."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
diff --git a/tests/test_az_voen.doctest b/tests/test_az_voen.doctest
new file mode 100644
index 0000000..ef0a890
--- /dev/null
+++ b/tests/test_az_voen.doctest
@@ -0,0 +1,254 @@
+test_az_voen.doctest - more detailed doctests for stdnum.az.voen module
+
+Copyright (C) 2022 Leandro Regueiro
+
+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.az.voen module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.az import voen
+
+
+Tests for some corner cases.
+
+>>> voen.validate('1400057421')
+'1400057421'
+>>> voen.validate('140 155 5071')
+'1401555071'
+>>> voen.validate('12345')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> voen.validate('ZZ00057421')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> voen.validate('1400057424')
+Traceback (most recent call last):
+    ...
+InvalidComponent: ...
+
+
+Nine digit numbers get turned into 10 digits because a leading 0 is sometimes
+left out.
+
+>>> voen.compact('300725012')
+'0300725012'
+>>> voen.validate('300725012')
+'0300725012'
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 0200432771
+... 0400198812
+... 0600192862
+... 0800000512
+... 0900062472
+... 1000276532
+... 1002031802
+... 1002193082
+... 1002632171
+... 1002900962
+... 1003160261
+... 1003337762
+... 1005196511
+... 1005643421
+... 1005837482
+... 1005840682
+... 1101062081
+... 1200941152
+... 1202174962
+... 1300182372
+... 1301478392
+... 1302363462
+... 1303029632
+... 1303392331
+... 1303776231
+... 1303959432
+... 1304429532
+... 1305161112
+... 1305777442
+... 1306144182
+... 1306379412
+... 1400138202
+... 1401146511
+... 1401598061
+... 1403147441
+... 1403149281
+... 1403424841
+... 1403575391
+... 1404034501
+... 1404363351
+... 1404678141
+... 1501238982
+... 1501398032
+... 1502964842
+... 1503660862
+... 1503706462
+... 1503883601
+... 1503929742
+... 1504014182
+... 1504695681
+... 1601293792
+... 1602158822
+... 1602379421
+... 1603117061
+... 1603390091
+... 1700548702
+... 1701338762
+... 1701423382
+... 1701825192
+... 1702640641
+... 1801735962
+... 1803381652
+... 1803742052
+... 1803964261
+... 1804578291
+... 1900156291
+... 1900666092
+... 1901679992
+... 1902783672
+... 1903742192
+... 2001344972
+... 2001938462
+... 2002612202
+... 2003266571
+... 2003583512
+... 2004402192
+... 2005090691
+... 2005913681
+... 200722502
+... 2100223801
+... 2100710642
+... 2201511592
+... 2300195562
+... 2300216922
+... 2300295452
+... 2304643692
+... 2600559872
+... 2600774242
+... 2601337332
+... 2602941392
+... 2700093202
+... 2700300252
+... 2700947102
+... 2800038132
+... 2900398871
+... 2903227542
+... 2903472571
+... 3000740252
+... 300293922
+... 3100775152
+... 3100922132
+... 3101355922
+... 3101945282
+... 3102821612
+... 3200741062
+... 3200777152
+... 3300511732
+... 3400435452
+... 3500865702
+... 3600093232
+... 3700292512
+... 3700362132
+... 3800115252
+... 3800140902
+... 3900596722
+... 4001130832
+... 400198382
+... 4100217642
+... 4101419902
+... 4200287721
+... 4200811431
+... 4300343432
+... 4300573181
+... 4401208732
+... 4500078262
+... 4501381622
+... 4600238532
+... 4700899992
+... 4800213191
+... 4900279562
+... 5000136892
+... 500027792
+... 5100109431
+... 5100186382
+... 5200103342
+... 5300164942
+... 5400520582
+... 5400932482
+... 5500638121
+... 5600095582
+... 5700281172
+... 5700680892
+... 5800143892
+... 5900080492
+... 6000440592
+... 6000474432
+... 600495162
+... 6100192552
+... 6100585341
+... 6101051612
+... 6200094532
+... 6300004442
+... 6300030322
+... 6400005112
+... 6402002901
+... 6500021732
+... 6500262492
+... 6600304962
+... 6701087912
+... 6800062912
+... 6800100962
+... 6900587432
+... 7000419952
+... 700516122
+... 7100015622
+... 7200649742
+... 7300403792
+... 7400146452
+... 7400216762
+... 7500537672
+... 7600071671
+... 7600205102
+... 7700749702
+... 7701086342
+... 7701233582
+... 7800009861
+... 7900062142
+... 8000051472
+... 800044702
+... 800047872
+... 8100182712
+... 8200088092
+... 8300356482
+... 8300737062
+... 8400248092
+... 8500094382
+... 8600050502
+... 8700029382
+... 900215552
+... 9900051231
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not voen.is_valid(x)]
+[]

-----------------------------------------------------------------------

Summary of changes:
 stdnum/{tn => az}/__init__.py    |   6 +-
 stdnum/{za/tin.py => az/voen.py} |  62 +++++-----
 tests/test_az_voen.doctest       | 254 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 290 insertions(+), 32 deletions(-)
 copy stdnum/{tn => az}/__init__.py (86%)
 copy stdnum/{za/tin.py => az/voen.py} (52%)
 create mode 100644 tests/test_az_voen.doctest


hooks/post-receive
-- 
python-stdnum