Skip to content

[16.0][IMP] stock_move_line_change_lot: add hooks before/after moves reservation #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions stock_move_line_change_lot/models/stock_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from collections import defaultdict

from odoo import _, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare
Expand Down Expand Up @@ -66,11 +68,7 @@ def write(self, vals):
if not vals.get("lot_id"):
return super().write(vals)

res, already_processed, to_reassign_moves = self._do_change_lot(vals)
res &= super(StockMoveLine, self - already_processed).write(vals)
if to_reassign_moves:
to_reassign_moves._action_assign()

res, __, ___ = self._do_change_lot(vals)
return res

def _do_change_lot(self, vals):
Expand All @@ -96,6 +94,7 @@ def _do_change_lot(self, vals):
res = True
already_processed = self.browse()
to_reassign_moves = self.env["stock.move"]
moves_by_previous_lot = defaultdict(self.env["stock.move"].browse)
lot = self.env["stock.lot"].browse(vals["lot_id"])
for move_line in self:
if move_line.move_id._should_bypass_reservation(move_line.location_id):
Expand All @@ -114,6 +113,7 @@ def _do_change_lot(self, vals):
package = move_line.package_id.browse(
vals.get("package_id", move_line.package_id.id)
)
moves_by_previous_lot[move_line.lot_id] |= move_line.move_id

available_quantity = 0
# Collect new lot inside or outside a package (strict=False)
Expand Down Expand Up @@ -186,4 +186,29 @@ def _do_change_lot(self, vals):
# recompute the state to be "partially_available"
move_line.move_id._recompute_state()
already_processed |= move_line

still_todo = self - already_processed
if still_todo:
res &= super(StockMoveLine, still_todo).write(vals)
if to_reassign_moves:
self._handle_change_lot_reassign(
lot, to_reassign_moves, moves_by_previous_lot
)
return res, already_processed, to_reassign_moves

def _handle_change_lot_reassign(
self, lot, to_reassign_moves, moves_by_previous_lot
):
for previous_lot, moves in moves_by_previous_lot.items():
moves &= to_reassign_moves
self._hook_change_lot_before_assign(previous_lot, lot, moves)
to_reassign_moves._action_assign()
for previous_lot, moves in moves_by_previous_lot.items():
moves &= to_reassign_moves
self._hook_change_lot_after_assign(previous_lot, lot, moves)

def _hook_change_lot_before_assign(self, previous_lot, lot, moves):
pass

def _hook_change_lot_after_assign(self, previous_lot, lot, moves):
pass