[IMP] hr_expense: update journal on currency change and improve the code for multicur...
authorPurnendu Singh (OpenERP) <psi@tinyerp.com>
Tue, 14 Aug 2012 11:33:20 +0000 (17:03 +0530)
committerPurnendu Singh (OpenERP) <psi@tinyerp.com>
Tue, 14 Aug 2012 11:33:20 +0000 (17:03 +0530)
bzr revid: psi@tinyerp.com-20120814113320-wf5bnieerjo7yt86

addons/hr_expense/hr_expense.py
addons/hr_expense/hr_expense_view.xml

index 01a054c..c64da1f 100644 (file)
@@ -100,6 +100,13 @@ class hr_expense_expense(osv.osv):
         'currency_id': _get_currency,
     }
 
+    def onchange_currency_id(self, cr, uid, ids, currency_id=False, company_id=False, context=None):
+        res =  {'value': {'journal_id': False}}
+        journal_id = self.pool.get('account.journal').search(cr, uid, [('type','=','purchase'), ('currency','=',currency_id), ('company_id', '=', company_id)], context=context)[0]
+        if journal_id:
+            res['value']['journal_id'] = journal_id
+        return res
+
     def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
         emp_obj = self.pool.get('hr.employee')
         department_id = False
@@ -145,6 +152,13 @@ class hr_expense_expense(osv.osv):
             total = 0.0
             ctx = context.copy()
             ctx.update({'date': exp.date})
+            journal = False
+            if exp.journal_id:
+                journal = exp.journal_id
+            else:
+                journal_id = voucher_obj._get_journal(cr, uid, context={'type': 'purchase', 'company_id': company_id})
+                if journal_id:
+                    journal = account_journal.browse(cr, uid, journal_id, context=context)
             for line in exp.line_ids:
                 if line.product_id:
                     acc = line.product_id.product_tmpl_id.property_account_expense
@@ -155,8 +169,8 @@ class hr_expense_expense(osv.osv):
                     if not acc:
                         raise osv.except_osv(_('Error!'), _('Please configure Default Expense account for Product purchase: `property_account_expense_categ`.'))
                 total_amount = 0.0
-                if exp.company_id.currency_id != exp.currency_id:
-                    total_amount = currency_obj.compute(cr, uid, exp.currency_id.id, exp.company_id.currency_id.id, line.total_amount, context=ctx)
+                if journal.currency and exp.currency_id.id != journal.currency.id:
+                    total_amount = currency_obj.compute(cr, uid, exp.currency_id.id, journal.currency.id, line.total_amount, context=ctx)
                 else:
                     total_amount = line.total_amount
                 lines.append((0, False, {
@@ -179,17 +193,9 @@ class hr_expense_expense(osv.osv):
                 'company_id': company_id,
                 'currency_id': exp.currency_id.id,
                 'line_ids': lines,
-                'amount': total
+                'amount': total,
+                'journal_id': journal.id,
             }
-            journal = False
-            if exp.journal_id:
-                voucher['journal_id'] = exp.journal_id.id
-                journal = exp.journal_id
-            else:
-                journal_id = voucher_obj._get_journal(cr, uid, context={'type': 'purchase', 'company_id': company_id})
-                if journal_id:
-                    voucher['journal_id'] = journal_id
-                    journal = account_journal.browse(cr, uid, journal_id, context=context)
             if journal and not journal.analytic_journal_id:
                 analytic_journal_ids = analytic_journal_obj.search(cr, uid, [('type','=','purchase')], context=context)
                 if analytic_journal_ids:
index 1f9d49e..a9bebeb 100644 (file)
@@ -84,7 +84,7 @@
                         <group>
                             <field name="name"/>
                             <field name="user_valid"/>
-                            <field name="currency_id" groups="base.group_multi_currency"/>
+                            <field name="currency_id" groups="base.group_multi_currency" on_change="onchange_currency_id(currency_id, company_id)"/>
                         </group>
                     </group>
                     <notebook>