From 1c85d1564e84f2fb5698a677adb129977e01e7f3 Mon Sep 17 00:00:00 2001 From: Michael Tietz Date: Mon, 17 Apr 2023 14:17:37 +0200 Subject: [PATCH] [IMP] stock_helper: add location._get_source_location_from_route returns the next source location of a location by the given route and procure_method --- stock_helper/models/stock_location.py | 19 ++++++++- stock_helper/tests/__init__.py | 1 + .../tests/test_location_source_from_route.py | 40 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 stock_helper/tests/test_location_source_from_route.py diff --git a/stock_helper/models/stock_location.py b/stock_helper/models/stock_location.py index 66cc975be..03ad17023 100644 --- a/stock_helper/models/stock_location.py +++ b/stock_helper/models/stock_location.py @@ -1,5 +1,5 @@ # Copyright 2020-2021 Camptocamp SA (http://www.camptocamp.com) -# Copyright 2022 Michael Tietz (MT Software) +# Copyright 2022-2023 Michael Tietz (MT Software) # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). from odoo import models @@ -42,3 +42,20 @@ class StockLocation(models.Model): if self.parent_path.startswith(warehouse.view_location_id.parent_path): return warehouse return warehouses.browse() + + 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 b4e276258..509aac063 100644 --- a/stock_helper/tests/__init__.py +++ b/stock_helper/tests/__init__.py @@ -1,2 +1,3 @@ from . import test_location_is_sublocation_of from . import test_location_get_closest_warehouse +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)