1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from tools.translate import _
23 from osv import fields, osv
26 _inherit = "res.partner.bank"
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."),
32 def create(self, cr, uid, data, context={}):
33 result = super(bank, self).create(cr, uid, data, context=context)
34 self.post_write(cr, uid, [result], context=context)
37 def write(self, cr, uid, ids, data, context={}):
38 result = super(bank, self).write(cr, uid, ids, data, context=context)
39 self.post_write(cr, uid, ids, context=context)
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
46 def _prepare_name_get(self, cr, uid, bank_type_obj, bank_obj, context=None):
47 """Add ability to have %(currency_name)s in the format_layout of
48 res.partner.bank.type"""
49 bank_obj._data[bank_obj.id]['currency_name'] = bank_obj.currency_id and bank_obj.currency_id.name or ''
50 return super(bank, self)._prepare_name_get(cr, uid, bank_type_obj, bank_obj, context=context)
52 def post_write(self, cr, uid, ids, context={}):
53 if isinstance(ids, (int, long)):
56 obj_acc = self.pool.get('account.account')
57 obj_data = self.pool.get('ir.model.data')
59 for bank in self.browse(cr, uid, ids, context):
60 if bank.company_id and not bank.journal_id:
61 # Find the code and parent of the bank account to create
64 ids = obj_acc.search(cr, uid, [('type','=','liquidity'), ('company_id', '=', bank.company_id.id)], context=context)
65 # No liquidity account exists, no template available
68 ref_acc_bank_temp = obj_acc.browse(cr, uid, ids[0], context=context)
69 ref_acc_bank = ref_acc_bank_temp.parent_id
71 new_code = str(ref_acc_bank.code.ljust(dig-len(str(current_num)), '0')) + str(current_num)
72 ids = obj_acc.search(cr, uid, [('code', '=', new_code), ('company_id', '=', bank.company_id.id)])
76 name = self._prepare_name(bank)
81 'user_type': ref_acc_bank_temp.user_type.id,
83 'parent_id': ref_acc_bank.id,
84 'company_id': bank.company_id.id,
86 acc_bank_id = obj_acc.create(cr,uid,acc,context=context)
88 # Get the journal view id
89 data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_journal_bank_view')])
90 data = obj_data.browse(cr, uid, data_id[0], context=context)
91 view_id_cash = data.res_id
93 jour_obj = self.pool.get('account.journal')
96 code = _('BNK')+str(new_code)
97 ids = jour_obj.search(cr, uid, [('code','=',code)], context=context)
102 #create the bank journal
107 'company_id': bank.company_id.id,
108 'analytic_journal_id': False,
109 'default_credit_account_id': acc_bank_id,
110 'default_debit_account_id': acc_bank_id,
111 'view_id': view_id_cash
113 journal_id = jour_obj.create(cr, uid, vals_journal, context=context)
115 self.write(cr, uid, [bank.id], {'journal_id': journal_id}, context=context)
118 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: