Skip to content

Commit

Permalink
[IMP] stock_inventory_lockdown: refactor in order to block movement i…
Browse files Browse the repository at this point in the history
…n stock.move.line confirmation
  • Loading branch information
JoanSForgeFlow authored and ArnauCForgeFlow committed Dec 9, 2024
1 parent afbc8e1 commit 9462014
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
2 changes: 1 addition & 1 deletion stock_inventory_lockdown/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import stock_move
from . import stock_move_line
from . import stock_inventory
from . import stock_location
2 changes: 0 additions & 2 deletions stock_inventory_lockdown/models/stock_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class StockInventory(models.Model):

@api.model
def _get_locations_open_inventories(self, locations_ids=None):
"""IDs of locations in open exhaustive inventories, with children"""
inventory_domain = [("state", "=", "in_progress")]
if locations_ids:
inventory_domain.append(("location_ids", "child_of", locations_ids))
Expand All @@ -20,7 +19,6 @@ def _get_locations_open_inventories(self, locations_ids=None):
# Early exit if no match found
return []
location_ids = inventories.mapped("location_ids")

# Extend to the children Locations
location_domain = [
"|",
Expand Down
4 changes: 0 additions & 4 deletions stock_inventory_lockdown/models/stock_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@


class StockLocation(models.Model):
"""Refuse changes during exhaustive Inventories"""

_inherit = "stock.location"

@api.constrains("location_id")
def _check_inventory_location_id(self):
"""Error if an inventory is being conducted here"""
vals = set(self.ids) | set(self.mapped("location_id").ids)
location_inventory_open_ids = self.env[
"stock.inventory"
Expand All @@ -21,7 +18,6 @@ def _check_inventory_location_id(self):
raise ValidationError(_("An inventory is being conducted at this location"))

def unlink(self):
"""Refuse unlink if an inventory is being conducted"""
location_inventory_open_ids = (
self.env["stock.inventory"].sudo()._get_locations_open_inventories(self.ids)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from odoo.exceptions import ValidationError


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

def _get_reserved_locations(self):
self.ensure_one()
Expand All @@ -20,33 +20,34 @@ def _get_dest_locations(self):

@api.constrains("location_dest_id", "location_id", "state")
def _check_locked_location(self):
for move in self.filtered(lambda m: m.state != "draft"):
for move_line in self.filtered(lambda m: m.state == "done"):
locked_location_ids = self.env[
"stock.inventory"
]._get_locations_open_inventories(
[move.location_dest_id.id, move.location_id.id]
[move_line.location_dest_id.id, move_line.location_id.id]
)
reserved_locs = move._get_reserved_locations()
dest_locs = move._get_dest_locations()
reserved_origin_loc = move_line.location_id
dest_loc = move_line.location_dest_id
if (
locked_location_ids
and not any(
[
move.location_dest_id.usage == "inventory",
move.location_id.usage == "inventory",
move_line.location_dest_id.usage == "inventory",
move_line.location_id.usage == "inventory",
]
)
and (
move.location_dest_id in locked_location_ids
or any([loc in locked_location_ids for loc in dest_locs])
or any([loc in locked_location_ids for loc in reserved_locs])
reserved_origin_loc in locked_location_ids
or dest_loc in locked_location_ids
)
):
location_names = locked_location_ids.mapped("complete_name")
raise ValidationError(
_(
"An inventory is being conducted at the following "
"location(s):\n - %s"
"Inventory adjusment underway at "
"the following location(s):\n- %s\n"
"Moving products to or from these locations is "
"not allowed until the inventory check is complete."
)
% "\n - ".join(location_names)
)

0 comments on commit 9462014

Please sign in to comment.