diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index 17ba2c82c35..c9943c38419 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -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())