Skip to content

Commit

Permalink
[REF] joint_buying_base :
Browse files Browse the repository at this point in the history
model joint.buying.tour.line : start_hour and arrival_hour are now computed on the fly, to avoid timezone errors
model joint.buying.tour.line : add start_date and arrival_date, stored field.
minor refactor
  • Loading branch information
legalsylvain committed Oct 19, 2023
1 parent 0df7830 commit de2fdbf
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 22 deletions.
2 changes: 1 addition & 1 deletion joint_buying_base/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Joint Buying - Base",
"version": "12.0.4.0.0",
"version": "12.0.5.0.0",
"category": "GRAP - Logistics",
"author": "GRAP,La Jardinière,Hashbang",
"website": "https://github.com/grap/odoo-addons-logistics",
Expand Down
18 changes: 18 additions & 0 deletions joint_buying_base/migrations/12.0.5.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (C) 2023 - 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(use_env=True)
def migrate(env, version):
tours = env["joint.buying.tour"].search([])
_logger.info(
"Initialize new start and arrival dates fields" f" for {len(tours)} tours"
)
tours.recompute_dates()
21 changes: 21 additions & 0 deletions joint_buying_base/migrations/12.0.5.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2023-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(
"Drop start_hour and arrival_hour column in joint_buying_tour_line"
" as there are now computed on the fly"
)

env.cr.execute("ALTER TABLE joint_buying_tour_line DROP COLUMN start_hour;")
env.cr.execute("ALTER TABLE joint_buying_tour_line DROP COLUMN arrival_hour;")
30 changes: 15 additions & 15 deletions joint_buying_base/models/joint_buying_tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from bokeh.palettes import Category20c
from bokeh.plotting import figure
from bokeh.transform import cumsum
from dateutil.relativedelta import relativedelta

from odoo import _, api, fields, models

Expand Down Expand Up @@ -280,27 +281,26 @@ def copy(self, default=None):
@api.multi
def write(self, vals):
res = super().write(vals)
if "start_date" in vals:
self.recompute_start_hours()
if {"start_date", "line_ids"}.intersection(vals.keys()):
self.recompute_dates()
return res

def estimate_route(self):
self.mapped("line_ids").estimate_route()
self.recompute_start_hours()

def recompute_start_hours(self):
def _time_to_float(time):
return time.hour + time.minute / 60
self.mapped("line_ids")._estimate_route()
self.recompute_dates()

def recompute_dates(self):
for tour in self:
if not tour.line_ids:
continue
date_tz = fields.Datetime.context_timestamp(self, tour.start_date)
start_hour = _time_to_float(date_tz.time())
start_date = tour.start_date
for line in tour.line_ids:
line.start_hour = start_hour
line.arrival_hour = start_hour + line.duration
start_hour += line.duration
arrival_date = start_date + relativedelta(hours=line.duration)
line.write(
{
"start_date": start_date,
"arrival_date": arrival_date,
}
)
start_date = arrival_date

def see_steps(self):
self.ensure_one()
Expand Down
26 changes: 22 additions & 4 deletions joint_buying_base/models/joint_buying_tour_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ class JointBuyingTourLine(models.Model):
comodel_name="joint.buying.tour", required=True, ondelete="cascade"
)

distance = fields.Float()

duration = fields.Float()

distance = fields.Float()
start_date = fields.Datetime()

arrival_date = fields.Datetime()

start_hour = fields.Float()
start_hour = fields.Float(compute="_compute_hours")

arrival_hour = fields.Float()
arrival_hour = fields.Float(compute="_compute_hours")

starting_point_id = fields.Many2one(
comodel_name="res.partner", context=_JOINT_BUYING_PARTNER_CONTEXT
Expand All @@ -60,6 +64,20 @@ class JointBuyingTourLine(models.Model):
compute="_compute_costs", store=True, currency_field="currency_id"
)

@api.depends("start_date", "arrival_date", "tour_id.start_date")
def _compute_hours(self):
for line in self:
line.write(
{
"start_hour": (line.start_date - line.tour_id.start_date).seconds
/ 3600,
"arrival_hour": (
line.arrival_date - line.tour_id.start_date
).seconds
/ 3600,
}
)

@api.depends(
"tour_id.hourly_cost", "tour_id.kilometer_cost", "duration", "distance"
)
Expand Down Expand Up @@ -98,7 +116,7 @@ def _estimate_route_project_osrm(self):
"duration": result.get("duration") / 3600,
}

def estimate_route(self):
def _estimate_route(self):
no_coordinate_partners = self.env["res.partner"]
for line in self.filtered(lambda x: x.sequence_type == "journey"):
if (
Expand Down
2 changes: 2 additions & 0 deletions joint_buying_base/views/view_joint_buying_tour.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ There may be traffic jams, delays, unforeseen events, etc...
<field name="line_ids">
<tree>
<field name="sequence" invisible="1"/>
<field name="start_date"/>
<field name="start_hour" widget="float_time"/>
<field name="arrival_date"/>
<field name="arrival_hour" widget="float_time"/>
<field name="currency_id" invisible="1" />
<field name="sequence_type"/>
Expand Down
3 changes: 1 addition & 2 deletions joint_buying_base/wizards/joint_buying_wizard_set_tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def _default_line_ids(self):
@api.multi
def set_tour(self):
self.ensure_one()
# TODO - Optimize. do not delete lines, if lines are the same
self.tour_id.line_ids.unlink()
current_starting_point = self.starting_point_id
line_vals = []
for i, wizard_line in enumerate(self.line_ids):

line_vals.append(
(
0,
Expand All @@ -87,5 +87,4 @@ def set_tour(self):
current_starting_point = wizard_line.point_id
if line_vals:
self.tour_id.write({"line_ids": line_vals})
self.tour_id.recompute_start_hours()
return True

0 comments on commit de2fdbf

Please sign in to comment.