diff --git a/stock_request/models/stock_request_abstract.py b/stock_request/models/stock_request_abstract.py
index f9bfb1786..5c8f5f19f 100644
--- a/stock_request/models/stock_request_abstract.py
+++ b/stock_request/models/stock_request_abstract.py
@@ -96,14 +96,47 @@ class StockRequest(models.AbstractModel):
route_id = fields.Many2one(
"stock.location.route",
string="Route",
- domain=[("product_selectable", "=", True)],
+ domain="[('id', 'in', route_ids)]",
ondelete="restrict",
)
+ route_ids = fields.Many2many(
+ "stock.location.route",
+ string="Routes",
+ compute="_compute_route_ids",
+ readonly=True,
+ )
+
_sql_constraints = [
("name_uniq", "unique(name, company_id)", "Name must be unique")
]
+ @api.depends("product_id", "warehouse_id", "location_id")
+ def _compute_route_ids(self):
+ route_obj = self.env["stock.location.route"]
+ routes = route_obj.search(
+ [("warehouse_ids", "in", self.mapped("warehouse_id").ids)]
+ )
+ routes_by_warehouse = {}
+ for route in routes:
+ for warehouse in route.warehouse_ids:
+ routes_by_warehouse.setdefault(
+ warehouse.id, self.env["stock.location.route"]
+ )
+ routes_by_warehouse[warehouse.id] |= route
+ for record in self:
+ routes = route_obj
+ 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 and routes_by_warehouse.get(record.warehouse_id.id):
+ routes |= routes_by_warehouse[record.warehouse_id.id]
+ parents = record.get_parents().ids
+ record.route_ids = routes.filtered(
+ lambda r: any(p.location_id.id in parents for p in r.rule_ids)
+ )
+
def get_parents(self):
location = self.location_id
result = location
diff --git a/stock_request/tests/test_stock_request.py b/stock_request/tests/test_stock_request.py
index 0241165b7..90ba1fa92 100644
--- a/stock_request/tests/test_stock_request.py
+++ b/stock_request/tests/test_stock_request.py
@@ -254,6 +254,7 @@ class TestStockRequestBase(TestStockRequest):
vals = stock_request.default_get(["warehouse_id", "company_id"])
stock_request.update(vals)
stock_request.onchange_product_id()
+ self.assertIn(self.route.id, stock_request.route_ids.ids)
self.stock_request_user.company_id = self.company_2
stock_request.company_id = self.company_2
diff --git a/stock_request/views/stock_request_order_views.xml b/stock_request/views/stock_request_order_views.xml
index 0f33a4b51..c65142a56 100644
--- a/stock_request/views/stock_request_order_views.xml
+++ b/stock_request/views/stock_request_order_views.xml
@@ -132,6 +132,7 @@
options="{'no_create': True}"
groups="stock.group_stock_multi_locations"
/>
+
diff --git a/stock_request/views/stock_request_views.xml b/stock_request/views/stock_request_views.xml
index ae5f87ee3..af0eb1c6b 100644
--- a/stock_request/views/stock_request_views.xml
+++ b/stock_request/views/stock_request_views.xml
@@ -182,6 +182,7 @@
options="{'no_create': True}"
groups="stock.group_stock_multi_locations"
/>
+
+