Files
stock-logistics-warehouse/stock_warehouse_calendar/models/stock_warehouse.py
Lois Rilo 07cd9444bf [IMP] stock_warehouse_calendar: robustness
when delta argument is a float `plan_days` method can return
unexpected False result. This can lead to silent errors.
Therefore we ensure that delta is a integer in the helper.
2021-02-12 13:09:11 +01:00

44 lines
1.5 KiB
Python

# Copyright 2018-19 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from datetime import datetime, timedelta
from odoo import fields, models
class StockWarehouse(models.Model):
_inherit = "stock.warehouse"
calendar_id = fields.Many2one(
comodel_name="resource.calendar", string="Working Hours"
)
def wh_plan_days(self, date_from, delta):
"""Helper method to schedule warehouse operations based on its
working days (if set).
:param datetime date_from: reference date.
:param integer delta: offset to apply.
:return: datetime: resulting date.
"""
self.ensure_one()
if isinstance(delta, float):
delta = round(delta)
if not isinstance(date_from, datetime):
date_from = fields.Datetime.to_datetime(date_from)
if delta == 0:
return date_from
if self.calendar_id:
if delta < 0:
# We force the date planned to be at the beginning of the day.
# So no work intervals are found in the reference date.
dt_planned = date_from.replace(hour=0)
else:
# We force the date planned at the end of the day.
dt_planned = date_from.replace(hour=23)
date_result = self.calendar_id.plan_days(delta, dt_planned)
else:
date_result = date_from + timedelta(days=delta)
return date_result