[MERGE] forward port of branch 8.0 up to 92183e5
[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 from openerp.osv import fields, osv
23 from openerp.tools.translate import _
24
25 class res_partner_bank(osv.osv):
26     """Add fields and behavior for French RIB"""
27     _inherit = "res.partner.bank"
28
29     def _check_key(self, cr, uid, ids):
30         """Check the RIB key"""
31         for bank_acc in self.browse(cr, uid, ids):
32             # Ignore the accounts of type other than rib
33             if bank_acc.state != 'rib':
34                 continue
35             # Fail if the needed values are empty of too short 
36             if (not bank_acc.bank_code
37             or len(bank_acc.bank_code) != 5
38             or not bank_acc.office or len(bank_acc.office) != 5
39             or not bank_acc.rib_acc_number or len(bank_acc.rib_acc_number) != 11
40             or not bank_acc.key or len(bank_acc.key) != 2):
41                 return False
42             # Get the rib data (without the key)
43             rib = "%s%s%s" % (bank_acc.bank_code, bank_acc.office, bank_acc.rib_acc_number)
44             # Translate letters into numbers according to a specific table
45             #    (notice how s -> 2)
46             table = dict((ord(a), b) for a, b in zip(
47                 u'abcdefghijklmnopqrstuvwxyz', u'12345678912345678923456789'))
48             rib = rib.lower().translate(table)
49             # compute the key   
50             key = 97 - (100 * int(rib)) % 97
51             if int(bank_acc.key) != key:
52                 raise osv.except_osv(_('Error!'),
53                     _("The RIB key %s does not correspond to the other codes: %s %s %s.") % \
54                         (bank_acc.key, bank_acc.bank_code, bank_acc.office, bank_acc.rib_acc_number) )
55             if bank_acc.acc_number:
56                 if not self.is_iban_valid(cr, uid, bank_acc.acc_number):
57                     raise osv.except_osv(_('Error!'), _("The IBAN %s is not valid.") % bank_acc.acc_number)
58         return True
59
60     def onchange_bank_id(self, cr, uid, ids, bank_id, context=None):
61         """Change the bank code"""
62         result = super(res_partner_bank, self).onchange_bank_id(cr, uid, ids, bank_id,
63                                                         context=context)
64         if bank_id:
65             value = result.setdefault('value', {})
66             bank = self.pool.get('res.bank').browse(cr, uid, bank_id, 
67                                                     context=context)
68             value['bank_code'] = bank.rib_code
69         return result
70
71     _columns = {
72         'acc_number': fields.char('Account Number', size=64, required=False),
73         'rib_acc_number': fields.char('RIB account number', size=11, readonly=True,),
74         'bank_code': fields.char('Bank Code', size=64, readonly=True,),
75         'office': fields.char('Office Code', size=5, readonly=True,),
76         'key': fields.char('Key', size=2, readonly=True,
77                            help="The key is a number allowing to check the "
78                                 "correctness of the other codes."),
79     }
80
81     _constraints = [(_check_key, 'The RIB and/or IBAN is not valid', ['rib_acc_number', 'bank_code', 'office', 'key'])]
82
83
84 class res_bank(osv.osv):
85     """Add the bank code to make it easier to enter RIB data"""
86     _inherit = 'res.bank'
87
88     def name_search(self, cr, user, name, args=None, operator='ilike',
89                     context=None, limit=80):
90         """Search by bank code in addition to the standard search"""
91         # Get the standard results
92         results = super(res_bank, self).name_search(cr, user,
93              name, args=args ,operator=operator, context=context, limit=limit)
94         # Get additional results using the RIB code
95         ids = self.search(cr, user, [('rib_code', operator, name)],
96                               limit=limit, context=context)
97         # Merge the results
98         results = list(set(results + self.name_get(cr, user, ids, context)))
99         return results
100         
101     _columns = {
102         'rib_code': fields.char('RIB Bank Code'),
103     }
104 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
105