From 389c306a52ec12925449ecb4095b02de3bd3c132 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Thu, 16 Feb 2012 20:48:25 +0000 Subject: add a NIF (Número de identificação fiscal, Portuguese VAT number) module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@129 9dea7c4f-944c-4273-ac1a-574ede026edc --- README | 1 + stdnum/__init__.py | 2 ++ stdnum/pt/__init__.py | 0 stdnum/pt/nif.py | 58 +++++++++++++++++++++++++++++++++++++++++++ tests/test_robustness.doctest | 3 ++- 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 stdnum/pt/__init__.py create mode 100644 stdnum/pt/nif.py 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 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 -- cgit v1.2.3