diff --git a/delivery_carrier_calendar/README.rst b/delivery_carrier_calendar/README.rst new file mode 100644 index 0000000..8a24d9b --- /dev/null +++ b/delivery_carrier_calendar/README.rst @@ -0,0 +1,5 @@ +delivery_carrier_calendar +------------------------- + +Implement a resource calendar for delivery carrier indicating which days they +operate on. diff --git a/delivery_carrier_calendar/__init__.py b/delivery_carrier_calendar/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/delivery_carrier_calendar/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/delivery_carrier_calendar/__manifest__.py b/delivery_carrier_calendar/__manifest__.py new file mode 100644 index 0000000..b27add3 --- /dev/null +++ b/delivery_carrier_calendar/__manifest__.py @@ -0,0 +1,14 @@ +{ + "name": "delivery_carrier_calendar", + "summary": "Delivery Carrier Calendar", + "author": "Glo Networks", + "website": "https://github.com/GlodoUK/odoo-addons", + "category": "Delivery", + "version": "12.0.1.0.0", + "depends": ["delivery", "resource"], + "data": [ + "views/delivery_carrier.xml", + ], + "demo": [], + "license": "Other proprietary", +} diff --git a/delivery_carrier_calendar/models/__init__.py b/delivery_carrier_calendar/models/__init__.py new file mode 100644 index 0000000..6edba1a --- /dev/null +++ b/delivery_carrier_calendar/models/__init__.py @@ -0,0 +1,2 @@ +from . import delivery_carrier +from . import stock_picking diff --git a/delivery_carrier_calendar/models/delivery_carrier.py b/delivery_carrier_calendar/models/delivery_carrier.py new file mode 100644 index 0000000..1246c26 --- /dev/null +++ b/delivery_carrier_calendar/models/delivery_carrier.py @@ -0,0 +1,38 @@ +from dateutil.relativedelta import relativedelta + +from odoo import fields, models + + +class DeliveryCarrier(models.Model): + _inherit = "delivery.carrier" + + carrier_calendar_id = fields.Many2one("resource.calendar") + lead_time = fields.Integer( + string="Lead Days", + help="Lead time for this carrier, in days.", + default=0, + ) + + def plan_days( + self, + day_dt, + days=1, + compute_leaves=True, + domain=None, + safety_lead_days=1 + ): + self.ensure_one() + + lead_days = 0 + + if self.lead_time: + lead_days += self.lead_time + elif safety_lead_days: + lead_days += safety_lead_days + + if self.carrier_calendar_id: + return self.carrier_calendar_id.plan_days( + days + lead_days, day_dt, compute_leaves=compute_leaves, domain=domain + ) + + return day_dt + relativedelta(days=lead_days) diff --git a/delivery_carrier_calendar/models/stock_picking.py b/delivery_carrier_calendar/models/stock_picking.py new file mode 100644 index 0000000..5a5a9e8 --- /dev/null +++ b/delivery_carrier_calendar/models/stock_picking.py @@ -0,0 +1,32 @@ +from odoo import api, fields, models + + +class StockPicking(models.Model): + _inherit = "stock.picking" + + @api.one + @api.depends("move_lines.date_expected") + # XXX: This is @api.one to preserve compatibility with upstream + # pylint: disable=api-one-deprecated + def _compute_scheduled_date(self): + scheduled_date = False + + if self.carrier_id.carrier_calendar_id: + if self.move_type == "direct": + scheduled_date = min( + self.move_lines.mapped("date_expected") or [fields.Datetime.now()] + ) + else: + scheduled_date = max( + self.move_lines.mapped("date_expected") or [fields.Datetime.now()] + ) + + scheduled_date = self.carrier_id.plan_days( + scheduled_date, safety_lead_days=0 + ) + + if scheduled_date: + self.scheduled_date = scheduled_date + return + + return super()._compute_scheduled_date() diff --git a/delivery_carrier_calendar/views/delivery_carrier.xml b/delivery_carrier_calendar/views/delivery_carrier.xml new file mode 100644 index 0000000..49df5c7 --- /dev/null +++ b/delivery_carrier_calendar/views/delivery_carrier.xml @@ -0,0 +1,14 @@ + + + view_delivery_carrier_form + delivery.carrier + 75 + + + + + + + + + diff --git a/setup/delivery_carrier_calendar/odoo/addons/delivery_carrier_calendar b/setup/delivery_carrier_calendar/odoo/addons/delivery_carrier_calendar new file mode 120000 index 0000000..15e0aef --- /dev/null +++ b/setup/delivery_carrier_calendar/odoo/addons/delivery_carrier_calendar @@ -0,0 +1 @@ +../../../../delivery_carrier_calendar \ No newline at end of file diff --git a/setup/delivery_carrier_calendar/setup.py b/setup/delivery_carrier_calendar/setup.py new file mode 100644 index 0000000..28c57bb --- /dev/null +++ b/setup/delivery_carrier_calendar/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)