|
| 1 | +# tn.py - functions for handling Egypt Tax Number 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 | +u"""Tax Registration Number (الرقم الضريبي, Egypt tax number). |
| 22 | +
|
| 23 | +This number consists of 9 digits, usually separated into three groups |
| 24 | +using hyphens to make it easier to read, like XXX-XXX-XXX. |
| 25 | +
|
| 26 | +More information: |
| 27 | +
|
| 28 | +* https://emsp.mts.gov.eg:8181/EMDB-web/faces/authoritiesandcompanies/authority/website/SearchAuthority.xhtml?lang=en |
| 29 | +
|
| 30 | +>>> validate('100-531-385') |
| 31 | +'100531385' |
| 32 | +>>> validate(u'٣٣١-١٠٥-٢٦٨') |
| 33 | +'331105268' |
| 34 | +>>> validate('12345') |
| 35 | +Traceback (most recent call last): |
| 36 | + ... |
| 37 | +InvalidLength: ... |
| 38 | +>>> validate('VV3456789') |
| 39 | +Traceback (most recent call last): |
| 40 | + ... |
| 41 | +InvalidFormat: ... |
| 42 | +>>> format('100531385') |
| 43 | +'100-531-385' |
| 44 | +""" # noqa: E501 |
| 45 | + |
| 46 | +from stdnum.exceptions import * |
| 47 | +from stdnum.util import clean, isdigits |
| 48 | + |
| 49 | + |
| 50 | +_ARABIC_NUMBERS_MAP = { |
| 51 | + # Arabic-indic digits. |
| 52 | + u'٠': '0', |
| 53 | + u'١': '1', |
| 54 | + u'٢': '2', |
| 55 | + u'٣': '3', |
| 56 | + u'٤': '4', |
| 57 | + u'٥': '5', |
| 58 | + u'٦': '6', |
| 59 | + u'٧': '7', |
| 60 | + u'٨': '8', |
| 61 | + u'٩': '9', |
| 62 | + # Extended arabic-indic digits. |
| 63 | + u'۰': '0', |
| 64 | + u'۱': '1', |
| 65 | + u'۲': '2', |
| 66 | + u'۳': '3', |
| 67 | + u'۴': '4', |
| 68 | + u'۵': '5', |
| 69 | + u'۶': '6', |
| 70 | + u'۷': '7', |
| 71 | + u'۸': '8', |
| 72 | + u'۹': '9', |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +def compact(number): |
| 77 | + """Convert the number to the minimal representation. |
| 78 | +
|
| 79 | + This strips the number of any valid separators and removes surrounding |
| 80 | + whitespace. It also converts arabic numbers. |
| 81 | + """ |
| 82 | + try: |
| 83 | + return str(''.join((_ARABIC_NUMBERS_MAP.get(c, c) for c in clean(number, ' -/').strip()))) |
| 84 | + except UnicodeError: # pragma: no cover (Python 2 specific) |
| 85 | + raise InvalidFormat() |
| 86 | + |
| 87 | + |
| 88 | +def validate(number): |
| 89 | + """Check if the number is a valid Egypt Tax Number number. |
| 90 | +
|
| 91 | + This checks the length and formatting. |
| 92 | + """ |
| 93 | + number = compact(number) |
| 94 | + if not isdigits(number): |
| 95 | + raise InvalidFormat() |
| 96 | + if len(number) != 9: |
| 97 | + raise InvalidLength() |
| 98 | + return number |
| 99 | + |
| 100 | + |
| 101 | +def is_valid(number): |
| 102 | + """Check if the number is a valid Egypt Tax Number number.""" |
| 103 | + try: |
| 104 | + return bool(validate(number)) |
| 105 | + except ValidationError: |
| 106 | + return False |
| 107 | + |
| 108 | + |
| 109 | +def format(number): |
| 110 | + """Reformat the number to the standard presentation format.""" |
| 111 | + number = compact(number) |
| 112 | + return '-'.join([number[:3], number[3:-3], number[-3:]]) |
0 commit comments