|
| 1 | +# tin.py - functions for handling Uganda TIN 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 | +"""TIN (Tax identification number, Uganda tax number). |
| 22 | +
|
| 23 | +This number consists of 10 digits. |
| 24 | +
|
| 25 | +More information: |
| 26 | +
|
| 27 | +* https://thetaxman.ura.go.ug/understanding-a-tax-identification-number-tin/ |
| 28 | +* https://businessfocus.co.ug/how-to-get-taxpayer-identification-number-from-ura/ |
| 29 | +
|
| 30 | +>>> validate('1000957588') |
| 31 | +'1000957588' |
| 32 | +>>> validate('12345') |
| 33 | +Traceback (most recent call last): |
| 34 | + ... |
| 35 | +InvalidLength: ... |
| 36 | +>>> format('1000957588') |
| 37 | +'1000957588' |
| 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 Uganda TIN number. |
| 55 | +
|
| 56 | + This checks the length and formatting. |
| 57 | + """ |
| 58 | + number = compact(number) |
| 59 | + if len(number) != 10: |
| 60 | + raise InvalidLength() |
| 61 | + if not isdigits(number): |
| 62 | + raise InvalidFormat() |
| 63 | + return number |
| 64 | + |
| 65 | + |
| 66 | +def is_valid(number): |
| 67 | + """Check if the number is a valid Uganda TIN number.""" |
| 68 | + try: |
| 69 | + return bool(validate(number)) |
| 70 | + except ValidationError: |
| 71 | + return False |
| 72 | + |
| 73 | + |
| 74 | +def format(number): |
| 75 | + """Reformat the number to the standard presentation format.""" |
| 76 | + return compact(number) |
0 commit comments