[FIX] hr_expense: display right error if no employee found

Steps to reproduce:

  - Install `Expenses` module
  - Go to Settings > Technical > Email > Aliases
  - Edit the `expense` alias
  - Ensure the model is `Expense`
  - Set `Alias Contact Security` to `Everyone`, then save

  - Send an e-mail to the expense alias, using an e-mail address not
    referenced on one of the employees.

Issue:

  Error in logs related to `currency` variable.

Cause:

  Trying to create an expense for a non-existing user, therefore,
  no currency is set.

Solution:

  If no user is found, no need to create the expense and let the orm
  raise the error that a required field is not set (field employee on
  expense).

opw-3027350

closes odoo/odoo#106057

X-original-commit: a339caa2faf8cde0ec1c85079b9d74f31b055f24
Signed-off-by: Kevin Baptiste <kba@odoo.com>
Signed-off-by: Nasreddin Boulif (bon) <bon@odoo.com>
This commit is contained in:
Nasreddin Boulif (bon)
2022-11-18 16:04:23 +01:00
parent fa9d207e67
commit 123136bed7
+7 -3
View File
@@ -689,6 +689,9 @@ class HrExpense(models.Model):
('user_id.email', 'ilike', email_address)
], limit=1)
if not employee:
return super().message_new(msg_dict, custom_values=custom_values)
expense_description = msg_dict.get('subject', '')
if employee.user_id:
@@ -751,15 +754,16 @@ class HrExpense(models.Model):
symbols_pattern = '|'.join(symbols)
price_pattern = "((%s)?\s?%s\s?(%s)?)" % (symbols_pattern, float_pattern, symbols_pattern)
matches = re.findall(price_pattern, expense_description)
currency = currencies and currencies[0]
if matches:
match = max(matches, key=lambda match: len([group for group in match if group])) # get the longuest match. e.g. "2 chairs 120$" -> the price is 120$, not 2
full_str = match[0]
currency_str = match[1] or match[3]
price = match[2].replace(',', '.')
if currency_str:
currency = currencies.filtered(lambda c: currency_str in [c.symbol, c.name])[0]
currency = currency or currencies[0]
if currency_str and currencies:
currencies = currencies.filtered(lambda c: currency_str in [c.symbol, c.name])
currency = (currencies and currencies[0]) or currency
expense_description = expense_description.replace(full_str, ' ') # remove price from description
expense_description = re.sub(' +', ' ', expense_description.strip())