[FIX] account_voucher: multicompany compatibility
authorTorvald B. Bringsvor <torvald@bringsvor.com>
Wed, 5 Nov 2014 22:55:50 +0000 (23:55 +0100)
committerMartin Trigaux <mat@odoo.com>
Thu, 6 Nov 2014 14:24:04 +0000 (15:24 +0100)
When the company or the journal is changed, change the period accordingly to make sure to always keep coherant values.

addons/account_voucher/account_voucher.py
addons/account_voucher/account_voucher_view.xml
addons/account_voucher/voucher_payment_receipt_view.xml
addons/account_voucher/voucher_sales_purchase_view.xml

index c8be5c0..e1483ab 100644 (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'}
index 2b0508a..24c5664 100644 (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"/>
index dd316ea..b933de1 100644 (file)
                             <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"
index 3686cf2..399fc98 100644 (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'])]"/>