[MERGE] improve name_get for res.partner.bank with IBAN
[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 import netsvc
24 from osv import fields, osv
25 from tools.translate import _
26
27 # Length of IBAN
28 _iban_len = {'al':28, 'ad':24, 'at':20, 'be': 16, 'ba': 20, 'bg': 22, 'hr': 21, 'cy': 28,
29 'cz': 24, 'dk': 18, 'ee': 20, 'fo':18, 'fi': 18, 'fr': 27, 'ge': 22, 'de': 22, 'gi': 23,
30 'gr': 27, 'gl': 18, 'hu': 28, 'is':26, 'ie': 22, 'il': 23, 'it': 27, 'kz': 20, 'lv': 21,
31 'lb': 28, 'li': 21, 'lt': 20, 'lu':20 ,'mk': 19, 'mt': 31, 'mu': 30, 'mc': 27, 'gb': 22,
32 'me': 22, 'nl': 18, 'no': 15, 'pl':28, 'pt': 25, 'ro': 24, 'sm': 27, 'sa': 24, 'rs': 22,
33 'sk': 24, 'si': 19, 'es': 24, 'se':24, 'ch': 21, 'tn': 24, 'tr': 26}
34
35 # Reference Examples of IBAN
36 _ref_iban = { 'al':'ALkk BBBS SSSK CCCC CCCC CCCC CCCC', 'ad':'ADkk BBBB SSSS CCCC CCCC CCCC',
37 'at':'ATkk BBBB BCCC CCCC CCCC', 'be': 'BEkk BBBC CCCC CCKK', 'ba': 'BAkk BBBS SSCC CCCC CoKK',
38 'bg': 'BGkk BBBB SSSS DDCC CCCC CC', 'hr': 'HRkk BBBB BBBC CCCC CCCC C',
39 'cy': 'CYkk BBBS SSSS CCCC CCCC CCCC CCCC',
40 'cz': 'CZkk BBBB SSSS SSCC CCCC CCCC', 'dk': 'DKkk BBBB CCCC CCCC CC',
41  'ee': 'EEkk BBSS CCCC CCCC CCCK', 'fo': 'FOkk CCCC CCCC CCCC CC',
42  'fi': 'FIkk BBBB BBCC CCCC CK', 'fr': 'FRkk BBBB BGGG GGCC CCCC CCCC CKK',
43  'ge': 'GEkk BBCC CCCC CCCC CCCC CC', 'de': 'DEkk BBBB BBBB CCCC CCCC CC',
44  'gi': 'GIkk BBBB CCCC CCCC CCCC CCC', 'gr': 'GRkk BBBS SSSC CCCC CCCC CCCC CCC',
45  'gl': 'GLkk BBBB CCCC CCCC CC', 'hu': 'HUkk BBBS SSSC CCCC CCCC CCCC CCCC',
46  'is':'ISkk BBBB SSCC CCCC XXXX XXXX XX', 'ie': 'IEkk AAAA BBBB BBCC CCCC CC',
47  'il': 'ILkk BBBN NNCC CCCC CCCC CCC', 'it': 'ITkk KAAA AABB BBBC CCCC CCCC CCC',
48  'kz': 'KZkk BBBC CCCC CCCC CCCC', 'lv': 'LVkk BBBB CCCC CCCC CCCC C',
49 'lb': 'LBkk BBBB AAAA AAAA AAAA AAAA AAAA', 'li': 'LIkk BBBB BCCC CCCC CCCC C',
50 'lt': 'LTkk BBBB BCCC CCCC CCCC', 'lu': 'LUkk BBBC CCCC CCCC CCCC' ,
51 'mk': 'MKkk BBBC CCCC CCCC CKK', 'mt': 'MTkk BBBB SSSS SCCC CCCC CCCC CCCC CCC',
52 'mu': 'MUkk BBBB BBSS CCCC CCCC CCCC CCCC CC', 'mc': 'MCkk BBBB BGGG GGCC CCCC CCCC CKK',
53 'gb': 'GBkk BBBB SSSS SSCC CCCC CC', 'me': 'MEkk BBBC CCCC CCCC CCCC KK',
54 'nl': 'NLkk BBBB CCCC CCCC CC', 'no': 'NOkk BBBB CCCC CCK', 'pl':'PLkk BBBS SSSK CCCC CCCC CCCC CCCC',
55 'pt': 'PTkk BBBB SSSS CCCC CCCC CCCK K', 'ro': 'ROkk BBBB CCCC CCCC CCCC CCCC',
56 'sm': 'SMkk KAAA AABB BBBC CCCC CCCC CCC', 'sa': 'SAkk BBCC CCCC CCCC CCCC CCCC',
57 'rs': 'RSkk BBBC CCCC CCCC CCCC KK', 'sk': 'SKkk BBBB SSSS SSCC CCCC CCCC',
58 'si': 'SIkk BBSS SCCC CCCC CKK', 'es': 'ESkk BBBB GGGG KKCC CCCC CCCC',
59 'se': 'SEkk BBBB CCCC CCCC CCCC CCCC', 'ch': 'CHkk BBBB BCCC CCCC CCCC C',
60 'tn': 'TNkk BBSS SCCC CCCC CCCC CCCC', 'tr': 'TRkk BBBB BRCC CCCC CCCC CCCC CC'
61 }
62
63 def _format_iban(string):
64     '''
65     This function removes all characters from given 'string' that isn't a alpha numeric and converts it to upper case.
66     '''
67     res = ""
68     for char in string:
69         if char.isalnum():
70             res += char.upper()
71     return res
72
73 def _pretty_iban(string):
74     "return string in groups of four characters separated by a single space"
75     res = []
76     while string:
77         res.append(string[:4])
78         string = string[4:]
79     return ' '.join(res)
80
81 class res_partner_bank(osv.osv):
82     _inherit = "res.partner.bank"
83
84     def create(self, cr, uid, vals, context=None):
85         #overwrite to format the iban number correctly
86         if 'iban' in vals and vals['iban']:
87             vals['iban'] = _format_iban(vals['iban'])
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 'iban' in vals and vals['iban']:
93             vals['iban'] = _format_iban(vals['iban'])
94         return super(res_partner_bank, self).write(cr, uid, ids, vals, context)
95
96     def check_iban(self, cr, uid, ids, context=None):
97         '''
98         Check the IBAN number
99         '''
100         for bank_acc in self.browse(cr, uid, ids, context=context):
101             if not bank_acc.iban:
102                 continue
103             iban = _format_iban(bank_acc.iban).lower()
104             if iban[:2] in _iban_len and len(iban) != _iban_len[iban[:2]]:
105                 return False
106             #the four first digits have to be shifted to the end
107             iban = iban[4:] + iban[:4]
108             #letters have to be transformed into numbers (a = 10, b = 11, ...)
109             iban2 = ""
110             for char in iban:
111                 if char.isalpha():
112                     iban2 += str(ord(char)-87)
113                 else:
114                     iban2 += char
115             #iban is correct if modulo 97 == 1
116             if not int(iban2) % 97 == 1:
117                 return False
118         return True
119
120     def _construct_constraint_msg(self, cr, uid, ids, context=None):
121
122         def default_iban_check(iban_cn):
123             return iban_cn[0] in string.ascii_lowercase and iban_cn[1] in string.ascii_lowercase
124
125         iban_country = self.browse(cr, uid, ids)[0].iban[:2]
126         if default_iban_check(iban_country):
127             iban_example = iban_country in _ref_iban and _ref_iban[iban_country] + ' \nWhere A = Account number, B = National bank code, S = Branch code, C = account No, N = branch No, K = National check digits....' or ''
128             return _('The IBAN does not seem to be correct. You should have entered something like this %s'), (iban_example)
129         return _('The IBAN is invalid, It should begin with the country code'), ()
130
131     def name_get(self, cr, uid, ids, context=None):
132         res = []
133         to_check_ids = []
134         for val in self.browse(cr, uid, ids, context=context):
135             if val.state=='iban':
136                 res.append((val.id, _pretty_iban(val.iban or '')))
137             else:
138                 to_check_ids.append(val.id)
139         res += super(res_partner_bank, self).name_get(cr, uid, to_check_ids, context=context)
140         return res
141
142     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
143     #overwrite the search method in order to search not only on bank type == basic account number but also on type == iban
144         res = super(res_partner_bank,self).search(cr, uid, args, offset, limit, order, context=context, count=count)
145         if filter(lambda x:x[0]=='acc_number' ,args):
146             #get the value of the search
147             iban_value = filter(lambda x:x[0]=='acc_number' ,args)[0][2]
148             #get the other arguments of the search
149             args1 =  filter(lambda x:x[0]!='acc_number' ,args)
150             #add the new criterion
151             args1 += [('iban','ilike',iban_value)]
152             #append the results to the older search
153             res += super(res_partner_bank,self).search(cr, uid, args1, offset, limit,
154                 order, context=context, count=count)
155         return res
156
157     def get_bban_from_iban(self, cr, uid, ids, context=None):
158         '''
159         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.
160         '''
161         res = {}
162         mapping_list = {
163          #TODO add rules for others countries
164             'be': lambda x: x[4:],
165             'fr': lambda x: x[14:],
166             'ch': lambda x: x[9:],
167             'gb': lambda x: x[14:],
168         }
169         for record in self.browse(cr, uid, ids, context=context):
170             if not record.iban:
171                 res[record.id] = False
172                 continue
173             res[record.id] = False
174             for code, function in mapping_list.items():
175                 if record.iban.lower().startswith(code):
176                     res[record.id] = function(record.iban)
177                     break
178         return res
179
180     _columns = {
181         'iban': fields.char('IBAN', size=34, readonly=True, help="International Bank Account Number"),
182     }
183
184     _constraints = [(check_iban, _construct_constraint_msg, ["iban"])]
185
186 res_partner_bank()
187
188 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: