From d7b91741d5a2fa85522625f32b072e47b50204a8 Mon Sep 17 00:00:00 2001 From: Lois Rilo Date: Wed, 31 Jul 2019 17:38:31 +0200 Subject: [PATCH] [12.0][REW] stock_warehouse_calendar: add plan days helper method the method `wh_plan_days` can be used in any place where planning or scheduling needs to be done in a given warehouse context, considering the working days if set. --- stock_warehouse_calendar/README.rst | 3 +- stock_warehouse_calendar/models/stock_rule.py | 9 ++-- .../models/stock_warehouse.py | 31 ++++++++++++- .../readme/CONTRIBUTORS.rst | 3 +- .../static/description/index.html | 5 ++- .../tests/test_stock_warehouse_calendar.py | 45 +++++++++++++++++-- 6 files changed, 83 insertions(+), 13 deletions(-) diff --git a/stock_warehouse_calendar/README.rst b/stock_warehouse_calendar/README.rst index 160e057b8..41ebbc28f 100644 --- a/stock_warehouse_calendar/README.rst +++ b/stock_warehouse_calendar/README.rst @@ -87,7 +87,8 @@ Authors Contributors ~~~~~~~~~~~~ -* Jordi Ballester (EFICENT) . +* Jordi Ballester +* Lois Rilo Maintainers ~~~~~~~~~~~ diff --git a/stock_warehouse_calendar/models/stock_rule.py b/stock_warehouse_calendar/models/stock_rule.py index 896a6faae..b3751e256 100644 --- a/stock_warehouse_calendar/models/stock_rule.py +++ b/stock_warehouse_calendar/models/stock_rule.py @@ -1,7 +1,7 @@ -# Copyright 2018 Eficent Business and IT Consulting Services, S.L. +# Copyright 2018-19 Eficent Business and IT Consulting Services, S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -from odoo import fields, models +from odoo import models from datetime import datetime @@ -13,11 +13,10 @@ class StockRule(models.Model): res = super(StockRule, self)._get_stock_move_values( product_id, product_qty, product_uom, location_id, name, origin, values, group_id) - dt_planned = fields.Datetime.to_datetime(values['date_planned']) warehouse = self.propagate_warehouse_id or self.warehouse_id if warehouse.calendar_id and self.delay: - date_expected = warehouse.calendar_id.plan_days( - -1 * self.delay - 1, dt_planned) + date_expected = warehouse.wh_plan_days( + values['date_planned'], -1 * self.delay) if date_expected > datetime.now(): res['date'] = date_expected res['date_expected'] = date_expected diff --git a/stock_warehouse_calendar/models/stock_warehouse.py b/stock_warehouse_calendar/models/stock_warehouse.py index b14104db6..8509cd233 100644 --- a/stock_warehouse_calendar/models/stock_warehouse.py +++ b/stock_warehouse_calendar/models/stock_warehouse.py @@ -1,6 +1,8 @@ -# Copyright 2018 Eficent Business and IT Consulting Services, S.L. +# Copyright 2018-19 Eficent Business and IT Consulting Services, S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from datetime import datetime, timedelta + from odoo import fields, models @@ -11,3 +13,30 @@ class StockWarehouse(models.Model): 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 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 diff --git a/stock_warehouse_calendar/readme/CONTRIBUTORS.rst b/stock_warehouse_calendar/readme/CONTRIBUTORS.rst index d72a49b94..58e705af2 100644 --- a/stock_warehouse_calendar/readme/CONTRIBUTORS.rst +++ b/stock_warehouse_calendar/readme/CONTRIBUTORS.rst @@ -1 +1,2 @@ -* Jordi Ballester (EFICENT) . \ No newline at end of file +* Jordi Ballester +* Lois Rilo diff --git a/stock_warehouse_calendar/static/description/index.html b/stock_warehouse_calendar/static/description/index.html index 5bb345f1f..ca00c4c6c 100644 --- a/stock_warehouse_calendar/static/description/index.html +++ b/stock_warehouse_calendar/static/description/index.html @@ -3,7 +3,7 @@ - + Stock Warehouse Calendar