|
| 1 | +# ntn.py - functions for handling Pakistan NTN 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 | +"""NTN (National Tax Number, نیشنل ٹیکس نمبر, Pakistan tax number). |
| 22 | +
|
| 23 | +This number consists of 8 digits, the last being a check digit, usually |
| 24 | +separated by a hyphen like XXXXXXX-X. |
| 25 | +
|
| 26 | +Companies and associations of persons (AOP) are assigned a National Tax Number |
| 27 | +or Registration Number when they e-enroll on the FBR Iris portal. |
| 28 | +
|
| 29 | +More information: |
| 30 | +
|
| 31 | +* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Pakistan-TIN.pdf |
| 32 | +* https://e.fbr.gov.pk/ |
| 33 | +
|
| 34 | +>>> validate('3804142-1') |
| 35 | +'38041421' |
| 36 | +>>> validate('0822910 - 4') |
| 37 | +'08229104' |
| 38 | +>>> validate('12345') |
| 39 | +Traceback (most recent call last): |
| 40 | + ... |
| 41 | +InvalidLength: ... |
| 42 | +>>> format('38041421') |
| 43 | +'3804142-1' |
| 44 | +""" # noqa: E501 |
| 45 | + |
| 46 | +from stdnum.exceptions import * |
| 47 | +from stdnum.util import clean, isdigits |
| 48 | + |
| 49 | + |
| 50 | +def compact(number): |
| 51 | + """Convert the number to the minimal representation. |
| 52 | +
|
| 53 | + This strips the number of any valid separators and removes surrounding |
| 54 | + whitespace. |
| 55 | + """ |
| 56 | + return clean(number, ' -') |
| 57 | + |
| 58 | + |
| 59 | +def validate(number): |
| 60 | + """Check if the number is a valid Pakistan NTN number. |
| 61 | +
|
| 62 | + This checks the length, formatting and check digit. |
| 63 | + """ |
| 64 | + number = compact(number) |
| 65 | + if len(number) != 8: |
| 66 | + raise InvalidLength() |
| 67 | + if not isdigits(number): |
| 68 | + raise InvalidFormat() |
| 69 | + return number |
| 70 | + |
| 71 | + |
| 72 | +def is_valid(number): |
| 73 | + """Check if the number is a valid Pakistan NTN number.""" |
| 74 | + try: |
| 75 | + return bool(validate(number)) |
| 76 | + except ValidationError: |
| 77 | + return False |
| 78 | + |
| 79 | + |
| 80 | +def format(number): |
| 81 | + """Reformat the number to the standard presentation format.""" |
| 82 | + number = compact(number) |
| 83 | + return '-'.join([number[:-1], number[-1]]) |
0 commit comments