[FIX] base_iban: IBAN constraint trigger
[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 openerp.osv import fields, osv
24 from openerp.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 is_iban_valid(self, cr, uid, iban, context=None):
98         """ Check if IBAN is valid or not
99             @param iban: IBAN as string
100             @return: True if IBAN is valid, False otherwise
101         """
102         if not iban:
103             return False
104         iban = _format_iban(iban).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         return int(iban2) % 97 == 1
118
119     def check_iban(self, cr, uid, ids, context=None):
120         '''
121         Check the IBAN number
122         '''
123         for bank_acc in self.browse(cr, uid, ids, context=context):
124             if bank_acc.state != 'iban':
125                 continue
126             if not self.is_iban_valid(cr, uid, bank_acc.acc_number, context=context):
127                 return False
128         return True
129
130     def _construct_constraint_msg(self, cr, uid, ids, context=None):
131
132         def default_iban_check(iban_cn):
133              return iban_cn and iban_cn[0] in string.ascii_lowercase and iban_cn[1] in string.ascii_lowercase
134
135         iban_country = self.browse(cr, uid, ids)[0].acc_number and self.browse(cr, uid, ids)[0].acc_number[:2].lower()
136         if default_iban_check(iban_country):
137             if iban_country in _ref_iban:
138                 return _('The IBAN does not seem to be correct. You should have entered something like this %s'), \
139                         ('%s \nWhere B = National bank code, S = Branch code,'\
140                          ' C = Account No, K = Check digit' % _ref_iban[iban_country])
141             return _('This IBAN does not pass the validation check, please verify it'), ()
142         return _('The IBAN is invalid, it should begin with the country code'), ()
143
144     def _check_bank(self, cr, uid, ids, context=None):
145         for partner_bank in self.browse(cr, uid, ids, context=context):
146             if partner_bank.state == 'iban' and not partner_bank.bank.bic:
147                 return False
148         return True
149
150     def get_bban_from_iban(self, cr, uid, ids, context=None):
151         '''
152         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.
153         '''
154         res = {}
155         mapping_list = {
156          #TODO add rules for others countries
157             'be': lambda x: x[4:],
158             'fr': lambda x: x[14:],
159             'ch': lambda x: x[9:],
160             'gb': lambda x: x[14:],
161         }
162         for record in self.browse(cr, uid, ids, context=context):
163             if not record.acc_number:
164                 res[record.id] = False
165                 continue
166             res[record.id] = False
167             for code, function in mapping_list.items():
168                 if record.acc_number.lower().startswith(code):
169                     res[record.id] = function(record.acc_number)
170                     break
171         return res
172
173     _columns = {
174         # Deprecated: we keep it for backward compatibility, to be removed in v7
175         # We use acc_number instead of IBAN since v6.1, but we keep this field
176         # to not break community modules.
177         'iban': fields.related('acc_number', string='IBAN', size=34, readonly=True, help="International Bank Account Number", type="char"),
178     }
179     _constraints = [
180         (check_iban, _construct_constraint_msg, ["iban", "acc_number", "state"]),
181         (_check_bank, '\nPlease define BIC/Swift code on bank for bank type IBAN Account to make valid payments', ['bic'])
182     ]
183
184
185 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: