8000 Add Israeli TIN number · unho/python-stdnum@c81778a · GitHub
[go: up one dir, main page]

Skip to content

Commit c81778a

Browse files
committed
Add Israeli TIN number
Fixes arthurdejong#107.
1 parent 8433821 commit c81778a

File tree

3 files changed

+518
-0
lines changed

3 files changed

+518
-0
lines changed

stdnum/il/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
# 02110-1301 USA
2020

2121
"""Collection of Israeli numbers."""
22+
23+
# provide aliases
24+
from stdnum.il import hp as vat # noqa: F401

stdnum/il/hp.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# hp.py - functions for handling Israeli company numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2020 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+
"""Company Number (מספר חברה, or short ח.פ. Israeli company number).
22+
23+
It consists of nine digits and includes a check digit. For companies
24+
the first digit is a 5. The first two digits identify the type of
25+
company.
26+
27+
More information:
28+
29+
* https://he.wikipedia.org/wiki/תאגיד#מספר_רישום_התאגיד
30+
* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Israel-TIN.pdf
31+
* https://wiki.scn.sap.com/wiki/display/CRM/Israel
32+
33+
>>> validate('516179157')
34+
'516179157'
35+
>>> format(' 5161 79157 ')
36+
'516179157'
37+
>>> validate('516179150') # invalid check digit
38+
Traceback (most recent call last):
39+
...
40+
InvalidChecksum: ...
41+
>>> validate('490154203237518') # longer than 9 digits
42+
Traceback (most recent call last):
43+
...
44+
InvalidLength: ...
45+
>>> validate('416179157')
46+
Traceback (most recent call last):
47+
...
48+
InvalidComponent: ...
49+
"""
50+
51+
from stdnum import luhn
52+
from stdnum.exceptions import *
53+
from stdnum.util import clean, isdigits
54+
55+
56+
def compact(number):
57+
"""Convert the number to the minimal representation. This strips the
58+
number of any separators and removes surrounding whitespace."""
59+
return clean(number, ' -').strip()
60+
61+
62+
def validate(number):
63+
"""Check if the number provided is a valid ID. This checks the length,
64+
formatting and check digit."""
65+
number = compact(number)
66+
if len(number) != 9:
67+
raise InvalidLength()
68+
if not isdigits(number) or int(number) <= 0:
69+
raise InvalidFormat()
70+
if number[0] != '5':
71+
raise InvalidComponent()
72+
luhn.validate(number)
73+
return number
74+
75+
76+
def is_valid(number):
77+
"""Check if the number provided is a valid ID. This checks the length,
78+
formatting and check digit."""
79+
try:
80+
return bool(validate(number))
81+
except ValidationError:
82+
return False
83+
84+
85+
def format(number):
86+
"""Reformat the number to the standard presentation format."""
87+
return compact(number)

0 commit comments

Comments
 (0)
0