From 58fbb3b5f2cf329be26d49af8259c43c813d8b37 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Mon, 30 Oct 2023 17:56:23 +0100 Subject: [PATCH] [IMP] wizard.find.route : Specific Use Case : The first tour that goes to a route is not allways the best !!! --- .../demo/joint_buying_transport_request.xml | 8 +++++ .../test_joint_buying_wizard_find_route.py | 14 +++++++- .../wizards/joint_buying_wizard_find_route.py | 36 +++++++++++++++++-- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/joint_buying_product/demo/joint_buying_transport_request.xml b/joint_buying_product/demo/joint_buying_transport_request.xml index 4d31832a..f7940a0e 100644 --- a/joint_buying_product/demo/joint_buying_transport_request.xml +++ b/joint_buying_product/demo/joint_buying_transport_request.xml @@ -34,4 +34,12 @@ 66 + + + + + 222 + 33 + + diff --git a/joint_buying_product/tests/test_joint_buying_wizard_find_route.py b/joint_buying_product/tests/test_joint_buying_wizard_find_route.py index ac21785f..70b684b7 100644 --- a/joint_buying_product/tests/test_joint_buying_wizard_find_route.py +++ b/joint_buying_product/tests/test_joint_buying_wizard_find_route.py @@ -8,7 +8,7 @@ from .test_abstract import TestAbstract -@tagged("post_install", "-at_install") +@tagged("post_install", "-at_install", "find_route") class TestJointBuyingWizardFindRoute(TestAbstract): def setUp(self): super().setUp() @@ -53,6 +53,18 @@ def test_23_transport_request_vev_fumet_week_1(self): "joint_buying_product.request_vev_fumet_dombes_week_1", [], "not_computable" ) + def test_24_transport_request_vev_che_week_2(self): + """Complex case: a later start arrives earlier""" + self._verify_tour_lines_computation( + "joint_buying_product.request_vev_che_week_2", + [ + "joint_buying_base.tour_lyon_loire_3_line_2", + "joint_buying_base.tour_lyon_drome_2_line_2", + "joint_buying_base.tour_lyon_drome_2_line_4", + ], + "computed", + ) + def _verify_tour_lines_computation( self, request_xml_id, tour_line_xml_ids, expected_state ): diff --git a/joint_buying_product/wizards/joint_buying_wizard_find_route.py b/joint_buying_product/wizards/joint_buying_wizard_find_route.py index dab5424e..a2641fb5 100644 --- a/joint_buying_product/wizards/joint_buying_wizard_find_route.py +++ b/joint_buying_product/wizards/joint_buying_wizard_find_route.py @@ -121,21 +121,46 @@ def _create_initial_node(self, tree, partner, date): @api.model def _create_following_node(self, tree, parent, line, destination): + partner = line.arrival_point_id date = line.arrival_date best_main_node = ( line.arrival_point_id == destination or line.arrival_point_id.joint_buying_is_durable_storage ) - return tree.create_node( + new_node = tree.create_node( parent=parent, tag=f"{partner.joint_buying_code}-{date}-{line.id}" - f"-{best_main_node and '-BEST_MAIN_NODE'}", + f"{best_main_node and '-BEST_MAIN_NODE' or ''}", data=SimpleNamespace( partner=partner, date=date, best_main_node=best_main_node, line=line ), ) + best_nodes = [ + x + for x in tree.all_nodes() + if x.data.best_main_node and x.data.partner == line.arrival_point_id + ] + + if len(best_nodes) > 1: + the_best_node = self._get_best_node(best_nodes) + for node in best_nodes: + if node == the_best_node: + continue + node.tag = node.tag.replace("-BEST_MAIN_NODE", "") + node.data.best_main_node = False + + return new_node + + @api.model + def _get_best_node(self, nodes): + best_node = nodes[0] + for node in nodes[1:]: + if node.data.date < best_node.data.date: + best_node = node + return best_node + @api.model def _get_startable_nodes(self, tree): """ @@ -208,7 +233,12 @@ def _populate_tree(self, transport_request): tour=tour, from_line=line, destination=transport_request.destination_partner_id, - excludes=[x.data.partner for x in startable_nodes], + # We don't want to target origin + # you don't want to go round and round in the same place. + excludes=[ + transport_request.origin_partner_id, + startable_node.data.partner, + ], ) if found_lines: current_node = startable_node