lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.13-34-g7112874

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 1.13-34-g7112874



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  71128746b8215568d1ea15d2577b3408670f4892 (commit)
      from  a34a76dc657e6213e5116fdb399cba75bdba67cb (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=71128746b8215568d1ea15d2577b3408670f4892

commit 71128746b8215568d1ea15d2577b3408670f4892
Author: Leandro Regueiro <leandro.regueiro@gmail.com>
Date:   Sun Mar 8 13:36:16 2020 +0100

    Add support for Indonesian NPWP
    
    Closes https://github.com/arthurdejong/python-stdnum/issues/106
    Closes https://github.com/arthurdejong/python-stdnum/pull/198

diff --git a/stdnum/id/__init__.py b/stdnum/id/__init__.py
new file mode 100644
index 0000000..a574bd5
--- /dev/null
+++ b/stdnum/id/__init__.py
@@ -0,0 +1,24 @@
+# __init__.py - collection of Indonesian numbers
+# coding: utf-8
+#
+# Copyright (C) 2020 Leandro Regueiro
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""Collection of Indonesian numbers."""
+
+# provide aliases
+from stdnum.id import npwp as vat  # noqa: F401
diff --git a/stdnum/id/npwp.py b/stdnum/id/npwp.py
new file mode 100644
index 0000000..de14a54
--- /dev/null
+++ b/stdnum/id/npwp.py
@@ -0,0 +1,87 @@
+# npwp.py - functions for handling Indonesian NPWP numbers
+# coding: utf-8
+#
+# Copyright (C) 2020 Leandro Regueiro
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""NPWP (Nomor Pokok Wajib Pajak, Indonesian VAT Number).
+
+The Nomor Pokok Wajib Pajak (NPWP) is assigned to organisations and
+individuals (families) by the Indonesian Tax Office after registration by the
+tax payers.
+
+The number consists of 15 digits of which the first 2 denote the type of
+entity, 6 digits to identify the tax payer, a check digit over the first 8
+digits followed by 3 digits to identify the local tax office and 3 digits for
+branch code.
+
+More information:
+
+* 
https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Indonesia-TIN.pdf
+* https://metacpan.org/pod/Business::ID::NPWP
+* https://wiki.scn.sap.com/wiki/display/CRM/Indonesia
+
+>>> validate('01.312.166.0-091.000')
+'013121660091000'
+>>> validate('016090524017000')
+'016090524017000'
+>>> validate('123456789')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> format('013000666091000')
+'01.300.066.6-091.000'
+"""
+
+from stdnum import luhn
+from stdnum.exceptions import *
+from stdnum.util import clean, isdigits
+
+
+def compact(number):
+    """Convert the number to the minimal representation.
+
+    This strips the number of any valid separators and removes
+    surrounding whitespace.
+    """
+    return clean(number, ' -.').strip()
+
+
+def validate(number):
+    """Check if the number is a valid Indonesia NPWP number."""
+    number = compact(number)
+    if len(number) != 15:
+        raise InvalidLength()
+    if not isdigits(number):
+        raise InvalidFormat()
+    luhn.validate(number[:9])
+    return number
+
+
+def is_valid(number):
+    """Check if the number is a valid Indonesia NPWP number."""
+    try:
+        return bool(validate(number))
+    except ValidationError:
+        return False
+
+
+def format(number):
+    """Reformat the number to the standard presentation format."""
+    number = compact(number)
+    return '%s.%s.%s.%s-%s.%s' % (
+        number[:2], number[2:5], number[5:8], number[8], number[9:12], 
number[12:])
diff --git a/tests/test_id_npwp.doctest b/tests/test_id_npwp.doctest
new file mode 100644
index 0000000..13a78d9
--- /dev/null
+++ b/tests/test_id_npwp.doctest
@@ -0,0 +1,160 @@
+test_id_npwp.doctest - more detailed doctests for stdnum.id.npwp module
+
+Copyright (C) 2020 Leandro Regueiro
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA
+
+
+This file contains more detailed doctests for the stdnum.id.npwp module. It
+tries to test more corner cases and detailed functionality that is not really
+useful as module documentation.
+
+>>> from stdnum.id import npwp
+
+
+Tests for some corner cases.
+
+>>> npwp.validate('023869720091000')
+'023869720091000'
+>>> npwp.validate('01.451.869.0-054.000')
+'014518690054000'
+>>> npwp.validate('02 . 433 . 917 . 8 - 011 . 000')
+'024339178011000'
+>>> npwp.format('013319595054000')
+'01.331.959.5-054.000'
+>>> npwp.validate('12345678901234')
+Traceback (most recent call last):
+    ...
+InvalidLength: ...
+>>> npwp.validate('FF3456789012345')
+Traceback (most recent call last):
+    ...
+InvalidFormat: ...
+>>> npwp.validate('01.451.869.7-054.000')
+Traceback (most recent call last):
+    ...
+InvalidChecksum: ...
+
+
+These have been found online and should all be valid numbers.
+
+>>> numbers = '''
+...
+... 01.001.610.3-051.000
+... 01.001.634.3-093.000
+... 01.105.197.6-424.000
+... 01.112.397.3-441.000
+... 01.125.612.0-805.000
+... 01.129.154.9-201.000
+... 01.216.271.5-441.000
+... 01.258.022.1-201.000
+... 01.268.426.2-201.000
+... 01.311.830.2-073.000
+... 01.318.451.0-054.000
+... 01.334.427.0-054.000
+... 01.347.428.3-018.000
+... 01.360.902.9-054.000
+... 01.436.246.1-441.000
+... 01.452.340.1-101.000
+... 01.455.616.1-441.000
+... 01.457.751.4-426.000
+... 01.464.753.1-542.000
+... 01.482.738.0-424.000
+... 01.504.513.1-054.000
+... 01.546.704.6-012.000
+... 01.546.836.6-013.000
+... 01.548.436.3-423.000
+... 01.555.547.7-429.000
+... 01.562.420.8-805.000
+... 01.575.888.1-101.000
+... 01.580.769.6-201.000
+... 01.627.251.0-404.000
+... 01.657.878.3-101.000
+... 01.671.242.4-054.000
+... 01.677.930.8-429.000
+... 01.690.679.4-801.000
+... 01.709.626.4-054.000
+... 01.721.123.6-054.000
+... 01.773.770.1-805.000
+... 01.796.259.8-805.000
+... 01.815.656.2-807.000
+... 01.931.919.3-101.000
+... 01.966.099.2-801.000
+... 01.997.870.9-038.000
+... 010692903641000
+... 017048299643001
+... 02.037.103.5-502.000
+... 02.071.326.9-101.000
+... 02.071.363.2-101.000
+... 02.081.228.5-424.000
+... 02.098.700.4-013.000
+... 02.098.788.9-013.000
+... 02.102.011.0-101.000
+... 02.151.039.1-106.000
+... 02.173.740.8-002.000
+... 02.203.654.5-429.000
+... 02.333.122.6-441.000
+... 02.367.986.3-426.000
+... 02.399.244.9-091.000
+... 02.446.000.8-429.000
+... 02.480.884.2-444.000
+... 02.504.191.4-054.000
+... 02.527.123.0-017.000
+... 02.547.885.0-805.000
+... 02.564.234.9-421.000
+... 02.585.033.0-216.000
+... 02.643.101.5-805.000
+... 02.650.677.4-101.000
+... 02.679.257.2-801.000
+... 02.719.242.6-106.000
+... 02.755.249.6-541.000
+... 02.790.820.1-428.000
+... 02.887.783.5-105.000
+... 02.910.648.1-805.000
+... 02.932.094.2-101.000
+... 02.956.222.0-106.000
+... 02.956.223.8-106.000
+... 02.972.041.4-428.000
+... 02.976.331.5-103.000
+... 02.984.042.8-201.000
+... 03.026.787.6-805.000
+... 03.195.088.4-801.000
+... 03.217.024.3-805.000
+... 21.078.367.6-424.000
+... 31.294.519.9-424.000
+... 31.470.209.3-428.000
+... 31.496.676.3-106.000
+... 31.578.913.1-201.000
+... 31.682.116.4-201.000
+... 31.696.264.6-201.000
+... 31.729.429.6-542.000
+... 66.487.591.1-071.000
+... 70.838.297.3-201.000
+... 71.269.216.9-203.000
+... 75.193.535.4-805.000
+... 755023033513000
+... 762278075077000
+... 764038907023000
+... 811145994124000
+... 813195435017000
+... 83.132.665.7-201.000
+... 84.054.217.9-609.000
+... 846150324629000
+... 90.004.581.6-201.000
+...
+... '''
+>>> [x for x in numbers.splitlines() if x and not npwp.is_valid(x)]
+[]

-----------------------------------------------------------------------

Summary of changes:
 stdnum/{sg => id}/__init__.py    |   6 +-
 stdnum/{za/tin.py => id/npwp.py} |  62 +++++++--------
 tests/test_id_npwp.doctest       | 160 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+), 33 deletions(-)
 copy stdnum/{sg => id}/__init__.py (85%)
 copy stdnum/{za/tin.py => id/npwp.py} (51%)
 create mode 100644 tests/test_id_npwp.doctest


hooks/post-receive
-- 
python-stdnum