From 37cd2a1936f962b544a4ab60bdbe5cc571144dcc Mon Sep 17 00:00:00 2001
From: Victor Feyens <vfe@odoo.com>
Date: Mon, 22 May 2023 15:33:30 +0000
Subject: [PATCH] [FIX] sale: correct ordering of onchange updates

Since e9ad3442021c3b7849f55673f3fef06f1f2104b0, on product change,
the uom & qty were only reset to defaults after the price & tax
recomputation.

This commit brings back the previous (and correct) behavior, by updating
the uom & qty alongside the description, before the taxes & price updates.

closes odoo/odoo#121883

Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
Co-authored-by: Duong Kim <81897633+kimduong986@users.noreply.github.com>
---
 addons/sale/models/sale_order_line.py | 60 +++++++++++++--------------
 1 file changed, 30 insertions(+), 30 deletions(-)

diff --git a/addons/sale/models/sale_order_line.py b/addons/sale/models/sale_order_line.py
index 5b48a734f44e..811e6a37b84a 100644
--- a/addons/sale/models/sale_order_line.py
+++ b/addons/sale/models/sale_order_line.py
@@ -606,6 +606,15 @@ class SaleOrderLine(models.Model):
 
     @api.onchange('product_id')
     def product_id_change(self):
+        if not self.product_id:
+            return
+
+        if not self.product_uom or (self.product_id.uom_id.id != self.product_uom.id):
+            self.update({
+                'product_uom': self.product_id.uom_id,
+                'product_uom_qty': self.product_uom_qty or 1.0
+            })
+
         self._update_description()
         self._update_taxes()
 
@@ -623,6 +632,7 @@ class SaleOrderLine(models.Model):
     def _update_description(self):
         if not self.product_id:
             return
+
         valid_values = self.product_id.product_tmpl_id.valid_product_template_attribute_line_ids.product_template_value_ids
         # remove the is_custom values that don't belong to this template
         for pacv in self.product_custom_attribute_value_ids:
@@ -634,49 +644,39 @@ class SaleOrderLine(models.Model):
             if ptav._origin not in valid_values:
                 self.product_no_variant_attribute_value_ids -= ptav
 
-        vals = {}
-        if not self.product_uom or (self.product_id.uom_id.id != self.product_uom.id):
-            vals['product_uom'] = self.product_id.uom_id
-            vals['product_uom_qty'] = self.product_uom_qty or 1.0
-
         lang = get_lang(self.env, self.order_id.partner_id.lang).code
         product = self.product_id.with_context(
             lang=lang,
         )
-
-        self.update({'name': self.with_context(lang=lang).get_sale_order_line_multiline_description_sale(product)})
+        self.update({
+            'name': self.with_context(lang=lang).get_sale_order_line_multiline_description_sale(product)
+        })
 
     def _update_taxes(self):
         if not self.product_id:
             return
 
-        vals = {}
-        if not self.product_uom or (self.product_id.uom_id.id != self.product_uom.id):
-            vals['product_uom'] = self.product_id.uom_id
-            vals['product_uom_qty'] = self.product_uom_qty or 1.0
-
-        product = self.product_id.with_context(
-            partner=self.order_id.partner_id,
-            quantity=vals.get('product_uom_qty') or self.product_uom_qty,
-            date=self.order_id.date_order,
-            pricelist=self.order_id.pricelist_id.id,
-            uom=self.product_uom.id
-        )
-
         self._compute_tax_id()
 
         if self.order_id.pricelist_id and self.order_id.partner_id:
-            vals['price_unit'] = product._get_tax_included_unit_price(
-                self.company_id,
-                self.order_id.currency_id,
-                self.order_id.date_order,
-                'sale',
-                fiscal_position=self.order_id.fiscal_position_id,
-                product_price_unit=self._get_display_price(product),
-                product_currency=self.order_id.currency_id
+            product = self.product_id.with_context(
+                partner=self.order_id.partner_id,
+                quantity=self.product_uom_qty,
+                date=self.order_id.date_order,
+                pricelist=self.order_id.pricelist_id.id,
+                uom=self.product_uom.id
             )
-
-        self.update(vals)
+            self.update({
+                'price_unit': product._get_tax_included_unit_price(
+                    self.company_id,
+                    self.order_id.currency_id,
+                    self.order_id.date_order,
+                    'sale',
+                    fiscal_position=self.order_id.fiscal_position_id,
+                    product_price_unit=self._get_display_price(product),
+                    product_currency=self.order_id.currency_id
+                )
+            })
 
     @api.onchange('product_uom', 'product_uom_qty')
     def product_uom_change(self):
-- 
GitLab