1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
24
from osv import fields, osv
26
def _format_iban(string):
28
This function removes all characters from given 'string' that isn't a alpha numeric and converts it to lower case.
36
class res_partner_bank(osv.osv):
37
_inherit = "res.partner.bank"
39
def create(self, cr, uid, vals, context={}):
40
#overwrite to format the iban number correctly
41
if 'iban' in vals and vals['iban']:
42
vals['iban'] = _format_iban(vals['iban'])
43
return super(res_partner_bank, self).create(cr, uid, vals, context)
45
def write(self, cr, uid, ids, vals, context={}):
46
#overwrite to format the iban number correctly
47
if 'iban' in vals and vals['iban']:
48
vals['iban'] = _format_iban(vals['iban'])
49
return super(res_partner_bank, self).write(cr, uid, ids, vals, context)
51
def check_iban(self, cr, uid, ids):
55
for bank_acc in self.browse(cr, uid, ids):
58
iban =_format_iban(bank_acc.iban)
59
#the four first digits have to be shifted to the end
60
iban = iban[4:] + iban[:4]
61
#letters have to be transformed into numbers (a = 10, b = 11, ...)
65
iban2 += str(ord(char)-87)
68
#iban is correct if modulo 97 == 1
69
if not int(iban2) % 97 == 1:
73
def name_get(self, cr, uid, ids, context=None):
76
for id in self.browse(cr, uid, ids):
78
res.append((id.id,id.iban))
80
to_check_ids.append(id.id)
81
res += super(res_partner_bank, self).name_get(cr, uid, to_check_ids, context)
84
def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
85
#overwrite the search method in order to search not only on bank type == basic account number but also on type == iban
86
res = super(res_partner_bank,self).search(cr, uid, args, offset, limit, order, context=context, count=count)
87
if filter(lambda x:x[0]=='acc_number' ,args):
88
#get the value of the search
89
iban_value = filter(lambda x:x[0]=='acc_number' ,args)[0][2]
90
#get the other arguments of the search
91
args1 = filter(lambda x:x[0]!='acc_number' ,args)
92
#add the new criterion
93
args1 += [('iban','ilike',iban_value)]
94
#append the results to the older search
95
res += super(res_partner_bank,self).search(cr, uid, args1, offset, limit,
96
order, context=context, count=count)
99
def get_bban_from_iban(self, cr, uid, ids, context=None):
101
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.
105
#TODO add rules for others countries
106
'be': lambda x: x[4:],
107
'fr': lambda x: x[14:],
108
'ch': lambda x: x[9:],
109
'gb': lambda x: x[14:],
111
for record in self.browse(cr, uid, ids, context):
113
res[record.id] = False
115
res[record.id] = False
116
for code, function in mapping_list.items():
117
if record.iban.lower().startswith(code):
118
res[record.id] = function(record.iban)
123
'iban': fields.char('IBAN', size=34, readonly=True, help="International Bank Account Number"),
126
_constraints = [(check_iban, "The IBAN number doesn't seem to be correct.", ["iban"])]
131
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: