Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/es/cif.py
blob: 679e5969b7b8f9c8f44ace0a04747b8d9b7a3938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

"""CIF (Certificado de Identificación Fiscal, Spanish company tax number).

The CIF is a tax identification number for legal entities. It has 9 digits
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 import luhn
from stdnum.es import dni


__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 = luhn.calc_check_digit(number[1:])
    return check + 'JABCDEFGHI'[int(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:]