From 883c03a06c092c0f8a90ac590b1fe8e40e4f54cb Mon Sep 17 00:00:00 2001
From: alsh-odoo <alsh@odoo.com>
Date: Mon, 11 Sep 2023 15:21:49 +0530
Subject: [PATCH] [FIX] hr_expense: raise exception if multiple company records
 selected
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This traceback arises when the user selects multi
company records and click on the 'create report' button.

To reproduce this issue:

1) Install 'hr_expense'
2) Create a new company for example 'test'
3) Enable the 'test' company on the right corner
4) Now open 'employees' and change the company of the current user(Mitchel Admin)
   to the 'test'.
5) Open 'Expenses', create a new record, and change the company to 'test'.
6) Fill the required field values and save the record.
7) Now click 'My Expenses/My Expenses to report' and select all the records (Make sure
   'My Expenses' filter is applied)
8) Click on the 'Create Record' button

Error:- "ValueError: Expected singleton: res.company(2, 1)"

On the '_create_sheet_from_expenses' method, the value of 'company_id' is getting
through 'self.company_id.id'.
See:-
https://github.com/odoo/odoo/blob/f910987cb4af84c1e7afabf67f05d4eebc31e765/addons/hr_expense/models/hr_expense.py#L324-L335

But when user selects multi company recordsets
'self.company_id' has multi recordsets.

When 'company_id' is getting value through 'self.company_id.id', because of
'self.company_id' has multiple records, which leads
to the above traceback.

By applying this commit will resolve the issue by raising an exception,
when user selected multi company records to create report.

sentry-4465422743

closes odoo/odoo#135035

Signed-off-by: de Wouters de Bouchout Jean-Benoît (jbw) <jbw@odoo.com>
---
 addons/hr_expense/i18n/hr_expense.pot  | 6 ++++++
 addons/hr_expense/models/hr_expense.py | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/addons/hr_expense/i18n/hr_expense.pot b/addons/hr_expense/i18n/hr_expense.pot
index 361716eddeb3..711b3ca0da28 100644
--- a/addons/hr_expense/i18n/hr_expense.pot
+++ b/addons/hr_expense/i18n/hr_expense.pot
@@ -1802,6 +1802,12 @@ msgstr ""
 msgid "You cannot refuse your own expenses"
 msgstr ""
 
+#. module: hr_expense
+#: code:addons/hr_expense/models/hr_expense.py:0
+#, python-format
+msgid "You cannot report expenses for different companies in the same report."
+msgstr ""
+
 #. module: hr_expense
 #: code:addons/hr_expense/models/hr_expense.py:0
 #, python-format
diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py
index 85f50be31efb..78106963dbd8 100644
--- a/addons/hr_expense/models/hr_expense.py
+++ b/addons/hr_expense/models/hr_expense.py
@@ -328,6 +328,8 @@ Or send your receipts at <a href="mailto:%(email)s?subject=Lunch%%20with%%20cust
             raise UserError(_("You cannot report expenses for different employees in the same report."))
         if any(not expense.product_id for expense in self):
             raise UserError(_("You can not create report without product."))
+        if len(self.company_id) != 1:
+            raise UserError(_("You cannot report expenses for different companies in the same report."))
 
         todo = self.filtered(lambda x: x.payment_mode=='own_account') or self.filtered(lambda x: x.payment_mode=='company_account')
         sheet = self.env['hr.expense.sheet'].create({
-- 
GitLab