[IMP] improve Typo and remove extra space.
[odoo/odoo.git] / addons / l10n_fr_rib / bank.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2011 Numérigraphe SARL.
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 from tools.translate import _
25
26 class res_partner_bank(osv.osv):
27     """Add fields and behavior for French RIB"""
28     _inherit = "res.partner.bank"
29
30     def _check_key(self, cr, uid, ids):
31         """Check the RIB key"""
32         for bank_acc in self.browse(cr, uid, ids):
33             # Ignore the accounts of type other than rib
34             if bank_acc.state !='rib':
35                 continue
36             # Fail if the needed values are empty of too short 
37             if (not bank_acc.bank_code
38             or len(bank_acc.bank_code) != 5
39             or not bank_acc.office or len(bank_acc.office) != 5
40             or not bank_acc.acc_number or len(bank_acc.acc_number) != 11
41             or not bank_acc.key or len(bank_acc.key) != 2):
42                 return False
43             # Get the rib data (without the key)
44             rib = "%s%s%s" % (bank_acc.bank_code, bank_acc.office,
45                               bank_acc.acc_number)
46             # Translate letters into numbers according to a specific table
47             #    (notice how s -> 2)
48             table = dict((ord(a), b) for a, b in zip(
49                 u'abcdefghijklmnopqrstuvwxyz', u'12345678912345678923456789'))
50             rib = rib.lower().translate(table)
51             # compute the key   
52             key = 97 - (100 * int(rib)) % 97
53             if int(bank_acc.key) != key:
54                 return False
55         return True
56
57     def onchange_bank_id(self, cr, uid, ids, bank_id, context=None):
58         """Change the bank code"""
59         result = super(res_partner_bank, self).onchange_bank_id(cr, uid, ids, bank_id,
60                                                         context=context)
61         if bank_id:
62             value = result.setdefault('value', {})
63             bank = self.pool.get('res.bank').browse(cr, uid, bank_id, 
64                                                     context=context)
65             value['bank_code'] = bank.rib_code
66         return result
67
68     _columns = {
69         'bank_code': fields.char('Bank Code', size=64, readonly=True,),
70         'office': fields.char('Office Code', size=5, readonly=True,),
71         'key': fields.char('Key', size=2, readonly=True,
72                            help="The key is a number allowing to check the "
73                                 "correctness of the other codes."),
74     }
75     
76     def _construct_constraint_msg(self, cr, uid, ids, context=None):
77         """Quote the data in the warning message"""
78         # Only process the first id
79         if type(ids) not in (int, long):
80             id = ids[0]
81         rib = self.browse(cr, uid, id, context=context)
82         if rib:
83             return (_("\nThe RIB key %s does not correspond to the other "
84                         "codes: %s %s %s.") %
85                         (rib.key, 
86                         rib.bank_code, 
87                         rib.office,
88                         rib.acc_number) )
89
90     _constraints = [(_check_key,
91                      _construct_constraint_msg,
92                      ["key"])]
93     
94 res_partner_bank()
95
96 class res_bank(osv.osv):
97     """Add the bank code to make it easier to enter RIB data"""
98     _inherit = 'res.bank'
99
100     def name_search(self, cr, user, name, args=None, operator='ilike',
101                     context=None, limit=80):
102         """Search by bank code in addition to the standard search"""
103         # Get the standard results
104         results = super(res_bank, self).name_search(cr, user,
105              name, args=args ,operator=operator, context=context, limit=limit)
106         # Get additional results using the RIB code
107         ids = self.search(cr, user, [('rib_code', operator, name)],
108                               limit=limit, context=context)
109         # Merge the results
110         results = list(set(results + self.name_get(cr, user, ids, context)))
111         return results
112         
113     _columns = {
114         'rib_code': fields.char('RIB Bank Code', size=64),
115     }
116 res_bank()
117 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
118