[FIX] stock: unlink move_lines in batch

Before this patch, cancelling a PO of 2000 lines would take a **very,
very** long time to process... so long that I terminated it before it
finished... yikes.

This was due to the call to `_do_unreserve` on every single move when
calling `_action_cancel`, this meant that n² calls to unlink where being
made (at least), which also meant n² calls to
`stock.move._recompute_state` were being made... that's a lot of
recomputes... = bad performance

The patch simply calls unlink once, which means `_recompute_state` is
called only once, saving a lot of time.

Cancel time for a PO containing 2000 lines:
        - Before patch: 1h30min+ (process terminated before finishing)
        - After patch: 7min

Other modules making use of stock and `_action_cancel` should also see
benefits from this patch

closes odoo/odoo#31787

Signed-off-by: Arnold Moyaux <amoyaux@users.noreply.github.com>
This commit is contained in:
Adrian Torres
2019-03-12 15:35:19 +00:00
committed by Arnold Moyaux
parent 859586b641
commit 073b7bb944
+6 -4
View File
@@ -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