From 22543ec59236d5252b12299f0dc37236732dc1e9 Mon Sep 17 00:00:00 2001 From: DavidJForgeFlow Date: Thu, 18 Apr 2024 10:45:58 +0200 Subject: [PATCH] [IMP] stock_inventory Add company to adjustments to avoid multi-company errors. Adds information fields to tree view. Also adds 'Assign to' in stock inventory that propagates to quants. Also propagates the date field. --- stock_inventory/__manifest__.py | 1 + stock_inventory/models/stock_inventory.py | 83 ++++++++++++++++++++--- stock_inventory/models/stock_quant.py | 5 +- stock_inventory/security/security.xml | 11 +++ stock_inventory/views/stock_inventory.xml | 6 ++ 5 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 stock_inventory/security/security.xml diff --git a/stock_inventory/__manifest__.py b/stock_inventory/__manifest__.py index 42e10fb24356..2121d8d1bb7a 100644 --- a/stock_inventory/__manifest__.py +++ b/stock_inventory/__manifest__.py @@ -11,6 +11,7 @@ "depends": ["stock"], "data": [ "security/ir.model.access.csv", + "security/security.xml", "views/stock_inventory.xml", "views/stock_quant.xml", "views/stock_move_line.xml", diff --git a/stock_inventory/models/stock_inventory.py b/stock_inventory/models/stock_inventory.py index f237057dbadf..6bc8179a8002 100644 --- a/stock_inventory/models/stock_inventory.py +++ b/stock_inventory/models/stock_inventory.py @@ -12,6 +12,14 @@ class InventoryAdjustmentsGroup(models.Model): date = fields.Datetime(default=lambda self: fields.Datetime.now()) + company_id = fields.Many2one( + comodel_name="res.company", + readonly=True, + index=True, + states={"draft": [("readonly", False)]}, + default=lambda self: self.env.company, + ) + state = fields.Selection( [("draft", "Draft"), ("in_progress", "In Progress"), ("done", "Done")], default="draft", @@ -22,7 +30,9 @@ class InventoryAdjustmentsGroup(models.Model): ) location_ids = fields.Many2many( - "stock.location", string="Locations", domain="[('usage', '=', 'internal')]" + "stock.location", + string="Locations", + domain="[('usage', '=', 'internal'),('company_id', '=', company_id)]", ) product_selection = fields.Selection( @@ -37,15 +47,22 @@ class InventoryAdjustmentsGroup(models.Model): required=True, ) - product_ids = fields.Many2many("product.product", string="Products") + product_ids = fields.Many2many( + "product.product", string="Products", domain="[('company_id', '=', company_id)]" + ) - stock_quant_ids = fields.Many2many("stock.quant", string="Inventory Adjustment") + stock_quant_ids = fields.Many2many( + "stock.quant", + string="Inventory Adjustment", + domain="[('company_id', '=', company_id)]", + ) category_id = fields.Many2one("product.category", string="Product Category") lot_ids = fields.Many2many( "stock.production.lot", string="Lot/Serial Numbers", + domain="[('company_id', '=', company_id)]", ) stock_move_ids = fields.One2many( @@ -71,6 +88,13 @@ class InventoryAdjustmentsGroup(models.Model): "the locations settled, not the child ones." ) + responsible_id = fields.Many2one( + comodel_name="res.users", + string="Assigned to", + tracking=True, + help="Specific responsible of Inventory Adjustment.", + ) + @api.depends("stock_quant_ids") def _compute_count_stock_quants(self): self.count_stock_quants = len(self.stock_quant_ids) @@ -162,8 +186,8 @@ def action_state_to_in_progress(self): [ ("state", "=", "in_progress"), "|", - ("location_ids", "in", self.location_ids.mapped("id")), - ("location_ids", "in", self.location_ids.child_ids.ids), + ("location_ids", "in", self.location_ids.ids), + ("location_ids", "child_of", self.location_ids.ids), ], limit=1, ) @@ -176,12 +200,24 @@ def action_state_to_in_progress(self): ) self.state = "in_progress" self.refresh_stock_quant_ids() - self.stock_quant_ids.update({"to_do": True}) + self.stock_quant_ids.update( + { + "to_do": True, + "user_id": self.responsible_id, + "inventory_date": self.date, + } + ) return def action_state_to_done(self): self.state = "done" - self.stock_quant_ids.update({"to_do": True}) + self.stock_quant_ids.update( + { + "to_do": True, + "user_id": False, + "inventory_date": False, + } + ) return def action_auto_state_to_done(self): @@ -192,7 +228,13 @@ def action_auto_state_to_done(self): def action_state_to_draft(self): self.state = "draft" - self.stock_quant_ids.update({"to_do": True}) + self.stock_quant_ids.update( + { + "to_do": True, + "user_id": False, + "inventory_date": False, + } + ) self.stock_quant_ids = None return @@ -212,6 +254,31 @@ def action_view_stock_moves(self): result["context"] = [] return result + @api.constrains("state", "location_ids") + def _check_inventory_in_progress_not_override(self): + inventories = self.search([("state", "=", "in_progress")]) + for rec in inventories: + inventory = inventories.filtered( + lambda x: x.id != rec.id + and ( + any(i in x.location_ids for i in rec.location_ids) + or ( + any( + i in x.location_ids.child_internal_location_ids + for i in rec.location_ids + ) + and not x.exclude_sublocation + ) + ) + ) + if len(inventory) > 0: + raise ValidationError( + _( + "Cannot be more than one in progress inventory adjustment " + "affecting the same location at the same time." + ) + ) + @api.constrains("product_selection", "product_ids") def _check_one_product_in_product_selection(self): for rec in self: diff --git a/stock_inventory/models/stock_quant.py b/stock_inventory/models/stock_quant.py index bbd544d10e9f..a533890ffa13 100644 --- a/stock_inventory/models/stock_quant.py +++ b/stock_inventory/models/stock_quant.py @@ -15,7 +15,10 @@ def _apply_inventory(self): .search([("state", "=", "in_progress")]) .filtered( lambda x: rec.location_id in x.location_ids - or rec.location_id in x.location_ids.child_ids + or ( + rec.location_id in x.location_ids.child_internal_location_ids + and not x.exclude_sublocation + ) ) ) moves = record_moves.search( diff --git a/stock_inventory/security/security.xml b/stock_inventory/security/security.xml new file mode 100644 index 000000000000..11aab9b291b1 --- /dev/null +++ b/stock_inventory/security/security.xml @@ -0,0 +1,11 @@ + + + + Stock Inventory multi-company + + + ['|',('company_id','=',False),('company_id', 'in', company_ids)] + + diff --git a/stock_inventory/views/stock_inventory.xml b/stock_inventory/views/stock_inventory.xml index ae11b660aa65..fed3e75b6602 100644 --- a/stock_inventory/views/stock_inventory.xml +++ b/stock_inventory/views/stock_inventory.xml @@ -90,6 +90,8 @@ + + + + + +