Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/es
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2012-02-11 15:44:35 +0100
committerArthur de Jong <arthur@arthurdejong.org>2012-02-11 15:44:35 +0100
commitaa90c4f62ebbcbf478610211ffdacf04cf78f8e6 (patch)
treea9142d0958bf63d768a56b92a98b97421eed9dbc /stdnum/es
parenta574e6c21caf9c6f41198ef5442fccc5d5c4d53c (diff)
add a CIF (Certificado de Identificación Fiscal, Spanish tax identification number) module
git-svn-id: http://arthurdejong.org/svn/python-stdnum/python-stdnum@114 9dea7c4f-944c-4273-ac1a-574ede026edc
Diffstat (limited to 'stdnum/es')
-rw-r--r--stdnum/es/cif.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/stdnum/es/cif.py b/stdnum/es/cif.py
new file mode 100644
index 0000000..1d12466
--- /dev/null
+++ b/stdnum/es/cif.py
@@ -0,0 +1,91 @@
+# cif.py - functions for handling Spanish fiscal 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 Spanish tax identification number (Certificado de
+Identificación Fiscal) for legal entities. The CIF is a 9 digit number
+where the first digit is a letter (denoting the type of entity) and the
+last is a check digit (which may also be a letter).
+
+>>> compact('J-99216582')
+'J99216582'
+>>> is_valid('J99216582')
+True
+>>> is_valid('J99216583') # invalid check digit
+False
+>>> is_valid('M-1234567-L')
+True
+>>> is_valid('O-1234567-L') # invalid first character
+False
+>>> split('A13 585 625')
+('A', '13', '58562', '5')
+"""
+
+from stdnum.es import dni
+from stdnum.util import digitsum
+
+
+__all__ = ['compact', 'is_valid', 'split']
+
+
+# use the same compact function as DNI
+compact = dni.compact
+
+
+def calc_check_digits(number):
+ """Calculate the check digits for the specified number. The number
+ passed should not have the check digit included. This function returns
+ both the number and character check digit candidates."""
+ check = (10 - digitsum(
+ (2 - (i % 2)) * int(n)
+ for i, n in enumerate(number[1:])
+ )) % 10
+ return str(check) + 'JABCDEFGHI'[check]
+
+
+def is_valid(number):
+ """Checks to see if the number provided is a valid DNI number. This
+ checks the length, formatting and check digit."""
+ try:
+ number = compact(number)
+ except:
+ return False
+ if len(number) != 9 or not number[1:-1].isdigit():
+ return False
+ if number[0] in 'KLM':
+ # K: Spanish younger than 14 year old
+ # L: Spanish living outside Spain without DNI
+ # M: granted the tax to foreigners who have no NIE
+ # these use the old checkdigit algorithm (the DNI one)
+ return number[-1] == dni.calc_check_digit(number[1:-1])
+ # there seems to be conflicting information on which organisation types
+ # should have which type of check digit (alphabetic or numeric) so
+ # we support either here
+ if number[0] in 'ABCDEFGHJNPQRSUVW':
+ return number[-1] in calc_check_digits(number[:-1])
+ # anything else is invalid
+ return False
+
+
+def split(number):
+ """Split the provided number into a letter to define the type of
+ organisation, two digits that specify a province, a 5 digit sequence
+ number within the province and a check digit."""
+ number = compact(number)
+ return number[0], number[1:3], number[3:8], number[8:]