1# iso11649.py - functions for performing the ISO 11649 checksum validation
2#               for structured creditor reference numbers
3#
4# Copyright (C) 2018 Esben Toke Christensen
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"""ISO 11649 (Structured Creditor Reference).
22
23The ISO 11649 structured creditor number consists of 'RF' followed by two
24check digits and up to 21 digits. The number may contain letters.
25
26The reference number is validated by moving RF and the check digits to the
27end of the number, and checking that the ISO 7064 Mod 97, 10 checksum of this
28string is 1.
29
30More information:
31
32* https://en.wikipedia.org/wiki/Creditor_Reference
33
34>>> validate('RF18 5390 0754 7034')
35'RF18539007547034'
36>>> validate('RF18 5390 0754 70Y')
37'RF185390075470Y'
38>>> is_valid('RF18 5390 0754 7034')
39True
40>>> validate('RF17 5390 0754 7034')
41Traceback (most recent call last):
42    ...
43InvalidChecksum: ...
44>>> format('RF18539007547034')
45'RF18 5390 0754 7034'
46"""
47
48from stdnum.exceptions import *
49from stdnum.iso7064 import mod_97_10
50from stdnum.util import clean
51
52
53def compact(number):
54    """Convert the number to the minimal representation. This strips the
55    number of any invalid separators and removes surrounding whitespace."""
56    return clean(number, ' -.,/:').upper().strip()
57
58
59def validate(number):
60    """Check if the number provided is a valid ISO 11649 structured creditor
61    reference number."""
62    number = compact(number)
63    if len(number) < 5 or len(number) > 25:
64        raise InvalidLength()
65    if not number.startswith('RF'):
66        raise InvalidFormat()
67    mod_97_10.validate(number[4:] + number[:4])
68    return number
69
70
71def is_valid(number):
72    """Check if the number provided is a valid ISO 11649 structured creditor
73    number. This checks the length, formatting and check digits."""
74    try:
75        return bool(validate(number))
76    except ValidationError:
77        return False
78
79
80def format(number):
81    """Format the number provided for output.
82
83    Blocks of 4 characters, the last block can be less than 4 characters. See
84    https://www.paymentstandards.ch/dam/downloads/ig-qr-bill-en.pdf chapter
85    3.6.2.
86    """
87    number = compact(number)
88    return ' '.join(number[i:i + 4] for i in range(0, len(number), 4))
89