Skip to content

Commit

Permalink
[IMP] restrict_lot propagation method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kev-Roche committed Sep 2, 2024
1 parent 044018e commit 997ccc3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
37 changes: 5 additions & 32 deletions stock_restrict_lot/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from odoo import _, api, exceptions, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.misc import OrderedSet


class StockMove(models.Model):
Expand Down Expand Up @@ -90,34 +91,18 @@ def _split(self, qty, restrict_partner_id=False):
vals_list[0]["restrict_lot_id"] = self.restrict_lot_id.id
return vals_list

def get_all_dest_moves(self):
res = self.move_dest_ids
moves_to_search = self.move_dest_ids
while moves_to_search:
new_dest_ids = moves_to_search.move_dest_ids
moves_to_search = new_dest_ids - res
res |= new_dest_ids
return res

def get_all_orig_moves(self):
res = self.move_orig_ids
moves_to_search = self.move_orig_ids
while moves_to_search:
new_orig_ids = moves_to_search.move_orig_ids
moves_to_search = new_orig_ids - res
res |= new_orig_ids
return res

def write(self, vals):
if "restrict_lot_id" not in vals:
return super().write(vals)
else:
restrict_lot_id = vals.pop("restrict_lot_id")
restrict_lot = self.env["stock.lot"].browse(restrict_lot_id)
chained_moves = self | self.get_all_dest_moves() | self.get_all_orig_moves()
chained_moves = OrderedSet()
self._rollup_move_dests(chained_moves)
self._rollup_move_origs(chained_moves)
if any(
[
sm.state == "done" and sm.lot_ids != restrict_lot
sm.state == "done" and sm.lot_ids and sm.lot_ids != restrict_lot
for sm in chained_moves
]
):
Expand All @@ -130,15 +115,3 @@ def write(self, vals):
)
super(StockMove, chained_moves).write({"restrict_lot_id": restrict_lot_id})
return super().write(vals)

@api.model_create_multi
def create(self, vals_list):
res = super().create(vals_list)
for move in res:
if not move.restrict_lot_id:
chained_moves = (
move | move.get_all_dest_moves() | move.get_all_orig_moves()
)
if chained_moves.restrict_lot_id:
move.restrict_lot_id = chained_moves.restrict_lot_id[0]
return res
7 changes: 7 additions & 0 deletions stock_restrict_lot/models/stock_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ def _get_custom_move_fields(self):
fields = super()._get_custom_move_fields()
fields += ["restrict_lot_id"]
return fields

def _push_prepare_move_copy_values(self, move_to_copy, new_date):
values = super()._push_prepare_move_copy_values(move_to_copy, new_date)
values["restrict_lot_id"] = (
move_to_copy.restrict_lot_id.id if move_to_copy.restrict_lot_id else False
)
return values

0 comments on commit 997ccc3

Please sign in to comment.