[FIX] hr_expense: select the right employee for an expense created with alias

Current behavior:
When trying to create an expense using alias, if there's several `hr.employee` linked to a user, it select the first one instead of this with the right company

Steps to reproduce the error :
- Create different employee's profiles for a same user
- Put the default one on the user's profile (don't put the first that you created because it will select the first for the expense)
- Try to send an email to the expense's alias and check at the logs

After this commit:
The right employee (this one in the default company) will be selected and no error will be triggered

opw-3754015

closes odoo/odoo#162923

Forward-port-of: odoo/odoo#161853
Signed-off-by: John Laterre (jol) <jol@odoo.com>
Signed-off-by: Cameron Noupoue (cano) <cano@odoo.com>
This commit is contained in:
Cameron
2024-04-24 11:16:03 +00:00
parent 436be43d49
commit ecedbeb7c9
2 changed files with 33 additions and 5 deletions
+6 -5
View File
@@ -921,12 +921,13 @@ class HrExpense(models.Model):
def message_new(self, msg_dict, custom_values=None):
email_address = email_split(msg_dict.get('email_from', False))[0]
employee = self.env['hr.employee'].search(
['|', ('work_email', 'ilike', email_address), ('user_id.email', 'ilike', email_address)],
limit=1,
)
employee = self.env['hr.employee'].search([
'|',
('work_email', 'ilike', email_address),
('user_id.email', 'ilike', email_address)
]).filtered(lambda e: e.company_id == e.user_id.company_id)
if not employee:
if len(employee) != 1:
return super().message_new(msg_dict, custom_values=custom_values)
expense_description = msg_dict.get('subject', '')
@@ -40,6 +40,33 @@ class TestExpensesMailImport(TestExpenseCommon):
{'product_id': self.product_c.id, 'total_amount_currency': 100.0, 'employee_id': self.expense_employee.id},
])
def test_import_expense_from_email_several_employees(self):
"""When a user has several employees' profiles from different companies, the right record should be selected"""
user = self.expense_user_employee
company_2 = user.company_ids[1]
user.company_id = company_2.id
# Create a second employee linked to the user for another company
company_2_employee = self.env['hr.employee'].create({
'name': 'expense_employee_2',
'company_id': company_2.id,
'user_id': user.id,
'work_email': user.email,
})
message_parsed = {
'message_id': "the-world-is-a-ghetto",
'subject': 'New expense',
'email_from': user.email,
'to': 'catchall@yourcompany.com',
'body': "Don't you know, that for me, and for you",
'attachments': [],
}
expense = self.env['hr.expense'].message_new(message_parsed)
self.assertRecordValues(expense, [{
'employee_id': company_2_employee.id,
}])
def test_import_expense_from_mail_parsing_subjects(self):
def assertParsedValues(subject, currencies, exp_description, exp_amount, exp_product, exp_currency):
product, amount, currency_id, description = self.env['hr.expense'] \