8000 Add support for Egypt TIN · arthurdejong/python-stdnum@4bb5d70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4bb5d70

Browse files
committed
Add support for Egypt TIN
Closes #225.
1 parent 2b6e087 commit 4bb5d70

File tree

3 files changed

+420
-0
lines changed

3 files changed

+420
-0
lines changed

stdnum/eg/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# __init__.py - collection of Egypt 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+
"""Collection of Egypt numbers."""
22+
23+
# provide aliases
24+
from stdnum.eg import tn as vat # noqa: F401

stdnum/eg/tn.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
"""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('12345')
33+
Traceback (most recent call last):
34+
...
35+
InvalidLength: ...
36+
>>> validate('VV3456789')
37+
Traceback (most recent call last):
38+
...
39+
InvalidFormat: ...
40+
>>> format('100531385')
41+
'100-531-385'
42+
""" # noqa: E501
43+
44+
from stdnum.exceptions import *
45+
from stdnum.util import clean, isdigits
46+
47+
48+
def compact(number):
49+
"""Convert the number to the minimal representation.
50+
51+
This strips the number of any valid separators and removes surrounding
52+
whitespace. It also converts arabic numbers.
53+
"""
54+
return str(clean(number, u' -–/').strip())
55+
56+
57+
def validate(number):
58+
"""Check if the number is a valid Egypt Tax Number number.
59+
60+
This checks the length and formatting.
61+
"""
62+
number = compact(number)
63+
if len(number) != 9:
64+
raise InvalidLength()
65+
if not isdigits(number):
66+
raise InvalidFormat()
67+
return number
68+
69+
70+
def is_valid(number):
71+
"""Check if the number is a valid Egypt Tax Number number."""
72+
try:
73+
return bool(validate(number))
74+
except ValidationError:
75+
return False
76+
77+
78+
def format(number):
79+
"""Reformat the number to the standard presentation format."""
80+
number = compact(number)
81+
return '-'.join([number[:3], number[3:-3], number[-3:]])

0 commit comments

Comments
 (0)
0