@@ -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