From 2250b3dc0d896f5553d572bb482056075ff9f925 Mon Sep 17 00:00:00 2001
From: Jinjiu Liu <jili@odoo.com>
Date: Mon, 28 Nov 2022 08:04:59 +0000
Subject: [PATCH] [FIX] hr_expense: add extra case for unit_amount compute with
 attachment
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reproduction:
1. Install Expense, make sure in the product list of test data, we have
Expenses(without cost e.g. 0.00) and Daily Allowance(with cost e.g. 100)
2. Create a new expense with the product Daily Allowance, and attach a
file to the attachment, save expense
3. Edit the created expense, change the product to Expenses and save
4. The UI is not changed and can’t edit the total amount of the expense

Reason: the recomputation of product_has_cost is based on the
unit_amount. However, when there’s an attachment, the recomputation of
unit_amount doesn’t consider the case when the product is changed to one
without standard_price (unit_amount).

Fix: add a condition to make sure when the product is changed from one
with standard_price to one without standard_price, the unit_amount of
expense is recomputed correctly

opw-3051726

Related commit: https://github.com/odoo-dev/odoo/commit/918a5f2cc022994ee08bead25dd79072bf3fba87

closes odoo/odoo#106660

Signed-off-by: Kevin Baptiste <kba@odoo.com>
---
 addons/hr_expense/models/hr_expense.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py
index c027ead06bdb..d19a6bcb5d64 100644
--- a/addons/hr_expense/models/hr_expense.py
+++ b/addons/hr_expense/models/hr_expense.py
@@ -244,7 +244,7 @@ class HrExpense(models.Model):
         for expense in self.filtered('product_id'):
             expense = expense.with_company(expense.company_id)
             expense.name = expense.name or expense.product_id.display_name
-            if not expense.attachment_number or (expense.attachment_number and not expense.unit_amount):
+            if not expense.attachment_number or (expense.attachment_number and not expense.unit_amount) or (expense.attachment_number and expense.unit_amount and not expense.product_id.standard_price):
                 expense.unit_amount = expense.product_id.price_compute('standard_price')[expense.product_id.id]
             expense.product_uom_id = expense.product_id.uom_id
             expense.tax_ids = expense.product_id.supplier_taxes_id.filtered(lambda tax: tax.price_include and tax.company_id == expense.company_id)  # taxes only from the same company
-- 
GitLab