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