[IMP] base_iban: don't break on empty IBAN values, support more countries, reviewed...
[odoo/odoo.git] / addons / base_iban / base_iban.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 import string
22
23 from osv import fields, osv
24 from tools.translate import _
25
26 # Reference Examples of IBAN
27 _ref_iban = { 'al':'ALkk BBBS SSSK CCCC CCCC CCCC CCCC', 'ad':'ADkk BBBB SSSS CCCC CCCC CCCC',
28 'at':'ATkk BBBB BCCC CCCC CCCC', 'be': 'BEkk BBBC CCCC CCKK', 'ba': 'BAkk BBBS SSCC CCCC CCKK',
29 'bg': 'BGkk BBBB SSSS DDCC CCCC CC', 'bh': 'BHkk BBBB SSSS SSSS SSSS SS',
30 'cr': 'CRkk BBBC CCCC CCCC CCCC C',
31 'hr': 'HRkk BBBB BBBC CCCC CCCC C', 'cy': 'CYkk BBBS SSSS CCCC CCCC CCCC CCCC',
32 'cz': 'CZkk BBBB SSSS SSCC CCCC CCCC', 'dk': 'DKkk BBBB CCCC CCCC CC',
33 'do': 'DOkk BBBB CCCC CCCC CCCC CCCC CCCC',
34  'ee': 'EEkk BBSS CCCC CCCC CCCK', 'fo': 'FOkk CCCC CCCC CCCC CC',
35  'fi': 'FIkk BBBB BBCC CCCC CK', 'fr': 'FRkk BBBB BGGG GGCC CCCC CCCC CKK',
36  'ge': 'GEkk BBCC CCCC CCCC CCCC CC', 'de': 'DEkk BBBB BBBB CCCC CCCC CC',
37  'gi': 'GIkk BBBB CCCC CCCC CCCC CCC', 'gr': 'GRkk BBBS SSSC CCCC CCCC CCCC CCC',
38  'gl': 'GLkk BBBB CCCC CCCC CC', 'hu': 'HUkk BBBS SSSC CCCC CCCC CCCC CCCC',
39  'is':'ISkk BBBB SSCC CCCC XXXX XXXX XX', 'ie': 'IEkk BBBB SSSS SSCC CCCC CC',
40  'il': 'ILkk BBBS SSCC CCCC CCCC CCC', 'it': 'ITkk KBBB BBSS SSSC CCCC CCCC CCC',
41  'kz': 'KZkk BBBC CCCC CCCC CCCC', 'kw': 'KWkk BBBB CCCC CCCC CCCC CCCC CCCC CC',
42  'lv': 'LVkk BBBB CCCC CCCC CCCC C',
43 'lb': 'LBkk BBBB CCCC CCCC CCCC CCCC CCCC', 'li': 'LIkk BBBB BCCC CCCC CCCC C',
44 'lt': 'LTkk BBBB BCCC CCCC CCCC', 'lu': 'LUkk BBBC CCCC CCCC CCCC' ,
45 'mk': 'MKkk BBBC CCCC CCCC CKK', 'mt': 'MTkk BBBB SSSS SCCC CCCC CCCC CCCC CCC',
46 'mr': 'MRkk BBBB BSSS SSCC CCCC CCCC CKK',
47 'mu': 'MUkk BBBB BBSS CCCC CCCC CCCC CCCC CC', 'mc': 'MCkk BBBB BGGG GGCC CCCC CCCC CKK',
48 'me': 'MEkk BBBC CCCC CCCC CCCC KK',
49 'nl': 'NLkk BBBB CCCC CCCC CC', 'no': 'NOkk BBBB CCCC CCK',
50 'pl':'PLkk BBBS SSSK CCCC CCCC CCCC CCCC',
51 'pt': 'PTkk BBBB SSSS CCCC CCCC CCCK K', 'ro': 'ROkk BBBB CCCC CCCC CCCC CCCC',
52 'sm': 'SMkk KBBB BBSS SSSC CCCC CCCC CCC', 'sa': 'SAkk BBCC CCCC CCCC CCCC CCCC',
53 'rs': 'RSkk BBBC CCCC CCCC CCCC KK', 'sk': 'SKkk BBBB SSSS SSCC CCCC CCCC',
54 'si': 'SIkk BBSS SCCC CCCC CKK', 'es': 'ESkk BBBB SSSS KKCC CCCC CCCC',
55 'se': 'SEkk BBBB CCCC CCCC CCCC CCCC', 'ch': 'CHkk BBBB BCCC CCCC CCCC C',
56 'tn': 'TNkk BBSS SCCC CCCC CCCC CCCC', 'tr': 'TRkk BBBB BRCC CCCC CCCC CCCC CC',
57 'ae': 'AEkk BBBC CCCC CCCC CCCC CCC',
58 'gb': 'GBkk BBBB SSSS SSCC CCCC CC',
59 }
60
61 def _format_iban(iban_str):
62     '''
63     This function removes all characters from given 'iban_str' that isn't a alpha numeric and converts it to upper case.
64     '''
65     res = ""
66     if iban_str:
67         for char in iban_str:
68             if char.isalnum():
69                 res += char.upper()
70     return res
71
72 def _pretty_iban(iban_str):
73     "return iban_str in groups of four characters separated by a single space"
74     res = []
75     while iban_str:
76         res.append(iban_str[:4])
77         iban_str = iban_str[4:]
78     return ' '.join(res)
79
80 class res_partner_bank(osv.osv):
81     _inherit = "res.partner.bank"
82
83     def create(self, cr, uid, vals, context=None):
84         #overwrite to format the iban number correctly
85         if (vals.get('state',False)=='iban') and vals.get('acc_number', False):
86             vals['acc_number'] = _format_iban(vals['acc_number'])
87             vals['acc_number'] = _pretty_iban(vals['acc_number'])
88         return super(res_partner_bank, self).create(cr, uid, vals, context)
89
90     def write(self, cr, uid, ids, vals, context=None):
91         #overwrite to format the iban number correctly
92         if (vals.get('state',False)=='iban') and vals.get('acc_number', False):
93             vals['acc_number'] = _format_iban(vals['acc_number'])
94             vals['acc_number'] = _pretty_iban(vals['acc_number'])
95         return super(res_partner_bank, self).write(cr, uid, ids, vals, context)
96
97     def check_iban(self, cr, uid, ids, context=None):
98         '''
99         Check the IBAN number
100         '''
101         for bank_acc in self.browse(cr, uid, ids, context=context):
102             if bank_acc.state<>'iban':
103                 continue
104             iban = _format_iban(bank_acc.acc_number).lower()
105             if iban[:2] in _ref_iban and len(iban) != len(_format_iban(_ref_iban[iban[:2]])):
106                 return False
107             #the four first digits have to be shifted to the end
108             iban = iban[4:] + iban[:4]
109             #letters have to be transformed into numbers (a = 10, b = 11, ...)
110             iban2 = ""
111             for char in iban:
112                 if char.isalpha():
113                     iban2 += str(ord(char)-87)
114                 else:
115                     iban2 += char
116             #iban is correct if modulo 97 == 1
117             if not int(iban2) % 97 == 1:
118                 return False
119         return True
120
121     def _construct_constraint_msg(self, cr, uid, ids, context=None):
122
123         def default_iban_check(iban_cn):
124             return iban_cn[0] in string.ascii_lowercase and iban_cn[1] in string.ascii_lowercase
125
126         iban_country = self.browse(cr, uid, ids)[0].acc_number[:2].lower()
127         if default_iban_check(iban_country):
128             if iban_country in _ref_iban:
129                 return _('The IBAN does not seem to be correct. You should have entered something like this %s'), \
130                         ('%s \nWhere B = National bank code, S = Branch code,'\
131                          ' C = Account No, K = Check digit' % _ref_iban[iban_country])
132             return _('This IBAN does not pass the validation check, please verify it'), ()
133         return _('The IBAN is invalid, it should begin with the country code'), ()
134
135     def _check_bank(self, cr, uid, ids, context=None):
136         for partner_bank in self.browse(cr, uid, ids, context=context):
137             if partner_bank.state == 'iban' and not partner_bank.bank.bic:
138                 return False
139         return True
140
141     def get_bban_from_iban(self, cr, uid, ids, context=None):
142         '''
143         This function returns the bank account number computed from the iban account number, thanks to the mapping_list dictionary that contains the rules associated to its country.
144         '''
145         res = {}
146         mapping_list = {
147          #TODO add rules for others countries
148             'be': lambda x: x[4:],
149             'fr': lambda x: x[14:],
150             'ch': lambda x: x[9:],
151             'gb': lambda x: x[14:],
152         }
153         for record in self.browse(cr, uid, ids, context=context):
154             if not record.acc_number:
155                 res[record.id] = False
156                 continue
157             res[record.id] = False
158             for code, function in mapping_list.items():
159                 if record.acc_number.lower().startswith(code):
160                     res[record.id] = function(record.acc_number)
161                     break
162         return res
163
164     _columns = {
165         # Deprecated: we keep it for backward compatibility, to be removed in v7
166         # We use acc_number instead of IBAN since v6.1, but we keep this field
167         # to not break community modules.
168         'iban': fields.related('acc_number', string='IBAN', size=34, readonly=True, help="International Bank Account Number", type="char"),
169     }
170     _constraints = [
171         (check_iban, _construct_constraint_msg, ["iban"]),
172         (_check_bank, '\nPlease define BIC/Swift code on bank for bank type IBAN Account to make valid payments', ['bic'])
173     ]
174
175 res_partner_bank()
176
177 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: