|
| 1 | +# itn.py - functions for handling ITN numbers |
| 2 | +# |
| 3 | +# Copyright (C) 2020 Sergi Almacellas Abellana |
| 4 | +# |
| 5 | +# This library is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License as published by the Free Software Foundation; either |
| 8 | +# version 2.1 of the License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# This library is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +# Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public |
| 16 | +# License along with this library; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | +# 02110-1301 USA |
| 19 | + |
| 20 | +"""ITN No. (Malaysian Income Tax Number) |
| 21 | +
|
| 22 | +The number is assigned by The Inland Revenue Board of Malaysia (IRBM) and it |
| 23 | +is required to report the income. This unique number is known as |
| 24 | +"Nombor CukaiPendapatan" or Income Tax Number. |
| 25 | +
|
| 26 | +The number consist of 11 or 12 digits. It is structured by two types, normally |
| 27 | +separated by an space. The first one consists of 1 or 2 leters and represents |
| 28 | +the type of the file number. The second one is always ten digits an represents |
| 29 | +the tax number. |
| 30 | +
|
| 31 | +>>> validate('C2584563202') |
| 32 | +'C2584563202' |
| 33 | +>>> validate('CDB2584563202') # Should contain the prefix |
| 34 | +Traceback (most recent call last): |
| 35 | + ... |
| 36 | +InvalidLength: ... |
| 37 | +>>> validate('CD12346789012') # Should contain the prefix |
| 38 | +Traceback (most recent call last): |
| 39 | + ... |
| 40 | +InvalidLength: ... |
| 41 | +>>> validate('C258456320B') # number should only contain digits |
| 42 | +Traceback (most recent call last): |
| 43 | + ... |
| 44 | +InvalidFormat: ... |
| 45 | +>>> format('C2584563202') |
| 46 | +'C 2584563202' |
| 47 | +""" |
| 48 | + |
| 49 | +from stdnum.exceptions import * |
| 50 | +from stdnum.util import clean, isdigits |
| 51 | + |
| 52 | + |
| 53 | +def compact(number): |
| 54 | + """Convert the number to the minimal representation. This strips the |
| 55 | + number of any valid separators and removes surrounding whitespace.""" |
| 56 | + return clean(number, ' -*').strip() |
| 57 | + |
| 58 | + |
| 59 | +def split(number): |
| 60 | + number = compact(number) |
| 61 | + index = 10 |
| 62 | + if len(number) > 12: |
| 63 | + index += 11 |
| 64 | + return number[:-index], number[-index:] |
| 65 | + |
| 66 | + |
| 67 | +def validate(number): |
| 68 | + """Check if the number is a valid NRIC number. This checks the length, |
| 69 | + formatting and birth date and place.""" |
| 70 | + number = compact(number) |
| 71 | + if len(number) > 13 or len(number) <= 10: |
| 72 | + raise InvalidLength() |
| 73 | + prefix, digits = split(number) |
| 74 | + if not prefix or len(prefix) > 2: |
| 75 | + raise InvalidLength() |
| 76 | + if not isdigits(digits): |
| 77 | + raise InvalidFormat() |
| 78 | + return number |
| 79 | + |
| 80 | + |
| 81 | +def is_valid(number): |
| 82 | + """Check if the number is a valid NRIC number.""" |
| 83 | + try: |
| 84 | + return bool(validate(number)) |
| 85 | + except ValidationError: |
| 86 | + return False |
| 87 | + |
| 88 | + |
| 89 | +def format(number): |
| 90 | + """Reformat the number to the standard presentation format.""" |
| 91 | + return ' '.join(split(number)) |
0 commit comments