Skip to content

Commit

Permalink
[FIX] hr_expense: expenses created by email
Browse files Browse the repository at this point in the history
Current behavior:
When trying to create an expense by email 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 #162898

X-original-commit: b8a5175
Signed-off-by: John Laterre (jol) <jol@odoo.com>
  • Loading branch information
n-kmron committed Apr 23, 2024
1 parent afbed43 commit b4c8625
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions addons/hr_expense/models/hr_expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,9 @@ def message_new(self, msg_dict, custom_values=None):
'|',
('work_email', 'ilike', email_address),
('user_id.email', 'ilike', email_address)
], limit=1)
]).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', '')
Expand Down
27 changes: 27 additions & 0 deletions addons/hr_expense/tests/test_expenses_mail_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ def test_import_expense_from_email(self):
'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_email_no_product(self):
message_parsed = {
'message_id': "the-world-is-a-ghetto",
Expand Down

0 comments on commit b4c8625

Please sign in to comment.