diff --git a/joint_buying_base/migrations/12.0.7.0.0/pre-migration.py b/joint_buying_base/migrations/12.0.7.0.0/pre-migration.py new file mode 100644 index 00000000..b15e0a3a --- /dev/null +++ b/joint_buying_base/migrations/12.0.7.0.0/pre-migration.py @@ -0,0 +1,37 @@ +# Copyright (C) 2024-Today: GRAP (http://www.grap.coop) +# @author: Sylvain LE GAL (https://twitter.com/legalsylvain) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +import logging + +from openupgradelib import openupgrade + +_logger = logging.getLogger(__name__) + + +@openupgrade.migrate() +def migrate(env, version): + + _logger.info( + "Fix bad computation of joint_buying_transport_request.start_date field." + ) + openupgrade.logged_query( + """ + UPDATE joint_buying_transport_request tr_main + SET start_date = tmp.min_start_date + FROM ( + SELECT tr.id as transport_request_id, + min(tl.start_date) as min_start_date + FROM joint_buying_transport_request tr + INNER JOIN joint_buying_transport_request_line trl + ON trl.request_id = tr.id + INNER JOIN joint_buying_tour_line tl + ON tl.id = trl.tour_line_id + WHERE tr.state = 'computed' + GROUP BY tr.id + ) as tmp + WHERE + tr_main.id = tmp.transport_request_id + AND tr_main.start_date != tmp.min_start_date; + """ + ) diff --git a/joint_buying_base/models/joint_buying_transport_request.py b/joint_buying_base/models/joint_buying_transport_request.py index 1d97956b..1b12df0d 100644 --- a/joint_buying_base/models/joint_buying_transport_request.py +++ b/joint_buying_base/models/joint_buying_transport_request.py @@ -291,7 +291,7 @@ def _set_tour_lines(self, tour_lines): self.write( { - "start_date": max(tour_lines.mapped("start_date")), + "start_date": min(tour_lines.mapped("start_date")), "arrival_date": max(tour_lines.mapped("arrival_date")), "state": "computed", "line_ids": [(5,)] + [(0, 0, x) for x in line_vals],