1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from osv import fields, osv
25 def _format_iban(string):
27 This function removes all characters from given 'string' that isn't a alpha numeric and converts it to lower case.
35 class res_partner_bank(osv.osv):
36 _inherit = "res.partner.bank"
38 def create(self, cr, uid, vals, context={}):
39 #overwrite to format the iban number correctly
40 if 'iban' in vals and vals['iban']:
41 vals['iban'] = _format_iban(vals['iban'])
42 return super(res_partner_bank, self).create(cr, uid, vals, context)
44 def write(self, cr, uid, ids, vals, context={}):
45 #overwrite to format the iban number correctly
46 if 'iban' in vals and vals['iban']:
47 vals['iban'] = _format_iban(vals['iban'])
48 return super(res_partner_bank, self).write(cr, uid, ids, vals, context)
50 def check_iban(self, cr, uid, ids):
54 for bank_acc in self.browse(cr, uid, ids):
57 iban = _format_iban(bank_acc.iban)
58 #the four first digits have to be shifted to the end
59 iban = iban[4:] + iban[:4]
60 #letters have to be transformed into numbers (a = 10, b = 11, ...)
64 iban2 += str(ord(char) - 87)
67 #iban is correct if modulo 97 == 1
68 if not int(iban2) % 97 == 1:
72 def name_get(self, cr, uid, ids, context=None):
75 for id in self.browse(cr, uid, ids):
76 if id.state == 'iban':
77 res.append((id.id, id.iban))
79 to_check_ids.append(id.id)
80 res += super(res_partner_bank, self).name_get(cr, uid, to_check_ids, context)
83 def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
84 #overwrite the search method in order to search not only on bank type == basic account number but also on type == iban
85 res = super(res_partner_bank, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
86 if filter(lambda x:x[0] == 'acc_number' , args):
87 #get the value of the search
88 iban_value = filter(lambda x:x[0] == 'acc_number' , args)[0][2]
89 #get the other arguments of the search
90 args1 = filter(lambda x:x[0] != 'acc_number' , args)
91 #add the new criterion
92 args1 += [('iban', 'ilike', iban_value)]
93 #append the results to the older search
94 res += super(res_partner_bank, self).search(cr, uid, args1, offset, limit,
95 order, context=context, count=count)
98 def get_bban_from_iban(self, cr, uid, ids, context=None):
100 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.
104 #TODO add rules for others countries
105 'be': lambda x: x[4:],
106 'fr': lambda x: x[14:],
107 'ch': lambda x: x[9:],
108 'gb': lambda x: x[14:],
110 for record in self.browse(cr, uid, ids, context):
112 res[record.id] = False
114 res[record.id] = False
115 for code, function in mapping_list.items():
116 if record.iban.lower().startswith(code):
117 res[record.id] = function(record.iban)
122 'iban': fields.char('IBAN', size=34, readonly=True, help="International Bank Account Number"),
125 _constraints = [(check_iban, "The IBAN number doesn't seem to be correct.", ["iban"])]
130 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: