[FIX] fix the description problem in invoice report and in notes of customer invoice...
[odoo/odoo.git] / addons / account / account_bank.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 from tools.translate import _
23 from osv import fields, osv
24
25 class bank(osv.osv):
26     _inherit = "res.partner.bank"
27     _columns = {
28         'journal_id': fields.many2one('account.journal', 'Account Journal', help="This journal will be created automatically for this bank account when you save the record"),
29     }
30     def create(self, cr, uid, data, context={}):
31         result = super(bank, self).create(cr, uid, data, context=context)
32         self.post_write(cr, uid, [result], context=context)
33         return result
34
35     def write(self, cr, uid, ids, data, context={}):
36         result = super(bank, self).write(cr, uid, ids, data, context=context)
37         self.post_write(cr, uid, ids, context=context)
38         return result
39
40     def post_write(self, cr, uid, ids, context={}):
41         obj_acc = self.pool.get('account.account')
42         obj_data = self.pool.get('ir.model.data')
43         for bank in self.browse(cr, uid, ids, context):
44             if bank.company_id and not bank.journal_id:
45                 # Find the code and parent of the bank account to create
46                 dig = 6
47                 current_num = 1
48                 ids = obj_acc.search(cr, uid, [('type','=','liquidity')], context=context) 
49                 # No liquidity account exists, no template available
50                 if not ids: continue
51
52                 ref_acc_bank_temp = obj_acc.browse(cr, uid, ids[0], context=context)
53                 ref_acc_bank = ref_acc_bank_temp.parent_id
54                 while True:
55                     new_code = str(ref_acc_bank.code.ljust(dig-len(str(current_num)), '0')) + str(current_num)
56                     ids = obj_acc.search(cr, uid, [('code', '=', new_code), ('company_id', '=', bank.company_id.id)])
57                     if not ids:
58                         break
59                     current_num += 1
60
61                 acc = {
62                     'name': (bank.bank_name or '')+' '+bank.acc_number,
63                     'currency_id': bank.company_id.currency_id.id,
64                     'code': new_code,
65                     'type': 'liquidity',
66                     'user_type': ref_acc_bank_temp.user_type.id,
67                     'reconcile': False,
68                     'parent_id': ref_acc_bank.id,
69                     'company_id': bank.company_id.id,
70                 }
71                 acc_bank_id  = obj_acc.create(cr,uid,acc,context=context)
72
73                 # Get the journal view id
74                 data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_journal_bank_view')])
75                 data = obj_data.browse(cr, uid, data_id[0], context=context)
76                 view_id_cash = data.res_id
77                 
78                 jour_obj = self.pool.get('account.journal')
79                 new_code = 1
80                 while True:
81                     code = _('BNK')+str(new_code)
82                     ids = jour_obj.search(cr, uid, [('code','=',code)], context=context)
83                     if not ids:
84                         break
85                     new_code += 1
86
87                 #create the bank journal
88                 vals_journal = {
89                     'name':  (bank.bank_name or '')+' '+bank.acc_number,
90                     'code': code,
91                     'type': 'bank',
92                     'company_id': bank.company_id.id,
93                     'analytic_journal_id': False,
94                     'currency_id': False,
95                     'default_credit_account_id': acc_bank_id,
96                     'default_debit_account_id': acc_bank_id,
97                     'view_id': view_id_cash
98                 }
99                 journal_id = jour_obj.create(cr, uid, vals_journal, context=context)
100
101                 self.write(cr, uid, [bank.id], {'journal_id': journal_id}, context=context)
102         return True
103