[FIX] sale_product_matrix: multiple fixes

1) Do not overwrite lines description if the line was not modified

Fixes #78833

2) Correctly recompute prices on quantity change.

When no new line was added to the SO through the matrix, the prices were
not recomputed, even if the qty was modified for some products.

The prices have to be recomputed in this case since pricelist provide
rules based on minimum qty.

X-original-commit: 4f819438aafead57d6bc79e03f9a7980c6420783
Part-of: odoo/odoo#78971
This commit is contained in:
Victor Feyens
2021-10-26 06:44:19 +00:00
parent bed205a579
commit e9c487dba8
@@ -45,6 +45,7 @@ class SaleOrder(models.Model):
if self.grid and self.grid_update:
grid = json.loads(self.grid)
product_template = self.env['product.template'].browse(grid['product_template_id'])
product_ids = set()
dirty_cells = grid['changes']
Attrib = self.env['product.template.attribute.value']
default_so_line_vals = {}
@@ -64,8 +65,14 @@ class SaleOrder(models.Model):
old_qty = sum(order_lines.mapped('product_uom_qty'))
qty = cell['qty']
diff = qty - old_qty
if not diff:
continue
product_ids.add(product.id)
# TODO keep qty check? cannot be 0 because we only get cell changes ...
if diff and order_lines:
if order_lines:
if qty == 0:
if self.state in ['draft', 'sent']:
# Remove lines if qty was set to 0 in matrix
@@ -95,7 +102,7 @@ class SaleOrder(models.Model):
# if len(order_lines) > 1:
# # Remove 1+ lines
# self.order_line -= order_lines[1:]
elif diff and not order_lines:
else:
if not default_so_line_vals:
OrderLine = self.env['sale.order.line']
default_so_line_vals = OrderLine.default_get(OrderLine._fields.keys())
@@ -108,10 +115,14 @@ class SaleOrder(models.Model):
product_uom_qty=qty,
product_no_variant_attribute_value_ids=no_variant_attribute_values.ids)
))
if new_lines:
if product_ids:
res = False
self.update(dict(order_line=new_lines))
for line in self.order_line.filtered(lambda line: line.product_template_id == product_template):
if new_lines:
# Add new SO lines
self.update(dict(order_line=new_lines))
# Recompute prices for new/modified lines
for line in self.order_line.filtered(lambda line: line.product_id.id in product_ids):
res = line.product_id_change() or res
line._onchange_discount()
line._onchange_product_id_set_customer_lead()