Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/stdnum/mx/rfc.py
blob: 10da251add848e932ae9c2b49245780a93574a7a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# rfc.py - functions for handling Mexican tax numbers
# coding: utf-8
#
# Copyright (C) 2015 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

"""RFC (Registro Federal de Contribuyentes, Mexican tax number).

This number is used to identify individuals and companies for tax purposes.

The company number is 12 digits where the first 3 letters or digits are
derived from the name of the company, the following 6 contain the date of
incorporation, followed by 3 check digits.

Personal numbers consist of 13 digits where the first 4 characters from the
person's name, followed by their birth date and 3 check digits.

The first two check digits are calculated based on the person's or company's
full name. The last check digit is calculated over all the preceding digits
in the number. However, it seems a lot of numbers (estimated at around 1.5%
of all numbers) are in use with invalid check digits so this test is disabled
by default.

More information:

* http://www.sisi.org.mx/jspsi/documentos/2005/seguimiento/06101/0610100162005_065.doc
* https://es.wikipedia.org/wiki/Registro_Federal_de_Contribuyentes_(M%C3%A9xico)

An online validation service is available at:

* https://portalsat.plataforma.sat.gob.mx/ConsultaRFC/

>>> validate('GODE 561231 GR8')  # personal number
'GODE561231GR8'
>>> validate('MAB-930714-8T4')  # company number
'MAB9307148T4'
>>> validate('COMG-600703')  # personal number without serial
'COMG600703'
>>> validate('VACE-460910-SX6')
'VACE460910SX6'
>>> validate('VACE-460910-SX6', validate_check_digits=True)
Traceback (most recent call last):
    ...
InvalidChecksum: ...
>>> format('GODE561231GR8')
'GODE 561231 GR8'
"""

import datetime
import re

from stdnum.exceptions import *
from stdnum.util import clean


# regular expression for matching numbers
_rfc_re = re.compile(r'^[A-Z&Ñ]{3,4}[0-9]{6}[0-9A-Z]{0,5}$')


# regular expression for matching the last 3 check digits
_check_digits_re = re.compile(r'^[1-9A-V][1-9A-Z][0-9A]$')


# these values should not appear as first part of a personal number
_name_blacklist = set([
    'BUEI', 'BUEY', 'CACA', 'CACO', 'CAGA', 'CAGO', 'CAKA', 'CAKO', 'COGE',
    'COJA', 'COJE', 'COJI', 'COJO', 'CULO', 'FETO', 'GUEY', 'JOTO', 'KACA',
    'KACO', 'KAGA', 'KAGO', 'KAKA', 'KOGE', 'KOJO', 'KULO', 'MAME', 'MAMO',
    'MEAR', 'MEAS', 'MEON', 'MION', 'MOCO', 'MULA', 'PEDA', 'PEDO', 'PENE',
    'PUTA', 'PUTO', 'QULO', 'RATA', 'RUIN',
])


# characters used for checksum calculation,
_alphabet = '0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ Ñ'


def compact(number):
    """Convert the number to the minimal representation. This strips
    surrounding whitespace and separation dash."""
    return clean(number, '-_ ').upper().strip()


def _get_date(number):
    """Convert the part of the number that represents a date into a
    datetime. Note that the century may be incorrect."""
    year = int(number[0:2])
    month = int(number[2:4])
    day = int(number[4:6])
    try:
        return datetime.date(year + 2000, month, day)
    except ValueError:
        raise InvalidComponent()


def calc_check_digit(number):
    """Calculate the check digit. The number passed should not have the
    check digit included."""
    number = ('   ' + number)[-12:]
    check = sum(_alphabet.index(n) * (13 - i) for i, n in enumerate(number))
    return _alphabet[(11 - check) % 11]


def validate(number, validate_check_digits=False):
    """Checks to see if the number provided is a valid number."""
    number = compact(number)
    if not _rfc_re.match(number):
        raise InvalidFormat()
    if len(number) in (10, 13):
        # number assigned to person
        if number[:4] in _name_blacklist:
            raise InvalidComponent()
        _get_date(number[4:10])
    elif len(number) == 12:
        # number assigned to company
        _get_date(number[3:9])
    else:
        raise InvalidLength()
    if validate_check_digits and len(number) >= 12:
        if not _check_digits_re.match(number[-3:]):
            raise InvalidComponent()
        if number[-1] != calc_check_digit(number[:-1]):
            raise InvalidChecksum()
    return number


def is_valid(number, validate_check_digits=False):
    """Checks to see if the number provided is a valid number."""
    try:
        return bool(validate(number, validate_check_digits))
    except ValidationError:
        return False


def format(number, separator=' '):
    """Reformat the passed number to the standard format."""
    number = compact(number)
    if len(number) == 12:
        return separator.join((
            number[:3], number[3:9], number[9:])).strip(separator)
    return separator.join((
        number[:4], number[4:10], number[10:])).strip(separator)