Skip to content

Commit

Permalink
[IMP] - depends on base_time_window
Browse files Browse the repository at this point in the history
  • Loading branch information
sbejaoui committed Feb 21, 2023
1 parent cf14a2c commit 15e8696
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 257 deletions.
4 changes: 1 addition & 3 deletions shipment_advice_planner_toursolver/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
"license": "AGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-transport",
"depends": ["shipment_advice_planner"],
"depends": ["shipment_advice_planner", "base_time_window"],
"data": [
# data
"data/toursolver_delivery_week_day.xml",
"data/toursolver_backend.xml",
"data/ir_ui_menu.xml",
"data/ir_sequence.xml",
Expand All @@ -20,7 +19,6 @@
"security/toursolver_backend.xml",
"security/toursolver_resource.xml",
"security/toursolver_task.xml",
"security/toursolver_delivery_week_day.xml",
"security/toursolver_delivery_window.xml",
# views
"views/toursolver_backend.xml",
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion shipment_advice_planner_toursolver/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
from . import toursolver_task
from . import stock_picking
from . import toursolver_delivery_window
from . import toursolver_delivery_week_day
from . import res_partner
from . import shipment_advice
35 changes: 6 additions & 29 deletions shipment_advice_planner_toursolver/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,14 @@ def _get_delivery_windows(self, day_name):
"""
Return the list of delivery windows by partner id for the given day.
:param day: The day name (see toursolver.delivery.week.day)
:param day: The day name (see time.weekday)
:return: dict partner_id:[delivery_window, ]
"""
self.ensure_one()
week_day_id = self.env["toursolver.delivery.week.day"]._get_id_by_name(day_name)
week_day_id = self.env["time.weekday"]._get_id_by_name(day_name)
return self.env["toursolver.delivery.window"].search(
[("partner_id", "=", self.id), ("week_day_ids", "in", week_day_id)]
[
("partner_id", "=", self.id),
("time_window_weekday_ids", "in", week_day_id),
]
)

def _get_delivery_sequence(self, day_name):
"""
Return a sequence position by partner id for the given day.
The sequence is computed from the delivery_widow start
:param day: The day name (see toursolver.delivery.week.day)
:return: dict partner_id:sequence
"""
week_day_id = self.env["toursolver.delivery.week.day"]._get_id_by_name(day_name)
res = {}
windows = self.env["toursolver.delivery.window"].search(
[("partner_id", "in", self.ids), ("week_day_ids", "in", week_day_id)],
order="start ASC",
)
i = 1
for window in windows.sorted("start"):
if window.partner_id.id not in res:
res[window.partner_id.id] = i
i += 1
for partner in self.sorted("name"):
if partner.id not in res:
res[partner.id] = i
i += 1
return res

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
# Copyright 2020 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import math

from psycopg2.extensions import AsIs

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo import fields, models


class ToursolverDeliveryWindow(models.Model):

_name = "toursolver.delivery.window"
_inherit = "time.window.mixin"
_description = "TourSolver Delivery Window"
_order = "partner_id, start"
_order = "partner_id, time_window_start"
_time_window_overlap_check_field = "partner_id"

start = fields.Float("From", required=True)
end = fields.Float("To", required=True)
week_day_ids = fields.Many2many(
comodel_name="toursolver.delivery.week.day", required=True
)
partner_id = fields.Many2one(
comodel_name="res.partner", required=True, index=True, ondelete="cascade"
)
Expand All @@ -28,69 +21,3 @@ class ToursolverDeliveryWindow(models.Model):
required=True,
default="preferable",
)

@api.constrains("start", "end", "week_day_ids")
def _check_window_no_onverlaps(self):
week_days_field = self._fields["week_day_ids"]
for record in self:
if record.start > record.end:
raise ValidationError(_("End time must be higher than start time."))
# here we use a plain SQL query to benefit of the numrange
# function available in PostgresSQL
# (http://www.postgresql.org/docs/current/static/rangetypes.html)
sql_query = """
SELECT
id
FROM
%(table)s w
join %(relation)s as d
on d.%(relation_window_fkey)s = w.id
WHERE
NUMRANGE(w.start::numeric, w.end::numeric) &&
NUMRANGE(%(start)s::numeric, %(end)s::numeric)
AND w.id != %(window_id)s
AND d.%(relation_week_day_fkey)s in %(week_day_ids)s
AND w.partner_id = %(partner_id)s"""
self.env.cr.execute(
sql_query,
dict(
table=AsIs(self._table),
relation=AsIs(week_days_field.relation),
relation_window_fkey=AsIs(week_days_field.column1),
relation_week_day_fkey=AsIs(week_days_field.column2),
start=record.start,
end=record.end,
window_id=record.id,
week_day_ids=tuple(record.week_day_ids.ids),
partner_id=record.partner_id.id,
),
)
res = self.env.cr.fetchall()
if res:
other = self.browse(res[0][0])
raise ValidationError(
_("%(rec_display_name)s overlaps %(other_display_name)s")
% dict(
rec_display_name=record.display_name,
other_display_name=other.display_name,
)
)

@api.depends("start", "end", "week_day_ids")
def _compute_display_name(self):
for record in self:
days = ", ".join(record.week_day_ids.mapped("display_name"))
start = self.float_to_time_repr(record.start)
end = self.float_to_time_repr(record.end)
record.display_name = f"{days}: From {start} to {end}"

@api.model
def float_to_time_repr(self, value):
pattern = "%02d:%02d"
hour = math.floor(value)
min_ = round((value % 1) * 60)
if min_ == 60:
min_ = 0
hour += 1

return pattern % (hour, min_)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ToursolverResource(models.Model):
)
use_delivery_person_coordinates_as_end = fields.Boolean(
help="If true the computed delivery will end at the delivery person's "
"address. Otherwise it will end at the Alcyon warehouse"
"address. Otherwise it will end at the warehouse address"
)
partner_id = fields.Many2one(comodel_name="res.partner", string="Contact")

Expand Down
6 changes: 4 additions & 2 deletions shipment_advice_planner_toursolver/models/toursolver_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,10 @@ def _toursolver_json_request_order_time_window(self, partner):
for window in delivery_windows:
time_windows.append(
{
"beginTime": window.float_to_time_repr(window.start),
"endTime": window.float_to_time_repr(window.end),
"beginTime": window.float_to_time_repr(
window.time_window_start
),
"endTime": window.float_to_time_repr(window.time_window_end),
}
)
else:
Expand Down

This file was deleted.

Loading

0 comments on commit 15e8696

Please sign in to comment.