[FIX] account_voucher: multicompany compatibility

When the company or the journal is changed, change the period accordingly to make sure to always keep coherant values.
This commit is contained in:
Torvald B. Bringsvor 2014-11-05 23:55:50 +01:00 committed by Martin Trigaux
parent b7c912ec68
commit 7e73a10712
4 changed files with 26 additions and 6 deletions

View File

@ -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'}

View File

@ -55,7 +55,7 @@
<field name="journal_id" widget="selection" on_change="onchange_journal_voucher(line_ids, tax_id, amount, partner_id, journal_id, type)"/>
<field name="type" required="1"/>
<field name="name" colspan="2"/>
<field name="company_id" widget="selection" groups="base.group_multi_company"/>
<field name="company_id" widget="selection" on_change="onchange_company(partner_id, journal_id, currency_id, company_id)" groups="base.group_multi_company"/>
<field name="reference"/>
<field name="number"/>
<field name="currency_id" groups="base.group_multi_currency"/>

View File

@ -435,7 +435,7 @@
<field name="date" on_change="onchange_date(date, currency_id, payment_rate_currency_id, amount, company_id, context)"/>
<field name="reference" string="Payment Ref" placeholder="e.g. 003/10"/>
<field name="name" colspan="2" placeholder="e.g. Invoice SAJ/0042"/>
<field name="company_id" widget="selection" groups="base.group_multi_company"/>
<field name="company_id" widget="selection" on_change="onchange_company(partner_id, journal_id, currency_id, company_id)" groups="base.group_multi_company"/>
<field name="account_id"
widget="selection"

View File

@ -73,7 +73,7 @@
<group>
<group>
<field name="type" invisible="True"/>
<field name="company_id" widget="selection" groups="base.group_multi_company"/>
<field name="company_id" widget="selection" on_change="onchange_company(partner_id, journal_id, currency_id, company_id)" groups="base.group_multi_company"/>
<field name="partner_id" domain="[('customer','=',True)]" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)" string="Customer" context="{'search_default_customer':1, 'show_address': 1}" options='{"always_reload": True}'/>
<field name="account_id"
domain="[('type','in', ['liquidity', 'receivable'])]"/>