8000 Add support for Indonesian NPWP · viggo-devries/python-stdnum@7112874 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7112874

Browse files
unhoarthurdejong
authored andcommitted
Add support for Indonesian NPWP
Closes arthurdejong#106 Closes arthurdejong#198
1 parent a34a76d commit 7112874

File tree

3 files changed

+271
-0
lines changed

3 files changed

+271
-0
lines changed

stdnum/id/__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 Indonesian 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+
"""Collection of Indonesian numbers."""
22+
23+
# provide aliases
24+
from stdnum.id import npwp as vat # noqa: F401

stdnum/id/npwp.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# npwp.py - functions for handling Indonesian NPWP 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+
"""NPWP (Nomor Pokok Wajib Pajak, Indonesian VAT Number).
22+
23+
The Nomor Pokok Wajib Pajak (NPWP) is assigned to organisations and
24+
individuals (families) by the Indonesian Tax Office after registration by the
25+
tax payers.
26+
27+
The number consists of 15 digits of which the first 2 denote the type of
28+
entity, 6 digits to identify the tax payer, a check digit over the first 8
29+
digits followed by 3 digits to identify the local tax office and 3 digits for
30+
branch code.
31+
32+
More information:
33+
34+
* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Indonesia-TIN.pdf
35+
* https://metacpan.org/pod/Business::ID::NPWP
36+
* https://wiki.scn.sap.com/wiki/display/CRM/Indonesia
37+
38+
>>> validate('01.312.166.0-091.000')
39+
'013121660091000'
40+
>>> validate('016090524017000')
41+
'016090524017000'
42+
>>> validate('123456789')
43+
Traceback (most recent call last):
44+
...
45+
InvalidLength: ...
46+
>>> format('013000666091000')
47+
'01.300.066.6-091.000'
48+
"""
49+
50+
from stdnum import luhn
51+
from stdnum.exceptions import *
52+
from stdnum.util import clean, isdigits
53+
54+
55+
def compact(number):
56+
"""Convert the number to the minimal representation.
57+
58+
This strips the number of any valid separators and removes
59+
surrounding whitespace.
60+
"""
61+
return clean(number, ' -.').strip()
62+
63+
64+
def validate(number):
65+
"""Check if the number is a valid Indonesia NPWP number."""
66+
number = compact(number)
67+
if len(number) != 15:
68+
raise InvalidLength()
69+
if not isdigits(number):
70+
raise InvalidFormat()
71+
luhn.validate(number[:9])
72+
return number
73+
74+
75+
def is_valid(number):
76+
"""Check if the number is a valid Indonesia NPWP number."""
77+
try:
78+
return bool(validate(number))
79+
except ValidationError:
80+
return False
81+
82+
83+
def format(number):
84+
"""Reformat the number to the standard presentation format."""
85+
number = compact(number)
86+
return '%s.%s.%s.%s-%s.%s' % (
87+
number[:2], number[2:5], number[5:8], number[8], number[9:12], number[12:])

tests/test_id_npwp.doctest

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
test_id_npwp.doctest - more detailed doctests for stdnum.id.npwp module
2+
3+
Copyright (C) 2020 Leandro Regueiro
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+
21+
This file contains more detailed doctests for the stdnum.id.npwp module. It
22+
tries to test more corner cases and detailed functionality that is not really
23+
useful as module documentation.
24+
25+
>>> from stdnum.id import npwp
26+
27+
28+
Tests for some corner cases.
29+
30+
>>> npwp.validate('023869720091000')
31+
'023869720091000'
32+
>>> npwp.validate('01.451.869.0-054.000')
33+
'014518690054000'
34+
>>> npwp.validate('02 . 433 . 917 . 8 - 011 . 000')
35+
'024339178011000'
36+
>>> npwp.format('013319595054000')
37+
'01.331.959.5-054.000'
38+
>>> npwp.validate('12345678901234')
39+
Traceback (most recent call last):
40+
...
41+
InvalidLength: ...
42+
>>> npwp.validate('FF3456789012345')
43+
Traceback (most recent call last):
44+
...
45+
InvalidFormat: ...
46+
>>> npwp.validate('01.451.869.7-054.000')
47+
Traceback (most recent call last):
48+
...
49+
InvalidChecksum: ...
50+
51+
52+
These have been found online and should all be valid numbers.
53+
54+
>>> numbers = '''
55+
...
56+
... 01.001.610.3-051.000
57+
... 01.001.634.3-093.000
58+
... 01.105.197.6-424.000
59+
... 01.112.397.3-441.000
60+
... 01.125.612.0-805.000
61+
... 01.129.154.9-201.000
62+
... 01.216.271.5-441.000
63+
... 01.258.022.1-201.000
64+
... 01.268.426.2-201.000
65+
... 01.311.830.2-073.000
66+
... 01.318.451.0-054.000
67+
... 01.334.427.0-054.000
68+
... 01.347.428.3-018.000
69+
... 01.360.902.9-054.000
70+
... 01.436.246.1-441.000
71+
... 01.452.340.1-101.000
72+
... 01.455.616.1-441.000
73+
... 01.457.751.4-426.000
74+
... 01.464.753.1-542.000
75+
... 01.482.738.0-424.000
76+
... 01.504.513.1-054.000
77+
... 01.546.704.6-012.000
78+
... 01.546.836.6-013.000
79+
... 01.548.436.3-423.000
80+
... 01.555.547.7-429.000
81+
... 01.562.420.8-805.000
82+
... 01.575.888.1-101.000
83+
... 01.580.769.6-201.000
84+
... 01.627.251.0-404.000
85+
... 01.657.878.3-101.000
86+
... 01.671.242.4-054.000
87+
... 01.677.930.8-429.000
88+
... 01.690.679.4-801.000
89+
... 01.709.626.4-054.000
90+
... 01.721.123.6-054.000
91+
... 01.773.770.1-805.000
92+
... 01.796.259.8-805.000
93+
... 01.815.656.2-807.000
94+
... 01.931.919.3-101.000
95+
... 01.966.099.2-801.000
96+
... 01.997.870.9-038.000
97+
... 010692903641000
98+
... 017048299643001
99+
... 02.037.103.5-502.000
100+
... 02.071.326.9-101.000
101+
... 02.071.363.2-101.000
102+
... 02.081.228.5-424.000
103+
... 02.098.700.4-013.000
104+
... 02.098.788.9-013.000
105+
... 02.102.011.0-101.000
106+
... 02.151.039.1-106.000
107+
... 02.173.740.8-002.000
108+
... 02.203.654.5-429.000
109+
... 02.333.122.6-441.000
110+
... 02.367.986.3-426.000
111+
... 02.399.244.9-091.000
112+
... 02.446.000.8-429.000
113+
... 02.480.884.2-444.000
114+
... 02.504.191.4-054.000
115+
... 02.527.123.0-017.000
116+
... 02.547.885.0-805.000
117+
... 02.564.234.9-421.000
118+
... 02.585.033.0-216.000
119+
... 02.643.101.5-805.000
120+
... 02.650.677.4-101.000
121+
... 02.679.257.2-801.000
122+
... 02.719.242.6-106.000
123+
... 02.755.249.6-541.000
124+
... 02.790.820.1-428.000
125+
... 02.887.783.5-105.000
126+
... 02.910.648.1-805.000
127+
... 02.932.094.2-101.000
128+
... 02.956.222.0-106.000
129+
... 02.956.223.8-106.000
130+
... 02.972.041.4-428.000
131+
... 02.976.331.5-103.000
132+
... 02.984.042.8-201.000
133+
... 03.026.787.6-805.000
134+
... 03.195.088.4-801.000
135+
... 03.217.024.3-805.000
136+
... 21.078.367.6-424.000
137+
... 31.294.519.9-424.000
138+
... 31.470.209.3-428.000
139+
... 31.496.676.3-106.000
140+
... 31.578.913.1-201.000
141+
... 31.682.116.4-201.000
142+
... 31.696.264.6-201.000
143+
... 31.729.429.6-542.000
144+
... 66.487.591.1-071.000
145+
... 70.838.297.3-201.000
146+
... 71.269.216.9-203.000
147+
... 75.193.535.4-805.000
148+
... 755023033513000
149+
... 762278075077000
150+
... 764038907023000
151+
... 811145994124000
152+
... 813195435017000
153+
... 83.132.665.7-201.000
154+
... 84.054.217.9-609.000
155+
... 846150324629000
156+
... 90.004.581.6-201.000
157+
...
158+
... '''
159+
>>> [x for x in numbers.splitlines() if x and not npwp.is_valid(x)]
160+
[]

0 commit comments

Comments
 (0)
0