|
| 1 | +# etin.py - functions for handling Bangladesh e-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 | +"""e-TIN (Electronic Taxpayer's Identification Number, Bangladesh tax number). |
| 22 | +
|
| 23 | +This number consists of 12 digits. |
| 24 | +
|
| 25 | +More information: |
| 26 | +
|
| 27 | +* http://bdlaws.minlaw.gov.bd/act-672/section-34615.html |
| 28 | +* https://www.incometax.gov.bd/TINHome |
| 29 | +* https://royandassociates.com.bd/how-to-register-for-e-tin-certificate-in-bangladesh/ |
| 30 | +* https://tin-check.com/en/bangladesh/ |
| 31 | +
|
| 32 | +>>> validate('224187378587') |
| 33 | +'224187378587' |
| 34 | +>>> validate('12345') |
| 35 | +Traceback (most recent call last): |
| 36 | + ... |
| 37 | +InvalidLength: ... |
| 38 | +>>> format('224187378587') |
| 39 | +'224187378587' |
| 40 | +""" # noqa: E501 |
| 41 | + |
| 42 | +from stdnum.exceptions import * |
| 43 | +from stdnum.util import clean, isdigits |
| 44 | + |
| 45 | + |
| 46 | +def compact(number): |
| 47 | + """Convert the number to the minimal representation. |
| 48 | +
|
| 49 | + This strips the number of any valid separators and removes surrounding |
| 50 | + whitespace. |
| 51 | + """ |
| 52 | + return clean(number, ' -').strip() |
| 53 | + |
| 54 | + |
| 55 | +def validate(number): |
| 56 | + """Check if the number is a valid Bangladesh e-TIN number. |
| 57 | +
|
| 58 | + This checks the length and formatting. |
| 59 | + """ |
| 60 | + number = compact(number) |
| 61 | + if len(number) != 12: |
| 62 | + raise InvalidLength() |
| 63 | + if not isdigits(number): |
| 64 | + raise InvalidFormat() |
| 65 | + return number |
| 66 | + |
| 67 | + |
| 68 | +def is_valid(number): |
| 69 | + """Check if the number is a valid Bangladesh e-TIN 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