diff --git a/stock_orderpoint_route/models/stock_warehouse_orderpoint.py b/stock_orderpoint_route/models/stock_warehouse_orderpoint.py index 82e855b56..ca857510a 100644 --- a/stock_orderpoint_route/models/stock_warehouse_orderpoint.py +++ b/stock_orderpoint_route/models/stock_warehouse_orderpoint.py @@ -20,33 +20,32 @@ class StockWarehouseOrderpoint(models.Model): @api.depends("product_id", "warehouse_id", "warehouse_id.route_ids", "location_id") def _compute_route_ids(self): route_obj = self.env["stock.location.route"] - for wh in self.mapped("warehouse_id"): - wh_routes = wh.route_ids - for record in self.filtered(lambda r: r.warehouse_id == wh): - routes = route_obj.browse() - if record.product_id: - routes += record.product_id.mapped( - "route_ids" - ) | record.product_id.mapped("categ_id").mapped("total_route_ids") - if record.warehouse_id: - routes |= wh_routes - parents = record.get_parents() - record.route_ids = routes.filtered( - lambda route: any( - p.location_id in parents - for p in route.rule_ids.filtered( - lambda rule: rule.action in ("pull", "pull_push") - ).mapped("location_src_id") - ) - ) + for record in self: + wh_routes = record.warehouse_id.route_ids + routes = route_obj.browse() + if record.product_id: + routes += record.product_id.mapped( + "route_ids" + ) | record.product_id.mapped("categ_id").mapped("total_route_ids") + if record.warehouse_id: + routes |= wh_routes + parents = record.get_parents() + record.route_ids = self._get_location_routes_of_parents(routes, parents) + + def _get_location_routes_of_parents(self, routes, parents): + return routes.filtered( + lambda route: any( + p.location_id in parents + for p in route.rule_ids.filtered( + lambda rule: rule.action in ("pull", "pull_push") + ).mapped("location_src_id") + ) + ) def get_parents(self): - location = self.location_id - result = location - while location.location_id: - location = location.location_id - result |= location - return result + return self.env["stock.location"].search( + [("id", "parent_of", self.location_id.id)] + ) def _prepare_procurement_values(self, product_qty, date=False, group=False): res = super()._prepare_procurement_values(product_qty, date=date, group=group) diff --git a/stock_orderpoint_route/tests/test_stock_orderpoint_route.py b/stock_orderpoint_route/tests/test_stock_orderpoint_route.py index 7989e0c52..f0bde822c 100644 --- a/stock_orderpoint_route/tests/test_stock_orderpoint_route.py +++ b/stock_orderpoint_route/tests/test_stock_orderpoint_route.py @@ -8,6 +8,7 @@ class TestStockOrderpointRoute(common.SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) # common models cls.orderpoint_model = cls.env["stock.warehouse.orderpoint"] @@ -99,7 +100,7 @@ class TestStockOrderpointRoute(common.SavepointCase): def _create_user(cls, name, group_ids, company_ids): return ( cls.env["res.users"] - .with_context({"no_reset_password": True}) + .with_context(no_reset_password=True) .create( { "name": name, @@ -136,7 +137,7 @@ class TestStockOrderpointRoute(common.SavepointCase): "location_id": self.warehouse.lot_stock_id.id, } - orderpoint = self.orderpoint_model.sudo(self.stock_manager).create(vals) + orderpoint = self.orderpoint_model.with_user(self.stock_manager).create(vals) self.assertIn(self.route, orderpoint.route_ids) self.assertIn(self.route2, orderpoint.route_ids) orderpoint.route_id = self.route.id @@ -161,7 +162,7 @@ class TestStockOrderpointRoute(common.SavepointCase): "location_id": self.warehouse.lot_stock_id.id, } - orderpoint = self.orderpoint_model.sudo(self.stock_manager).create(vals) + orderpoint = self.orderpoint_model.with_user(self.stock_manager).create(vals) self.assertIn(self.route, orderpoint.route_ids) self.assertIn(self.route2, orderpoint.route_ids) orderpoint.route_id = self.route2.id