Josse Colpaert (OpenERP) has proposed merging 
lp:~openerp-dev/openobject-addons/trunk-bug-1068038-jco into 
lp:openobject-addons.

Requested reviews:
  qdp (OpenERP) (qdp)
Related bugs:
  Bug #1068038 in OpenERP Addons: "hr_expense: generated invoice may use wrong 
accounts in a multi-company context"
  https://bugs.launchpad.net/openobject-addons/+bug/1068038

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-bug-1068038-jco/+merge/140942

Company read-only on expense and generating accounting entries checks if the 
companies of the user and the expense are equal.  (raises an exception if not)
-- 
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-bug-1068038-jco/+merge/140942
Your team OpenERP R&D Team is subscribed to branch 
lp:~openerp-dev/openobject-addons/trunk-bug-1068038-jco.
=== modified file 'account_check_writing/account_voucher.py'
--- account_check_writing/account_voucher.py	2012-12-17 15:46:28 +0000
+++ account_check_writing/account_voucher.py	2012-12-20 16:34:40 +0000
@@ -19,21 +19,27 @@
 #
 ##############################################################################
 
-from openerp.osv import osv,fields
-from openerp.tools.translate import _
-from openerp.tools.amount_to_text_en import amount_to_text
+from osv import osv,fields
+from tools.translate import _
+from tools.amount_to_text_en import amount_to_text
 from lxml import etree
 
 class account_voucher(osv.osv):
     _inherit = 'account.voucher'
 
-    def _make_journal_search(self, cr, uid, ttype, context=None):
+    def _make_journal_search(self, cr, uid, ttype, company_id = None, context=None):
         if context is None: 
             context = {}
         journal_pool = self.pool.get('account.journal')
         if context.get('write_check',False) :
-            return journal_pool.search(cr, uid, [('allow_check_writing', '=', True)], limit=1)
-        return journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
+            if company_id is None:
+                return journal_pool.search(cr, uid, [('allow_check_writing', '=', True)], limit=1)
+            else:
+                return journal_pool.search(cr, uid, [('allow_check_writing', '=', True), ('company_id', '=', company_id)])
+        if company_id is None:
+            return journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
+        else:
+            return journal_pool.search(cr, uid, [('type', '=', ttype), ('company_id', '=', company_id)], limit=1)
 
     _columns = {
         'amount_in_word' : fields.char("Amount in Word" , size=128, readonly=True, states={'draft':[('readonly',False)]}),
@@ -92,7 +98,7 @@
         nodes = doc.xpath("//field[@name='journal_id']")
         if context.get('write_check', False) :
             for node in nodes:
-                node.set('domain', "[('type', '=', 'bank'), ('allow_check_writing','=',True)]")
+                node.set('domain', "[('type', '=', 'bank'), ('allow_check_writing','=',True), ('company_id', '=', company_id)]")
                 node.set('widget', '')
             res['arch'] = etree.tostring(doc)
         return res

=== modified file 'account_voucher/account_voucher.py'
--- account_voucher/account_voucher.py	2012-12-20 11:47:30 +0000
+++ account_voucher/account_voucher.py	2012-12-20 16:34:40 +0000
@@ -22,10 +22,10 @@
 import time
 from lxml import etree
 
-from openerp import netsvc
-from openerp.osv import fields, osv
-import openerp.addons.decimal_precision as dp
-from openerp.tools.translate import _
+import netsvc
+from osv import osv, fields
+import decimal_precision as dp
+from tools.translate import _
 
 class res_company(osv.osv):
     _inherit = "res.company"
@@ -76,17 +76,23 @@
         periods = self.pool.get('account.period').find(cr, uid)
         return periods and periods[0] or False
 
-    def _make_journal_search(self, cr, uid, ttype, context=None):
+    def _make_journal_search(self, cr, uid, ttype, company_id = None, context=None):
         journal_pool = self.pool.get('account.journal')
-        return journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
+        if company_id is None: 
+            return journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
+        else:
+            return journal_pool.search(cr, uid, [('type', '=', ttype), ('company_id','=', company_id)])
 
     def _get_journal(self, cr, uid, context=None):
         if context is None: context = {}
         invoice_pool = self.pool.get('account.invoice')
         journal_pool = self.pool.get('account.journal')
+        company_id = self._get_company(cr, uid, context=context)
         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)
+            journal_id = journal_pool.search(cr, uid, ['&', ('company_id','=', company_id), '|',
+                                                        ('currency', '=', currency_id), '&', ('currency', '=', False),
+                                                        ('company_id.currency_id', '=', currency_id)], limit=1)
             return journal_id and journal_id[0] or False
         if context.get('journal_id', False):
             return context.get('journal_id')
@@ -96,8 +102,19 @@
         ttype = context.get('type', 'bank')
         if ttype in ('payment', 'receipt'):
             ttype = 'bank'
-        res = self._make_journal_search(cr, uid, ttype, context=context)
-        return res and res[0] or False
+        res = self._make_journal_search(cr, uid, ttype, company_id=company_id, context=context)
+        return res and res[0] or False
+
+    def _get_journal_from_company(self, cr, uid, company_id, context=None):
+        ttype = context.get('type', 'bank')
+        if ttype in ('payment', 'receipt'):
+            ttype = 'bank'
+        if company_id:
+            res = self._make_journal_search(cr, uid, ttype, company_id=company_id, context=context)
+        else:
+            res = False
+        return res and res[0] or False
+
 
     def _get_tax(self, cr, uid, context=None):
         if context is None: context = {}
@@ -161,6 +178,13 @@
             context= {}
         return context.get('amount', 0.0)
 
+    def _get_company(self, cr, uid, context=None):
+        if context is None:
+            context={}
+        res = context.get('invoice_id', False) and self.pool.get('account.invoice').browse(cr, uid, context['invoice_id'], context=context).company_id.id or (
+                        self.pool.get('res.company')._company_default_get(cr, uid, 'account.voucher',context=context))
+        return res
+
     def name_get(self, cr, uid, ids, context=None):
         if not ids:
             return []
@@ -338,6 +362,7 @@
         'active': True,
         'period_id': _get_period,
         'partner_id': _get_partner,
+        'company_id': _get_company,
         'journal_id':_get_journal,
         'currency_id': _get_currency,
         'reference': _get_reference,
@@ -348,7 +373,6 @@
         'pay_now': 'pay_now',
         'name': '',
         'date': lambda *a: time.strftime('%Y-%m-%d'),
-        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.voucher',context=c),
         'tax_id': _get_tax,
         'payment_option': 'without_writeoff',
         'comment': _('Write-Off'),
@@ -503,17 +527,41 @@
         return default
 
     def onchange_rate(self, cr, uid, ids, rate, amount, currency_id, payment_rate_currency_id, company_id, context=None):
-        res =  {'value': {'paid_amount_in_company_currency': amount}}
-        company_currency = self.pool.get('res.company').browse(cr, uid, company_id, context=context).currency_id
-        if rate and amount and currency_id:# and currency_id == payment_rate_currency_id:
-            voucher_rate = self.pool.get('res.currency').browse(cr, uid, currency_id, context).rate
-            if company_currency.id == payment_rate_currency_id:
-                company_rate = rate
-            else:
-                company_rate = self.pool.get('res.company').browse(cr, uid, company_id, context=context).currency_id.rate
-            res['value']['paid_amount_in_company_currency'] = amount / voucher_rate * company_rate
+        if company_id:
+            res =  {'value': {'paid_amount_in_company_currency': amount}}
+            company_currency = self.pool.get('res.company').browse(cr, uid, company_id, context=context).currency_id
+            if rate and amount and currency_id: # and currency_id == payment_rate_currency_id: 
+                voucher_rate = self.pool.get('res.currency').browse(cr, uid, currency_id, context).rate
+                if company_currency.id == payment_rate_currency_id:
+                    company_rate = rate
+                else:
+                    company_rate = self.pool.get('res.company').browse(cr, uid, company_id, context=context).currency_id.rate
+                res['value']['paid_amount_in_company_currency'] = amount / voucher_rate * company_rate
+            return res
+        else:
+            return  {'value': {}}
+
+    def onchange_company_id(self, cr, uid, ids, journal_id, line_ids, tax_id, partner_id, date, amount, ttype, company_id, context=None):
+        if context is None:
+            context ={}
+        if company_id:
+            if (not journal_id) or (not self.pool.get('account.journal').browse(cr, uid, journal_id, context=context).company_id.id == company_id):
+                journal_id = self._get_journal_from_company(cr, uid, company_id, context=context)
+            res = self.onchange_journal(cr, uid, ids, journal_id, line_ids, tax_id, partner_id, date, amount, ttype, company_id, context)
+            if not res: 
+                res = {'value': {}}
+            res['value'].update({'journal_id': journal_id})
+            ctx = context.copy()
+            ctx.update({'company_id': company_id})
+            period_pool = self.pool.get('account.period')
+            pids = period_pool.find(cr, uid, date, context=ctx)
+            if pids and pids[0]:
+                res['value'].update({'period_id': pids[0]})
+        else:
+            res = {'value': {'journal_id': False}}
         return res
 
+
     def onchange_amount(self, cr, uid, ids, amount, rate, partner_id, journal_id, currency_id, ttype, date, payment_rate_currency_id, company_id, context=None):
         if context is None:
             context = {}
@@ -611,7 +659,6 @@
         context_multi_currency = context.copy()
         if date:
             context_multi_currency.update({'date': date})
-
         currency_pool = self.pool.get('res.currency')
         move_line_pool = self.pool.get('account.move.line')
         partner_pool = self.pool.get('res.partner')
@@ -658,7 +705,9 @@
             account_type = 'receivable'
 
         if not context.get('move_line_ids', False):
-            ids = move_line_pool.search(cr, uid, [('state','=','valid'), ('account_id.type', '=', account_type), ('reconcile_id', '=', False), ('partner_id', '=', partner_id)], context=context)
+            ids = move_line_pool.search(cr, uid, [('state','=','valid'), ('account_id.type', '=', account_type), 
+                                                  ('reconcile_id', '=', False), ('partner_id', '=', partner_id), 
+                                                  ('company_id','=', journal.company_id.id)], context=context) 
         else:
             ids = context['move_line_ids']
         invoice_id = context.get('invoice_id', False)

=== modified file 'account_voucher/voucher_payment_receipt_view.xml'
--- account_voucher/voucher_payment_receipt_view.xml	2012-12-08 10:33:38 +0000
+++ account_voucher/voucher_payment_receipt_view.xml	2012-12-20 16:34:40 +0000
@@ -141,9 +141,8 @@
                             <field name="currency_id" invisible="1"/>
                             <field name="amount" invisible="context.get('line_type', False)" on_change="onchange_amount(amount, payment_rate, partner_id, journal_id, currency_id, type, date, payment_rate_currency_id, company_id, context)" class="oe_inline" widget='monetary' options='{"currency_field": "currency_id"}'/>
                             <field name="journal_id"
-                                domain="[('type','in',['bank', 'cash'])]"
+                                domain="[('type','in',['bank', 'cash']), ('company_id','=', company_id)]"
                                 invisible="context.get('line_type', False)"
-                                widget="selection"
                                 on_change="onchange_journal(journal_id, line_dr_ids, False, partner_id, date, amount, type, company_id, context)"
                                 string="Payment Method"/>
                         </group>
@@ -151,7 +150,9 @@
                             <field name="date" invisible="context.get('line_type', False)" on_change="onchange_date(date, currency_id, payment_rate_currency_id, amount, company_id, context)"/>
                             <field name="reference" invisible="context.get('line_type', False)" string="Payment Ref" placeholder="e.g. 003/10"/>
                             <field name="name" colspan="2" invisible="context.get('line_type', False)" 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_id(journal_id, line_dr_ids, False, partner_id, date, amount, type, company_id, context)" 
+                                groups="base.group_multi_company"/>
                         </group>
                     </group>
                     <notebook>
@@ -292,7 +293,7 @@
                     <group>
                         <group>
                             <field name="state" invisible="1"/>
-                            <field name="partner_id" required="1" on_change="onchange_partner_id(partner_id, journal_id, amount, currency_id, type, date, context)" string="Supplier" context="{'search_default_supplier': 1}"/>
+                            <field name="partner_id" string="Customer" required="1" readonly="True"/>
                             <field name="currency_id" invisible="1"/>
                             <field name="amount" class="oe_inline"
                                 string="Paid Amount"
@@ -300,18 +301,17 @@
                                 invisible="context.get('line_type', False)"
                                 on_change="onchange_amount(amount, payment_rate, partner_id, journal_id, currency_id, type, date, payment_rate_currency_id, company_id, context)"/>
                             <field name="journal_id"
-                                domain="[('type','in',['bank', 'cash'])]"
+                                domain="[('type','in',['bank', 'cash']), ('company_id','=',company_id)]"
                                 invisible="context.get('line_type', False)"
-                                widget="selection"
                                 on_change="onchange_journal(journal_id, line_cr_ids, False, partner_id, date, amount, type, company_id, context)"
                                 string="Payment Method"/>
+                            <field name="period_id" invisible="1"/>
                         </group>
                         <group>
                             <field name="date" invisible="context.get('line_type', False)" on_change="onchange_date(date, currency_id, payment_rate_currency_id, amount, company_id, context)"/>
                             <field name="reference" invisible="context.get('line_type', False)" string="Payment Ref" placeholder="e.g. 003/10"/>
                             <field name="name" colspan="2" invisible="context.get('line_type', False)" placeholder="e.g. Invoice SAJ/0042"/>
-                            <field name="company_id" widget="selection" groups="base.group_multi_company"/>
-
+                            <field name="company_id" widget="selection" groups="base.group_multi_company" readonly="True"/>
                             <field name="account_id"
                                 widget="selection"
                                 invisible="True"/>
@@ -410,8 +410,7 @@
                                 widget="monetary" options="{'currency_field': 'currency_id'}"
                                 on_change="onchange_amount(amount, payment_rate, partner_id, journal_id, currency_id, type, date, payment_rate_currency_id, company_id, context)"/>
                             <field name="journal_id"
-                                domain="[('type','in',['bank', 'cash'])]"
-                                widget="selection"
+                                domain="[('type','in',['bank', 'cash']), ('company_id', '=', company_id)]"
                                 on_change="onchange_journal(journal_id, line_cr_ids, False, partner_id, date, amount, type, company_id, context)"
                                 string="Payment Method"/>
                         </group>
@@ -419,7 +418,9 @@
                             <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_id(journal_id, line_cr_ids, False, partner_id, date, amount, type, company_id, context)"
+                                groups="base.group_multi_company"/>
 
                             <field name="account_id"
                                 widget="selection"
@@ -535,7 +536,7 @@
                 Enter the customer and the payment method and then, either
                 create manually a payment record or OpenERP will propose to you
                 automatically the reconciliation of this payment with the open
-                invoices or sales receipts.
+                invoices or sales receipts. 
               </p>
             </field>
         </record>

=== modified file 'hr_expense/hr_expense.py'
--- hr_expense/hr_expense.py	2012-12-20 11:47:30 +0000
+++ hr_expense/hr_expense.py	2012-12-20 16:34:40 +0000
@@ -126,12 +126,10 @@
     def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
         emp_obj = self.pool.get('hr.employee')
         department_id = False
-        company_id = False
         if employee_id:
             employee = emp_obj.browse(cr, uid, employee_id, context=context)
             department_id = employee.department_id.id
-            company_id = employee.company_id.id
-        return {'value': {'department_id': department_id, 'company_id': company_id}}
+        return {'value': {'department_id': department_id}}
 
     def expense_confirm(self, cr, uid, ids, context=None):
         for expense in self.browse(cr, uid, ids):
@@ -157,6 +155,8 @@
             context = {}
         for exp in self.browse(cr, uid, ids, context=context):
             company_id = exp.company_id.id
+            if self.pool.get('res.company')._company_default_get(cr, uid, 'hr_employee', context=context) != company_id:
+                raise osv.except_osv(_('Wrong company'), _('Please make sure that the company you are working in is the same as that of the expense'))
             lines = []
             total = 0.0
             ctx = context.copy()

=== modified file 'hr_expense/hr_expense_view.xml'
--- hr_expense/hr_expense_view.xml	2012-12-06 11:12:08 +0000
+++ hr_expense/hr_expense_view.xml	2012-12-20 16:34:40 +0000
@@ -76,7 +76,7 @@
                             <field name="employee_id" on_change="onchange_employee_id(employee_id)"/>
                             <field name="date"/>
                             <field name="department_id"/>
-                            <field name="company_id" groups="base.group_multi_company"/>
+                            <field name="company_id" readonly="1" groups="base.group_multi_company"/>
                         </group>
                         <group>
                             <field name="name"/>

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : openerp-dev-gtk@lists.launchpad.net
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to