|
| 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 | +This number consists of 10 digits. |
| 24 | +
|
| 25 | +The first two digits are the code for the territorial administrative unit. The |
| 26 | +following six digits are a serial number. The ninth digit is determined by some |
| 27 | +special algorithm. The tenth digit represents the legal status of a taxpayer: |
| 28 | +1 for legal persons and 2 for natural persons. |
| 29 | +
|
| 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('1400057421') |
| 37 | +'1400057421' |
| 38 | +>>> validate('140 155 5071') |
| 39 | +'1401555071' |
| 40 | +>>> validate('12345') |
| 41 | +Traceback (most recent call last): |
| 42 | + ... |
| 43 | +InvalidLength: ... |
| 44 | +>>> validate('1400057424') |
| 45 | +Traceback (most recent call last): |
| 46 | + ... |
| 47 | +InvalidComponent: ... |
| 48 | +>>> format('140 155 5071') |
| 49 | +'1401555071' |
| 50 | +""" # noqa: E501 |
| 51 | + |
| 52 | +from stdnum.exceptions import * |
| 53 | +from stdnum.util import clean, isdigits |
| 54 | + |
| 55 | + |
| 56 | +def compact(number): |
| 57 | + """Convert the number to the minimal representation. |
| 58 | +
|
| 59 | + This strips the number of any valid separators and removes surrounding |
| 60 | + whitespace. |
| 61 | + """ |
| 62 | + return clean(number, ' ') |
| 63 | + |
| 64 | + |
| 65 | +def validate(number): |
| 66 | + """Check if the number is a valid Azerbaijan VÖEN number. |
| 67 | +
|
| 68 | + This checks the length and formatting. |
| 69 | + """ |
| 70 | + number = compact(number) |
| 71 | + if len(number) != 10: |
| 72 | + raise InvalidLength() |
| 73 | + if not isdigits(number): |
| 74 | + raise InvalidFormat() |
| 75 | + if number[-1] not in ('1', '2'): |
| 76 | + raise InvalidComponent() |
| 77 | + return number |
| 78 | + |
| 79 | + |
| 80 | +def is_valid(number): |
| 81 | + """Check if the number is a valid Azerbaijan VÖEN number.""" |
| 82 | + try: |
| 83 | + return bool(validate(number)) |
| 84 | + except ValidationError: |
| 85 | + return False |
| 86 | + |
| 87 | + |
| 88 | +def format(number): |
| 89 | + """Reformat the number to the standard presentation format.""" |
| 90 | + return compact(number) |
0 commit comments