lists.arthurdejong.org
RSS feed

python-stdnum commit: r137 - in python-stdnum: . stdnum stdnum/fr tests

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

python-stdnum commit: r137 - in python-stdnum: . stdnum stdnum/fr tests



Author: arthur
Date: Fri Feb 17 23:18:48 2012
New Revision: 137
URL: http://arthurdejong.org/viewvc/python-stdnum?revision=137&view=revision

Log:
add a TVA (Numéro d'identification à la taxe sur la valeur ajoutée, French VAT 
number) module

Added:
   python-stdnum/stdnum/fr/tva.py
Modified:
   python-stdnum/README
   python-stdnum/stdnum/__init__.py
   python-stdnum/tests/test_robustness.doctest

Modified: python-stdnum/README
==============================================================================
--- python-stdnum/README        Fri Feb 17 17:27:34 2012        (r136)
+++ python-stdnum/README        Fri Feb 17 23:18:48 2012        (r137)
@@ -50,6 +50,8 @@
  * ANUM (Közösségi adószám, Hungarian VAT number)
  * KMKR (Käibemaksukohuslase, Estonian VAT number)
  * PVM (Pridėtinės vertės mokestis mokėtojo kodas, Lithuanian VAT number)
+ * TVA (Numéro d'identification à la taxe sur la valeur ajoutée, French
+   VAT number)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)
  * MEID (Mobile Equipment Identifier)

Modified: python-stdnum/stdnum/__init__.py
==============================================================================
--- python-stdnum/stdnum/__init__.py    Fri Feb 17 17:27:34 2012        (r136)
+++ python-stdnum/stdnum/__init__.py    Fri Feb 17 23:18:48 2012        (r137)
@@ -64,6 +64,8 @@
  * ANUM (Közösségi adószám, Hungarian VAT number)
  * KMKR (Käibemaksukohuslase, Estonian VAT number)
  * PVM (Pridėtinės vertės mokestis mokėtojo kodas, Lithuanian VAT number)
+ * TVA (Numéro d'identification à la taxe sur la valeur ajoutée, French
+   VAT number)
  * IMEI (International Mobile Equipment Identity)
  * IMSI (International Mobile Subscriber Identity)
  * MEID (Mobile Equipment Identifier)

Added: python-stdnum/stdnum/fr/tva.py
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ python-stdnum/stdnum/fr/tva.py      Fri Feb 17 23:18:48 2012        (r137)
@@ -0,0 +1,79 @@
+# tva.py - functions for handling French TVA 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 French TVA (Numéro d'identification à la taxe sur
+la valeur ajoutée, VAT) numbers.
+
+The number is the SIREN (Système d’Identification du Répertoire des
+Entreprises) prefixed by two digits. In old style numbers the two digits
+are numeric, with new style numbers at least one is a alphabetic.
+
+>>> compact('Fr 40 303 265 045')
+'40303265045'
+>>> is_valid('23334175221')
+True
+>>> is_valid('84 323 140 391')  # incorrect check digit
+False
+>>> is_valid('K7399859412')  # new-style number
+True
+>>> is_valid('4Z123456782')  # new-style number starting with digit
+True
+"""
+
+from stdnum.util import clean
+from stdnum.fr import siren
+
+
+# the valid characters for the first two digits (O and I are missing)
+_alphabet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ'
+
+
+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('FR'):
+        number = number[2:]
+    return number
+
+
+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
+    if len(number) == 11 and siren.is_valid(number[2:]) and \
+       number[0] in _alphabet and number[1] in _alphabet:
+        if number.isdigit():
+            # all-numeric digits
+            return int(number[:2]) == int(number[2:] + '12') % 97
+        else:
+            # one of the first two digits isn't a number
+            if number[0].isdigit():
+                check = _alphabet.index(number[0]) * 24 + \
+                        _alphabet.index(number[1]) - 10
+            else:
+                check = _alphabet.index(number[0]) * 34 + \
+                        _alphabet.index(number[1]) - 100
+            return (int(number[2:]) + 1 + check // 11 ) % 11 == check % 11
+    else:
+        return False

Modified: python-stdnum/tests/test_robustness.doctest
==============================================================================
--- python-stdnum/tests/test_robustness.doctest Fri Feb 17 17:27:34 2012        
(r136)
+++ python-stdnum/tests/test_robustness.doctest Fri Feb 17 23:18:48 2012        
(r137)
@@ -36,14 +36,14 @@
 >>> from stdnum.ee import kmkr
 >>> from stdnum.es import cif, dni, nie, nif as es_nif
 >>> from stdnum.fi import hetu, alv
->>> from stdnum.fr import siren
+>>> from stdnum.fr import siren, tva as fr_tva
 >>> from stdnum.gr import vat as gr_vat
 >>> from stdnum.hu import anum
 >>> from stdnum.ie import vat as ie_vat
 >>> from stdnum.iso7064 import mod_11_10, mod_11_2, mod_37_2, mod_37_36, 
 >>> mod_97_10
 >>> from stdnum.it import iva
 >>> from stdnum.lt import pvm
->>> from stdnum.lu import tva
+>>> from stdnum.lu import tva as lu_tva
 >>> from stdnum.lv import pvn
 >>> from stdnum.nl import bsn, onderwijsnummer, btw
 >>> from stdnum.pt import nif as pt_nif
-- 
To unsubscribe send an email to
python-stdnum-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/python-stdnum-commits/