Skip to content

Commit d6c49a0

Browse files
committed
[FIX] product_catalog_tree: improve order line quantity update logic
closes #858 Signed-off-by: matiasperalta1 <mnp@adhoc.com.ar>
1 parent 2f5bae0 commit d6c49a0

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

product_catalog_tree/models/product_product.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,26 @@ def _inverse_catalog_values(self, product_catalog_qty):
5858

5959
order = self.env[res_model].browse(order_id)
6060
for rec in self:
61-
# Actualizar la información de la línea de orden
62-
# Call the order method with a cleared context to avoid errors on creating move line
63-
order.with_company(order.company_id).with_context(**{})._update_order_line_info(rec.id, product_catalog_qty)
61+
existing_lines = order.order_line.filtered(lambda line: line.product_id.id == rec.id)
62+
if len(existing_lines) > 1 and product_catalog_qty > 0:
63+
total_current_qty = sum(existing_lines.mapped("product_uom_qty"))
64+
qty_difference = product_catalog_qty - total_current_qty
65+
if qty_difference >= 0:
66+
existing_lines[0].product_uom_qty += qty_difference
67+
else:
68+
remaining_to_reduce = abs(qty_difference)
69+
for line in existing_lines:
70+
if remaining_to_reduce <= 0:
71+
break
72+
can_reduce = min(line.product_uom_qty, remaining_to_reduce)
73+
line.product_uom_qty -= can_reduce
74+
remaining_to_reduce -= can_reduce
75+
else:
76+
# Actualizar la información de la línea de orden
77+
# Call the order method with a cleared context to avoid errors on creating move line
78+
order.with_company(order.company_id).with_context(**{})._update_order_line_info(
79+
rec.id, product_catalog_qty
80+
)
6481

6582
def increase_quantity(self):
6683
for rec in self:

0 commit comments

Comments
 (0)