Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2012-02-16 21:48:25 +0100
committerArthur de Jong <arthur@arthurdejong.org>2012-02-16 21:48:25 +0100
commit389c306a52ec12925449ecb4095b02de3bd3c132 (patch)
tree76917d545f259984116bb83245129a7e1ada1d61
parentcdc7f96bc21c9dcf13b31f1c365c6df59d217864 (diff)
add a NIF (Número de identificação fiscal, Portuguese VAT number) module
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@129 9dea7c4f-944c-4273-ac1a-574ede026edc
-rw-r--r--README1
-rw-r--r--stdnum/__init__.py2
-rw-r--r--stdnum/pt/__init__.py0
-rw-r--r--stdnum/pt/nif.py58
-rw-r--r--tests/test_robustness.doctest3
5 files changed, 63 insertions, 1 deletions
diff --git a/README b/README
index 7e4ea38..a5afe82 100644
--- a/README
+++ b/README
@@ -42,6 +42,7 @@ Currently this package supports the following formats:
* Partita IVA (Italian VAT number)
* Αριθμός Εγγραφής Φ.Π.Α. (Cypriot VAT number)
* UID (Umsatzsteuer-Identifikationsnummer, Austrian VAT number)
+ * NIF (Número de identificação fiscal, Portuguese VAT number)
* IMEI (International Mobile Equipment Identity)
* IMSI (International Mobile Subscriber Identity)
* MEID (Mobile Equipment Identifier)
diff --git a/stdnum/__init__.py b/stdnum/__init__.py
index a9e56c2..dbbff79 100644
--- a/stdnum/__init__.py
+++ b/stdnum/__init__.py
@@ -56,6 +56,7 @@ Currently this package supports the following formats:
* Partita IVA (Italian VAT number)
* Αριθμός Εγγραφής Φ.Π.Α. (Cypriot VAT number)
* UID (Umsatzsteuer-Identifikationsnummer, Austrian VAT number)
+ * NIF (Número de identificação fiscal, Portuguese VAT number)
* IMEI (International Mobile Equipment Identity)
* IMSI (International Mobile Subscriber Identity)
* MEID (Mobile Equipment Identifier)
@@ -64,6 +65,7 @@ Currently this package supports the following formats:
* ISIL (International Standard Identifier for Libraries and Related
Organizations)
+
Furthermore a number of generic check digit algorithms are available:
* the Verhoeff algorithm
diff --git a/stdnum/pt/__init__.py b/stdnum/pt/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/stdnum/pt/__init__.py
diff --git a/stdnum/pt/nif.py b/stdnum/pt/nif.py
new file mode 100644
index 0000000..71aed1b
--- /dev/null
+++ b/stdnum/pt/nif.py
@@ -0,0 +1,58 @@
+# nif.py - functions for handling Portuguese VAT numbers
+# coding: utf-8
+#
+# Copyright (C) 2012 Arthur de Jong
+#
+# 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
+
+"""Module for handling Portuguese NIF (Número de identificação fiscal,
+NIPC, Número de Identificação de Pessoa Colectiva, VAT) numbers.
+
+>>> compact('PT 501 964 843')
+'501964843'
+>>> is_valid('PT 501 964 843')
+True
+>>> is_valid('PT 501 964 842') # invalid check digits
+False
+"""
+
+from stdnum.util import clean
+
+
+def compact(number):
+ """Convert the number to the minimal representation. This strips the
+ number of any valid separators and removes surrounding whitespace."""
+ number = clean(number, ' -.').upper().strip()
+ if number.startswith('PT'):
+ number = number[2:]
+ return number
+
+
+def calc_check_digit(number):
+ """Calculate the check digit. The number passed should not have the
+ check digit included."""
+ return str((11 - sum((9 - i) * int(n) for i, n in enumerate(number)) ) % 11 % 10)
+
+
+def is_valid(number):
+ """Checks to see if the number provided is a valid VAT number. This
+ checks the length, formatting and check digit."""
+ try:
+ number = compact(number)
+ except:
+ return False
+ return number.isdigit() and len(number) == 9 and \
+ number[0] != '0' and calc_check_digit(number[:-1]) == number[-1]
diff --git a/tests/test_robustness.doctest b/tests/test_robustness.doctest
index 9559253..1026781 100644
--- a/tests/test_robustness.doctest
+++ b/tests/test_robustness.doctest
@@ -33,7 +33,7 @@ invalid junk.
>>> from stdnum.cz import rc
>>> from stdnum.de import vat as de_vat
>>> from stdnum.dk import cvr
->>> from stdnum.es import cif, dni, nie, nif
+>>> from stdnum.es import cif, dni, nie, nif as es_nif
>>> from stdnum.fi import hetu
>>> from stdnum.fr import siren
>>> from stdnum.gr import vat as gr_vat
@@ -42,6 +42,7 @@ invalid junk.
>>> from stdnum.lu import tva
>>> from stdnum.lv import pvn
>>> from stdnum.nl import bsn, onderwijsnummer, btw
+>>> from stdnum.pt import nif as pt_nif
>>> from stdnum.ro import cf, cnp
>>> from stdnum.us import ssn