diff --git a/addons/account_voucher/account_voucher.py b/addons/account_voucher/account_voucher.py index c8be5c0925c..e1483ab0188 100644 --- a/addons/account_voucher/account_voucher.py +++ b/addons/account_voucher/account_voucher.py @@ -69,8 +69,10 @@ class account_voucher(osv.osv): invoice_pool = self.pool.get('account.invoice') journal_pool = self.pool.get('account.journal') if context.get('invoice_id', False): - currency_id = invoice_pool.browse(cr, uid, context['invoice_id'], context=context).currency_id.id - journal_id = journal_pool.search(cr, uid, [('currency', '=', currency_id)], limit=1) + invoice = invoice_pool.browse(cr, uid, context['invoice_id'], context=context) + journal_id = journal_pool.search(cr, uid, [ + ('currency', '=', invoice.currency_id.id), ('company_id', '=', invoice.company_id.id) + ], limit=1, context=context) return journal_id and journal_id[0] or False if context.get('journal_id', False): return context.get('journal_id') @@ -878,7 +880,13 @@ class account_voucher(osv.osv): currency_id = journal.currency.id else: currency_id = journal.company_id.currency_id.id - vals['value'].update({'currency_id': currency_id, 'payment_rate_currency_id': currency_id}) + + period_id = self.pool['account.period'].find(cr, uid, context=dict(context, company_id=company_id)) + vals['value'].update({ + 'currency_id': currency_id, + 'payment_rate_currency_id': currency_id, + 'period_id' : period_id + }) #in case we want to register the payment directly from an invoice, it's confusing to allow to switch the journal #without seeing that the amount is expressed in the journal currency, and not in the invoice currency. So to avoid #this common mistake, we simply reset the amount to 0 if the currency is not the invoice currency. @@ -891,6 +899,18 @@ class account_voucher(osv.osv): vals[key].update(res[key]) return vals + def onchange_company(self, cr, uid, ids, partner_id, journal_id, currency_id, company_id, context=None): + """ + If the company changes, check that the journal is in the right company. + If not, fetch a new journal. + """ + journal_pool = self.pool['account.journal'] + journal = journal_pool.browse(cr, uid, journal_id, context=context) + if journal.company_id.id != company_id: + # can not guess type of journal, better remove it + return {'value': {'journal_id': False}} + return {} + def button_proforma_voucher(self, cr, uid, ids, context=None): self.signal_workflow(cr, uid, ids, 'proforma_voucher') return {'type': 'ir.actions.act_window_close'} diff --git a/addons/account_voucher/account_voucher_view.xml b/addons/account_voucher/account_voucher_view.xml index 2b0508a7542..24c566481d8 100644 --- a/addons/account_voucher/account_voucher_view.xml +++ b/addons/account_voucher/account_voucher_view.xml @@ -55,7 +55,7 @@ - + diff --git a/addons/account_voucher/voucher_payment_receipt_view.xml b/addons/account_voucher/voucher_payment_receipt_view.xml index dd316ea2600..b933de11e6e 100644 --- a/addons/account_voucher/voucher_payment_receipt_view.xml +++ b/addons/account_voucher/voucher_payment_receipt_view.xml @@ -435,7 +435,7 @@ - + - +