1# rif.py - functions for handling Venezuelan VAT numbers
2# coding: utf-8
3#
4# Copyright (C) 2019 Arthur de Jong
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19# 02110-1301 USA
20
21"""RIF (Registro de Identificación Fiscal, Venezuelan VAT number).
22
23The Registro de Identificación Fiscal (RIF) is the Venezuelan fiscal
24registration number. The number consists of 10 digits where the first digit
25denotes the type of number (person, company or government) and the last digit
26is a check digit.
27
28>>> validate('V-11470283-4')
29'V114702834'
30>>> validate('V-11470283-3')  # invalid check digit
31Traceback (most recent call last):
32    ...
33InvalidChecksum: ...
34"""
35
36from stdnum.exceptions import *
37from stdnum.util import clean, isdigits
38
39
40def compact(number):
41    """Convert the number to the minimal representation. This strips the
42    number of any valid separators and removes surrounding whitespace."""
43    return clean(number, ' -').upper().strip()
44
45
46# Known number types and their corresponding value in the check
47# digit calculation
48_company_types = {
49    'V': 4,   # natural person born in Venezuela
50    'E': 8,   # foreign natural person
51    'J': 12,  # company
52    'P': 16,  # passport
53    'G': 20,  # government
54}
55
56
57def calc_check_digit(number):
58    """Calculate the check digit for the RIF."""
59    number = compact(number)
60    weights = (3, 2, 7, 6, 5, 4, 3, 2)
61    c = _company_types[number[0]]
62    c += sum(w * int(n) for w, n in zip(weights, number[1:9]))
63    return '00987654321'[c % 11]
64
65
66def validate(number):
67    """Check if the number provided is a valid RIF. This checks the length,
68    formatting and check digit."""
69    number = compact(number)
70    if len(number) != 10:
71        raise InvalidLength()
72    if number[0] not in _company_types:
73        raise InvalidComponent()
74    if not isdigits(number[1:]):
75        raise InvalidFormat()
76    if number[-1] != calc_check_digit(number):
77        raise InvalidChecksum()
78    return number
79
80
81def is_valid(number):
82    """Check if the number provided is a valid RIF. This checks the length,
83    formatting and check digit."""
84    try:
85        return bool(validate(number))
86    except ValidationError:
87        return False
88