diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index f3bf3bae039..feec9ba4f14 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -1014,10 +1014,12 @@ class StockMove(models.Model): def _action_cancel(self): if any(move.state == 'done' for move in self): raise UserError(_('You cannot cancel a stock move that has been set to \'Done\'.')) - for move in self: - if move.state == 'cancel': - continue - move._do_unreserve() + moves_to_cancel = self.filtered(lambda m: m.state != 'cancel') + # self cannot contain moves that are either cancelled or done, therefore we can safely + # unlink all associated move_line_ids + moves_to_cancel._do_unreserve() + + for move in moves_to_cancel: siblings_states = (move.move_dest_ids.mapped('move_orig_ids') - move).mapped('state') if move.propagate: # only cancel the next move if all my siblings are also cancelled