*added an integrity check of the iban account number
[odoo/odoo.git] / addons / base_iban / base_iban.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
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
13 # Service Company
14 #
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.
19 #
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.
24 #
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.
28 #
29 ##############################################################################
30
31 import netsvc
32 from osv import fields, osv
33
34 def _format_iban(string):
35     '''
36     This function removes all characters from given 'string' that isn't a alpha numeric and converts it to lower case.
37     '''
38     res = ""
39     for char in string:
40         if char.isalnum():
41             res += char.lower()
42     return res
43
44 class res_partner_bank(osv.osv):
45     _inherit = "res.partner.bank"
46
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)
52
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)
58
59     def check_iban(self, cr, uid, ids):
60         '''
61         Check the IBAN number
62         '''
63         for bank_acc in self.browse(cr, uid, ids):
64             if not bank_acc.iban:
65                 continue
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, ...)
70             iban2 = ""
71             for char in iban:
72                 if char.isalpha():
73                     iban2 += str(ord(char)-87)
74                 else:
75                     iban2 += char
76             #iban is correct if modulo 97 == 1
77             if not int(iban2) % 97 == 1:
78                 return False
79         return True
80
81     def name_get(self, cr, uid, ids, context=None):
82         res = []
83         to_check_ids = []
84         for id in self.browse(cr, uid, ids):
85             if id.state=='iban':
86                 res.append((id.id,id.iban))
87             else:
88                 to_check_ids.append(id.id)
89         res += super(res_partner_bank, self).name_get(cr, uid, to_check_ids, context)
90         return res
91
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)
105         return res
106
107     def get_bban_from_iban(self, cr, uid, ids, context=None):
108         '''
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.
110         '''
111         res = {}
112         mapping_list = {
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:],
118         }
119         for record in self.browse(cr, uid, ids, context):
120             if not record.iban:
121                 res[record.id] = False
122                 continue
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)
127                     break
128         return res
129
130     _columns = {
131         'iban': fields.char('IBAN', size=34, readonly=True, help="International Bank Account Number"),
132     }
133
134     _constraints = [(check_iban, "The IBAN number doesn't seem to be correct.", ["iban"])]
135
136 res_partner_bank()
137
138
139 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
140