diff --git a/stock_helper/models/stock_location.py b/stock_helper/models/stock_location.py index cad6d288c..1a1d813a1 100644 --- a/stock_helper/models/stock_location.py +++ b/stock_helper/models/stock_location.py @@ -1,4 +1,5 @@ # Copyright 2020-2021 Camptocamp SA (http://www.camptocamp.com) +# Copyright 2022-2023 Michael Tietz (MT Software) # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). from odoo import models @@ -18,3 +19,20 @@ class StockLocation(models.Model): # Efficient way to verify that the current location is # below one of the other location without using SQL. return func(self.parent_path.startswith(other.parent_path) for other in others) + + def _get_source_location_from_route(self, route, procure_method): + self.ensure_one() + route.ensure_one() + values = { + "route_ids": route, + } + current_location = self + while current_location: + rule = self.env["procurement.group"]._get_rule( + self.env["product.product"], current_location, values + ) + if not rule: + return self.browse() + if rule.procure_method == procure_method: + return rule.location_src_id + current_location = rule.location_src_id diff --git a/stock_helper/tests/__init__.py b/stock_helper/tests/__init__.py index e67580e62..be6a115be 100644 --- a/stock_helper/tests/__init__.py +++ b/stock_helper/tests/__init__.py @@ -1 +1,2 @@ from . import test_location_is_sublocation_of +from . import test_location_source_from_route diff --git a/stock_helper/tests/test_location_source_from_route.py b/stock_helper/tests/test_location_source_from_route.py new file mode 100644 index 000000000..e3405e415 --- /dev/null +++ b/stock_helper/tests/test_location_source_from_route.py @@ -0,0 +1,40 @@ +# Copyright 2022 Michael Tietz (MT Software) +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + + +from .common import StockHelperCommonCase + + +class TestStockLocationSoruceFromRoute(StockHelperCommonCase): + def test_get_source_location_from_route(self): + self.wh.delivery_steps = "pick_pack_ship" + route = self.wh.delivery_route_id + + location = self.env.ref("stock.stock_location_customers") + source_location = location._get_source_location_from_route( + route, "make_to_stock" + ) + self.assertEqual(source_location, self.wh.lot_stock_id) + + source_location = location._get_source_location_from_route( + route, "make_to_order" + ) + self.assertEqual(source_location, self.wh.wh_output_stock_loc_id) + + location = source_location + source_location = location._get_source_location_from_route( + route, "make_to_order" + ) + self.assertEqual(source_location, self.wh.wh_pack_stock_loc_id) + + location = source_location + source_location = location._get_source_location_from_route( + route, "make_to_stock" + ) + self.assertEqual(source_location, self.wh.lot_stock_id) + + location = source_location + source_location = location._get_source_location_from_route( + route, "make_to_stock" + ) + self.assertFalse(source_location)