1# nrt.py - functions for handling Andorra NRT numbers
2# coding: utf-8
3#
4# Copyright (C) 2019 Leandro Regueiro
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"""NRT (Número de Registre Tributari, Andorra tax number).
22
23The Número de Registre Tributari (NRT) is an identifier of legal and natural
24entities for tax purposes.
25
26This number consists of one letter indicating the type of entity, then 6
27digits, followed by a check letter.
28
29More information:
30
31* https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Andorra-TIN.pdf
32
33>>> validate('U-132950-X')
34'U132950X'
35>>> validate('A123B')
36Traceback (most recent call last):
37    ...
38InvalidLength: ...
39>>> validate('I 706193 G')
40Traceback (most recent call last):
41    ...
42InvalidComponent: ...
43>>> format('D059888N')
44'D-059888-N'
45"""
46
47from stdnum.exceptions import *
48from stdnum.util import clean, isdigits
49
50
51def compact(number):
52    """Convert the number to the minimal representation.
53
54    This strips the number of any valid separators and removes surrounding
55    whitespace.
56    """
57    return clean(number, ' -.').upper().strip()
58
59
60def validate(number):
61    """Check if the number is a valid Andorra NRT number.
62
63    This checks the length, formatting and other contraints. It does not check
64    for control letter.
65    """
66    number = compact(number)
67    if len(number) != 8:
68        raise InvalidLength()
69    if not number[0].isalpha() or not number[-1].isalpha():
70        raise InvalidFormat()
71    if not isdigits(number[1:-1]):
72        raise InvalidFormat()
73    if number[0] not in 'ACDEFGLOPU':
74        raise InvalidComponent()
75    if number[0] == 'F' and number[1:-1] > '699999':
76        raise InvalidComponent()
77    if number[0] in 'AL' and not ('699999' < number[1:-1] < '800000'):
78        raise InvalidComponent()
79    return number
80
81
82def is_valid(number):
83    """Check if the number is a valid Andorra NRT number."""
84    try:
85        return bool(validate(number))
86    except ValidationError:
87        return False
88
89
90def format(number):
91    """Reformat the number to the standard presentation format."""
92    number = compact(number)
93    return '-'.join([number[0], number[1:-1], number[-1]])
94