[MRG] merge lp:~openerp-dev/openobject-addons/trunk-job-positions-improve-ssh
[odoo/odoo.git] / addons / l10n_be_coda / l10n_be_coda.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #
6 #    Copyright (c) 2011 Noviat nv/sa (www.noviat.be). All rights reserved.
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (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 Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22 from openerp.osv import osv, fields
23
24 class account_bank_statement(osv.osv):
25     _inherit = 'account.bank.statement'
26     _columns = {
27         'coda_note': fields.text('CODA Notes'),
28     }
29
30
31 class account_bank_statement_line(osv.osv):
32     _inherit = 'account.bank.statement.line'
33     _columns = {
34         'coda_account_number': fields.char('Account Number', help="The Counter Party Account Number")
35     }
36
37     def create(self, cr, uid, data, context=None):
38         """
39             This function creates a Bank Account Number if, for a bank statement line,
40             the partner_id field and the coda_account_number field are set,
41             and the account number does not exist in the database
42         """
43         if 'partner_id' in data and data['partner_id'] and 'coda_account_number' in data and data['coda_account_number']:
44             acc_number_ids = self.pool.get('res.partner.bank').search(cr, uid, [('acc_number', '=', data['coda_account_number'])])
45             if len(acc_number_ids) == 0:
46                 try:
47                     type_model, type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'bank_normal')
48                     type_id = self.pool.get('res.partner.bank.type').browse(cr, uid, type_id, context=context)
49                     self.pool.get('res.partner.bank').create(cr, uid, {'acc_number': data['coda_account_number'], 'partner_id': data['partner_id'], 'state': type_id.code}, context=context)
50                 except ValueError:
51                     pass
52         return super(account_bank_statement_line, self).create(cr, uid, data, context=context)
53
54     def write(self, cr, uid, ids, vals, context=None):
55         super(account_bank_statement_line, self).write(cr, uid, ids, vals, context)
56         """
57             Same as create function above, but for write function
58         """
59         if 'partner_id' in vals:
60             for line in self.pool.get('account.bank.statement.line').browse(cr, uid, ids, context=context):
61                 if line.coda_account_number:
62                     acc_number_ids = self.pool.get('res.partner.bank').search(cr, uid, [('acc_number', '=', line.coda_account_number)])
63                     if len(acc_number_ids) == 0:
64                         try:
65                             type_model, type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'bank_normal')
66                             type_id = self.pool.get('res.partner.bank.type').browse(cr, uid, type_id, context=context)
67                             self.pool.get('res.partner.bank').create(cr, uid, {'acc_number': line.coda_account_number, 'partner_id': vals['partner_id'], 'state': type_id.code}, context=context)
68                         except ValueError:
69                             pass
70         return True
71
72
73 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: