[FIX] account: several fixes on the new bank statement reconciliation widget
[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 openerp.tools.translate import _
23 from openerp.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         'currency_id': fields.related('journal_id', 'currency', type="many2one", relation='res.currency', readonly=True,
30             string="Currency", help="Currency of the related account journal."),
31     }
32     def create(self, cr, uid, data, context=None):
33         result = super(bank, self).create(cr, uid, data, context=context)
34         self.post_write(cr, uid, [result], context=context)
35         return result
36
37     def write(self, cr, uid, ids, data, context=None):
38         result = super(bank, self).write(cr, uid, ids, data, context=context)
39         self.post_write(cr, uid, ids, context=context)
40         return result
41
42     def _prepare_name(self, bank):
43         "Return the name to use when creating a bank journal"
44         return (bank.bank_name or '') + ' ' + (bank.acc_number or '')
45
46     def _prepare_name_get(self, cr, uid, bank_dicts, context=None):
47         """Add ability to have %(currency_name)s in the format_layout of res.partner.bank.type"""
48         currency_ids = list(set(data['currency_id'][0] for data in bank_dicts if data.get('currency_id')))
49         currencies = self.pool.get('res.currency').browse(cr, uid, currency_ids, context=context)
50         currency_name = dict((currency.id, currency.name) for currency in currencies)
51
52         for data in bank_dicts:
53             data['currency_name'] = data.get('currency_id') and currency_name[data['currency_id'][0]] or ''
54         return super(bank, self)._prepare_name_get(cr, uid, bank_dicts, context=context)
55
56     def post_write(self, cr, uid, ids, context=None):
57         if isinstance(ids, (int, long)):
58           ids = [ids]
59
60         obj_acc = self.pool.get('account.account')
61         obj_data = self.pool.get('ir.model.data')
62
63         for bank in self.browse(cr, uid, ids, context):
64             if bank.company_id and not bank.journal_id:
65                 # Find the code and parent of the bank account to create
66                 dig = 6
67                 current_num = 1
68                 ids = obj_acc.search(cr, uid, [('type','=','liquidity'), ('company_id', '=', bank.company_id.id), ('parent_id', '!=', False)], context=context)
69                 # No liquidity account exists, no template available
70                 if not ids: continue
71
72                 ref_acc_bank = obj_acc.browse(cr, uid, ids[0], context=context).parent_id
73                 while True:
74                     new_code = str(ref_acc_bank.code.ljust(dig-len(str(current_num)), '0')) + str(current_num)
75                     ids = obj_acc.search(cr, uid, [('code', '=', new_code), ('company_id', '=', bank.company_id.id)])
76                     if not ids:
77                         break
78                     current_num += 1
79                 name = self._prepare_name(bank)
80                 acc = {
81                     'name': name,
82                     'code': new_code,
83                     'type': 'liquidity',
84                     'user_type': ref_acc_bank.user_type.id,
85                     'reconcile': False,
86                     'parent_id': ref_acc_bank.id,
87                     'company_id': bank.company_id.id,
88                 }
89                 acc_bank_id  = obj_acc.create(cr,uid,acc,context=context)
90
91                 jour_obj = self.pool.get('account.journal')
92                 new_code = 1
93                 while True:
94                     code = _('BNK')+str(new_code)
95                     ids = jour_obj.search(cr, uid, [('code','=',code)], context=context)
96                     if not ids:
97                         break
98                     new_code += 1
99
100                 #create the bank journal
101                 vals_journal = {
102                     'name': name,
103                     'code': code,
104                     'type': 'bank',
105                     'company_id': bank.company_id.id,
106                     'analytic_journal_id': False,
107                     'default_credit_account_id': acc_bank_id,
108                     'default_debit_account_id': acc_bank_id,
109                 }
110                 journal_id = jour_obj.create(cr, uid, vals_journal, context=context)
111
112                 self.write(cr, uid, [bank.id], {'journal_id': journal_id}, context=context)
113         return True
114
115 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: