[CLEAN] Set Withespaces to PEP8 format
[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
22 import netsvc
23 from osv import fields, osv
24
25 def _format_iban(string):
26     '''
27     This function removes all characters from given 'string' that isn't a alpha numeric and converts it to lower case.
28     '''
29     res = ""
30     for char in string:
31         if char.isalnum():
32             res += char.lower()
33     return res
34
35 class res_partner_bank(osv.osv):
36     _inherit = "res.partner.bank"
37
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)
43
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)
49
50     def check_iban(self, cr, uid, ids):
51         '''
52         Check the IBAN number
53         '''
54         for bank_acc in self.browse(cr, uid, ids):
55             if not bank_acc.iban:
56                 continue
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, ...)
61             iban2 = ""
62             for char in iban:
63                 if char.isalpha():
64                     iban2 += str(ord(char) - 87)
65                 else:
66                     iban2 += char
67             #iban is correct if modulo 97 == 1
68             if not int(iban2) % 97 == 1:
69                 return False
70         return True
71
72     def name_get(self, cr, uid, ids, context=None):
73         res = []
74         to_check_ids = []
75         for id in self.browse(cr, uid, ids):
76             if id.state == 'iban':
77                 res.append((id.id, id.iban))
78             else:
79                 to_check_ids.append(id.id)
80         res += super(res_partner_bank, self).name_get(cr, uid, to_check_ids, context)
81         return res
82
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)
96         return res
97
98     def get_bban_from_iban(self, cr, uid, ids, context=None):
99         '''
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.
101         '''
102         res = {}
103         mapping_list = {
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:],
109         }
110         for record in self.browse(cr, uid, ids, context):
111             if not record.iban:
112                 res[record.id] = False
113                 continue
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)
118                     break
119         return res
120
121     _columns = {
122         'iban': fields.char('IBAN', size=34, readonly=True, help="International Bank Account Number"),
123     }
124
125     _constraints = [(check_iban, "The IBAN number doesn't seem to be correct.", ["iban"])]
126
127 res_partner_bank()
128
129
130 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
131