1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ##############################################################################
32 from osv import fields, osv
34 def _format_iban(string):
36 This function removes all characters from given 'string' that isn't a alpha numeric and converts it to lower case.
44 class res_partner_bank(osv.osv):
45 _inherit = "res.partner.bank"
47 def create(self, cr, uid, vals, context={}):
48 #overwrite to format the iban number correctly
49 if vals.has_key('iban'):
50 vals['iban'] = _format_iban(vals['iban'])
51 return super(res_partner_bank, self).create(cr, uid, vals, context)
53 def write(self, cr, uid, ids, vals, context={}):
54 #overwrite to format the iban number correctly
55 if vals.has_key('iban'):
56 vals['iban'] = _format_iban(vals['iban'])
57 return super(res_partner_bank, self).write(cr, uid, ids, vals, context)
59 def check_iban(self, cr, uid, ids):
63 for bank_acc in self.browse(cr, uid, ids):
66 iban =_format_iban(bank_acc.iban)
67 #the four first digits have to be shifted to the end
68 iban = iban[4:] + iban[:4]
69 #letters have to be transformed into numbers (a = 10, b = 11, ...)
73 iban2 += str(ord(char)-87)
76 #iban is correct if modulo 97 == 1
77 if not int(iban2) % 97 == 1:
81 def name_get(self, cr, uid, ids, context=None):
84 for id in self.browse(cr, uid, ids):
86 res.append((id.id,id.iban))
88 to_check_ids.append(id.id)
89 res += super(res_partner_bank, self).name_get(cr, uid, to_check_ids, context)
92 def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
93 #overwrite the search method in order to search not only on bank type == basic account number but also on type == iban
94 res = super(res_partner_bank,self).search(cr, uid, args, offset, limit, order, context=context, count=count)
95 if filter(lambda x:x[0]=='acc_number' ,args):
96 #get the value of the search
97 iban_value = filter(lambda x:x[0]=='acc_number' ,args)[0][2]
98 #get the other arguments of the search
99 args1 = filter(lambda x:x[0]!='acc_number' ,args)
100 #add the new criterion
101 args1 += [('iban','ilike',iban_value)]
102 #append the results to the older search
103 res += super(res_partner_bank,self).search(cr, uid, args1, offset, limit,
104 order, context=context, count=count)
107 def get_bban_from_iban(self, cr, uid, ids, context=None):
109 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.
113 #TODO add rules for others countries
114 'be': lambda x: x[4:],
115 'fr': lambda x: x[14:],
116 'ch': lambda x: x[9:],
117 'gb': lambda x: x[14:],
119 for record in self.browse(cr, uid, ids, context):
121 res[record.id] = False
123 res[record.id] = False
124 for code, function in mapping_list.items():
125 if record.iban.lower().startswith(code):
126 res[record.id] = function(record.iban)
131 'iban': fields.char('IBAN', size=34, readonly=True, help="International Bank Account Number"),
134 _constraints = [(check_iban, "The IBAN number doesn't seem to be correct.", ["iban"])]
139 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: