|
| 1 | +# voen.py - functions for handling Azerbaijan VOEN numbers |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2022 Leandro Regueiro |
| 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 | +"""VÖEN (Vergi ödəyicisinin eyniləşdirmə nömrəsi, Azerbaijan tax number). |
| 22 | +
|
| 23 | +The Vergi ödəyicisinin eyniləşdirmə nömrəsi is issued by the Azerbaijan state |
| 24 | +tax authorities to individuals and legal entities. |
| 25 | +
|
| 26 | +This number consists of 10 digits. The first two digits are the code for the |
| 27 | +territorial administrative unit. The following six digits are a serial |
| 28 | +number. The ninth digit is a check digit. The tenth digit represents the |
| 29 | +legal status of a taxpayer: 1 for legal persons and 2 for natural persons. |
| 30 | +
|
| 31 | +More information: |
| 32 | +
|
| 33 | +* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Azerbaijan-TIN.pdf |
| 34 | +* https://www.e-taxes.gov.az/ebyn/payerOrVoenChecker.jsp |
| 35 | +
|
| 36 | +>>> validate('140 155 5071') |
| 37 | +'1401555071' |
| 38 | +>>> validate('140 155 5081') |
| 39 | +Traceback (most recent call last): |
| 40 | + ... |
| 41 | +InvalidChecksum: ... |
| 42 | +>>> validate('1400057424') |
| 43 | +Traceback (most recent call last): |
| 44 | + ... |
| 45 | +InvalidComponent: ... |
| 46 | +""" # noqa: E501 |
| 47 | + |
| 48 | +from __future__ import annotations |
| 49 | + |
| 50 | +from stdnum.exceptions import * |
| 51 | +from stdnum.util import clean, isdigits |
| 52 | + |
| 53 | + |
| 54 | +def compact(number: str) -> str: |
| 55 | + """Convert the number to the minimal representation. |
| 56 | +
|
| 57 | + This strips the number of any valid separators and removes surrounding |
| 58 | + whitespace. |
| 59 | + """ |
| 60 | + number = clean(number, ' ') |
| 61 | + if len(number) == 9: |
| 62 | + number = '0' + number |
| 63 | + return number |
| 64 | + |
| 65 | + |
| 66 | +def _calc_check_digit(number: str) -> str: |
| 67 | + """Calculate the check digit for the VÖEN.""" |
| 68 | + weights = [4, 1, 8, 6, 2, 7, 5, 3] |
| 69 | + return str(sum(w * int(n) for w, n in zip(weights, number)) % 11) |
| 70 | + |
| 71 | + |
| 72 | +def validate(number: str) -> str: |
| 73 | + """Check if the number is a valid Azerbaijan VÖEN number.""" |
| 74 | + number = compact(number) |
| 75 | + if len(number) != 10: |
| 76 | + raise InvalidLength() |
| 77 | + if not isdigits(number): |
| 78 | + raise InvalidFormat() |
| 79 | + if number[-1] not in ('1', '2'): |
| 80 | + raise InvalidComponent() |
| 81 | + if number[-2:-1] != _calc_check_digit(number): |
| 82 | + raise InvalidChecksum() |
| 83 | + return number |
| 84 | + |
| 85 | + |
| 86 | +def is_valid(number: str) -> bool: |
| 87 | + """Check if the number is a valid Azerbaijan VÖEN number.""" |
| 88 | + try: |
| 89 | + return bool(validate(number)) |
| 90 | + except ValidationError: |
| 91 | + return False |
0 commit comments