From 8ccacf58aff5e5bf3f64abe5fbdbbe4c214b86f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20=28avd=29?= Date: Thu, 1 Jul 2021 10:16:26 +0000 Subject: [PATCH] [FIX] stock: improve move_line._free_reservation performances Move candidate.unlink out of the for loop to unlink records in batch. Remove to_unlink_candidates.move_id from move_to_recompute as move_line.unlink() will trigger recompute of their move's state. --- opw-2585598 closes odoo/odoo#73160 X-original-commit: e2e51c15d082ec17743b73ad1422e4a223268be9 Signed-off-by: William Henrotin --- addons/stock/models/stock_move_line.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/addons/stock/models/stock_move_line.py b/addons/stock/models/stock_move_line.py index 0ac5377b6b2..ab4dba26bc1 100644 --- a/addons/stock/models/stock_move_line.py +++ b/addons/stock/models/stock_move_line.py @@ -668,16 +668,17 @@ class StockMoveLine(models.Model): # As the move's state is not computed over the move lines, we'll have to manually # recompute the moves which we adapted their lines. move_to_recompute_state = self.env['stock.move'] + to_unlink_candidates = set() rounding = self.product_uom_id.rounding for candidate in outdated_candidates: if float_compare(candidate.product_qty, quantity, precision_rounding=rounding) <= 0: quantity -= candidate.product_qty - move_to_recompute_state |= candidate.move_id if candidate.qty_done: + move_to_recompute_state |= candidate.move_id candidate.product_uom_qty = 0.0 else: - candidate.unlink() + to_unlink_candidates.add(candidate.id) if float_is_zero(quantity, precision_rounding=rounding): break else: @@ -689,6 +690,7 @@ class StockMoveLine(models.Model): candidate.product_uom_qty = self.product_id.uom_id._compute_quantity(quantity_split, candidate.product_uom_id, rounding_method='HALF-UP') move_to_recompute_state |= candidate.move_id break + self.env['stock.move.line'].browse(to_unlink_candidates).unlink() move_to_recompute_state._recompute_state() def _should_bypass_reservation(self, location):