Skip to content

Commit

Permalink
[FIX] stock_dynamic_routing: merge moves
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaudoux committed Sep 1, 2023
1 parent fd917e8 commit ca36f05
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
14 changes: 9 additions & 5 deletions stock_available_to_promise_release/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,10 @@ def _run_stock_rule(self):
)
self.env["procurement.group"].run_defer(procurement_requests)

released_moves._after_release_assign_moves()
released_moves._after_release_update_chain()
assigned_moves = released_moves._after_release_assign_moves()
assigned_moves._after_release_update_chain()

return released_moves
return assigned_moves

def _before_release(self):
"""Hook that aims to be overridden."""
Expand All @@ -486,8 +486,12 @@ def _after_release_update_chain(self):
def _after_release_assign_moves(self):
move_ids = []
for origin_moves in self._get_chained_moves_iterator("move_orig_ids"):
move_ids += origin_moves.ids
self.env["stock.move"].browse(move_ids)._action_assign()
move_ids += origin_moves.filtered(
lambda m: m.state not in ("cancel", "done")
).ids
moves = self.env["stock.move"].browse(move_ids)
moves._action_assign()
return moves

def _release_split(self, remaining_qty):
"""Split move and put remaining_qty to a backorder move."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import stock_move
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2023 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)

from itertools import groupby

from odoo import models


class StockMove(models.Model):
_inherit = "stock.move"

def _after_release_assign_moves(self):
# Trigger the dynamic routing
moves = super()._after_release_assign_moves()
# Check if moves can be merged. We do this after the call to
# _action_assign in super as this could delete some records in self
sorted_moves_by_rule = sorted(moves, key=lambda m: m.picking_id.id)
moves_to_rereserve_ids = []
new_moves = self.browse()
for _picking_id, move_list in groupby(
sorted_moves_by_rule, key=lambda m: m.picking_id.id
):
moves = self.browse(m.id for m in move_list)
merged_moves = moves._merge_moves()
new_moves |= merged_moves
if moves != merged_moves:
for move in merged_moves:
if not move.quantity_done:
moves_to_rereserve_ids.append(move.id)

Check warning on line 29 in stock_available_to_promise_release_dynamic_routing/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

stock_available_to_promise_release_dynamic_routing/models/stock_move.py#L29

Added line #L29 was not covered by tests
if moves_to_rereserve_ids:
moves_to_rereserve = self.browse(moves_to_rereserve_ids)
moves_to_rereserve._do_unreserve()
moves_to_rereserve.with_context(

Check warning on line 33 in stock_available_to_promise_release_dynamic_routing/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

stock_available_to_promise_release_dynamic_routing/models/stock_move.py#L31-L33

Added lines #L31 - L33 were not covered by tests
exclude_apply_dynamic_routing=True
)._action_assign()
return new_moves

0 comments on commit ca36f05

Please sign in to comment.