|
| 1 | +# tan.py - functions for handling Mauritius TAN numbers |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2023 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 | +"""TAN (Tax Account Number, Mauritius tax number). |
| 22 | +
|
| 23 | +This number consists of 8 digits. For individuals the first digit is always 1, |
| 24 | +5, 7 or 8. For entities the first digit is always 2 or 3. |
| 25 | +
|
| 26 | +More information: |
| 27 | +
|
| 28 | +* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Mauritius-TIN.pdf |
| 29 | +
|
| 30 | +>>> validate('20405553') |
| 31 | +'20405553' |
| 32 | +>>> validate('12345') |
| 33 | +Traceback (most recent call last): |
| 34 | + ... |
| 35 | +InvalidLength: ... |
| 36 | +>>> format('20405553') |
| 37 | +'20405553' |
| 38 | +""" # noqa: E501 |
| 39 | + |
| 40 | +from stdnum.exceptions import * |
| 41 | +from stdnum.util import clean, isdigits |
| 42 | + |
| 43 | + |
| 44 | +def compact(number): |
| 45 | + """Convert the number to the minimal representation. |
| 46 | +
|
| 47 | + This strips the number of any valid separators and removes surrounding |
| 48 | + whitespace. |
| 49 | + """ |
| 50 | + return clean(number, ' -').strip() |
| 51 | + |
| 52 | + |
| 53 | +def validate(number): |
| 54 | + """Check if the number is a valid Mauritius TAN number. |
| 55 | +
|
| 56 | + This checks the length and formatting. |
| 57 | + """ |
| 58 | + number = compact(number) |
| 59 | + if len(number) != 8: |
| 60 | + raise InvalidLength() |
| 61 | + if not isdigits(number): |
| 62 | + raise InvalidFormat() |
| 63 | + if number[0] not in ('1', '2', '3', '5', '7', '8'): |
| 64 | + raise InvalidComponent() |
| 65 | + return number |
| 66 | + |
| 67 | + |
| 68 | +def is_valid(number): |
| 69 | + """Check if the number is a valid Mauritius TAN number.""" |
| 70 | + try: |
| 71 | + return bool(validate(number)) |
| 72 | + except ValidationError: |
| 73 | + return False |
| 74 | + |
| 75 | + |
| 76 | +def format(number): |
| 77 | + """Reformat the number to the standard presentation format.""" |
| 78 | + return compact(number) |
0 commit comments