|
| 1 | +# tin.py - functions for handling Ghana TIN numbers |
| 2 | +# coding: utf-8 |
| 3 | +# |
| 4 | +# Copyright (C) 2022 Leandro Regueiro |
| 5 | +# Copyright (C) 2022 Arthur de Jong |
| 6 | +# |
| 7 | +# This library is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 2.1 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This library is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public |
| 18 | +# License along with this library; if not, write to the Free Software |
| 19 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | +# 02110-1301 USA |
| 21 | + |
| 22 | +"""TIN (Taxpayer Identification Number, Ghana tax number). |
| 23 | +
|
| 24 | +This number is issued by the Ghana Revenue Authority (GRA) to individuals who |
| 25 | +are not eligible for the Ghanacard PIN and other entities. |
| 26 | +
|
| 27 | +This number consists of 11 alpha-numeric characters. It begins with one of the |
| 28 | +following prefixes: |
| 29 | +
|
| 30 | + P00 For Individuals. |
| 31 | + C00 For Companies limited by guarantee, shares, Unlimited (i.e organisation |
| 32 | + required to register with the RGD). |
| 33 | + G00 Government Agencies, MDAs. |
| 34 | + Q00 Foreign Missions, Employees of foreign missions. |
| 35 | + V00 Public Institutions, Trusts, Co-operatives, Foreign Shareholder |
| 36 | + (Offshore), (Entities not registered by RGD). |
| 37 | +
|
| 38 | +More information: |
| 39 | +
|
| 40 | +* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Ghana-TIN.pdf |
| 41 | +* https://gra.gov.gh/tin/ |
| 42 | +* https://gra.gov.gh/tin/tin-faq/ |
| 43 | +
|
| 44 | +>>> validate('C0000803561') |
| 45 | +'C0000803561' |
| 46 | +>>> validate('C0000803562') |
| 47 | +Traceback (most recent call last): |
| 48 | + ... |
| 49 | +InvalidChecksum: ... |
| 50 | +""" # noqa: E501 |
| 51 | + |
| 52 | +import re |
| 53 | + |
| 54 | +from stdnum.exceptions import * |
| 55 | +from stdnum.util import clean |
| 56 | + |
| 57 | + |
| 58 | +_gh_tin_re = re.compile(r'^[PCGQV]{1}00[A-Z0-9]{8}$') |
| 59 | + |
| 60 | + |
| 61 | +def compact(number): |
| 62 | + """Convert the number to the minimal representation. |
| 63 | +
|
| 64 | + This strips the number of any valid separators and removes surrounding |
| 65 | + whitespace. |
| 66 | + """ |
| 67 | + return clean(number, ' ').upper() |
| 68 | + |
| 69 | + |
| 70 | +def calc_check_digit(number): |
| 71 | + """Calculate the check digit for the TIN.""" |
| 72 | + check = sum((i + 1) * int(n) for i, n in enumerate(number[1:10])) % 11 |
| 73 | + return 'X' if check == 10 else str(check) |
| 74 | + |
| 75 | + |
| 76 | +def validate(number): |
| 77 | + """Check if the number is a valid Ghana TIN.""" |
| 78 | + number = compact(number) |
| 79 | + if len(number) != 11: |
| 80 | + raise InvalidLength() |
| 81 | + if not _gh_tin_re.match(number): |
| 82 | + raise InvalidFormat() |
| 83 | + if number[-1] != calc_check_digit(number): |
| 84 | + raise InvalidChecksum() |
| 85 | + return number |
| 86 | + |
| 87 | + |
| 88 | +def is_valid(number): |
| 89 | + """Check if the number is a valid Ghana TIN.""" |
| 90 | + try: |
| 91 | + return bool(validate(number)) |
| 92 | + except ValidationError: |
| 93 | + return False |
0 commit comments