From 123136bed78f85db993ae9b8b07126f3f17005a5 Mon Sep 17 00:00:00 2001 From: "Nasreddin Boulif (bon)" Date: Tue, 15 Nov 2022 12:47:47 +0000 Subject: [PATCH] [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 Signed-off-by: Nasreddin Boulif (bon) --- addons/hr_expense/models/hr_expense.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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())