diff --git a/stock_available_to_promise_release/models/stock_move.py b/stock_available_to_promise_release/models/stock_move.py index 7cc514c538d..0aa7980602b 100644 --- a/stock_available_to_promise_release/models/stock_move.py +++ b/stock_available_to_promise_release/models/stock_move.py @@ -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.""" @@ -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.""" diff --git a/stock_available_to_promise_release_dynamic_routing/__init__.py b/stock_available_to_promise_release_dynamic_routing/__init__.py index e69de29bb2d..0650744f6bc 100644 --- a/stock_available_to_promise_release_dynamic_routing/__init__.py +++ b/stock_available_to_promise_release_dynamic_routing/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_available_to_promise_release_dynamic_routing/models/__init__.py b/stock_available_to_promise_release_dynamic_routing/models/__init__.py new file mode 100644 index 00000000000..6bda2d2428e --- /dev/null +++ b/stock_available_to_promise_release_dynamic_routing/models/__init__.py @@ -0,0 +1 @@ +from . import stock_move diff --git a/stock_available_to_promise_release_dynamic_routing/models/stock_move.py b/stock_available_to_promise_release_dynamic_routing/models/stock_move.py new file mode 100644 index 00000000000..f34887d52db --- /dev/null +++ b/stock_available_to_promise_release_dynamic_routing/models/stock_move.py @@ -0,0 +1,36 @@ +# Copyright 2023 Jacques-Etienne Baudoux (BCIM) +# 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) + if moves_to_rereserve_ids: + moves_to_rereserve = self.browse(moves_to_rereserve_ids) + moves_to_rereserve._do_unreserve() + moves_to_rereserve.with_context( + exclude_apply_dynamic_routing=True + )._action_assign() + return new_moves