diff --git a/sale_stock_partner_delivery_window/README.rst b/sale_stock_partner_delivery_window/README.rst new file mode 100644 index 000000000000..6f7a4ce46c76 --- /dev/null +++ b/sale_stock_partner_delivery_window/README.rst @@ -0,0 +1,92 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +================================== +Sale Stock Partner Delivery Window +================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:75bb9a80a0fa2af909228102b088319b45e6d3dd055d95e797521fdfd19c0bd5 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-workflow/tree/19.0/sale_stock_partner_delivery_window + :alt: OCA/stock-logistics-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/stock-logistics-workflow-19-0/stock-logistics-workflow-19-0-sale_stock_partner_delivery_window + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Use the partner's "Delivery schedule preference" to compute the Sales +Order's Expected Date, which ends up being propagated to the Delivery. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Camptocamp + +Contributors +------------ + +- `Camptocamp `__ + + - Iván Todorovich + - Gaëtan Vaujour + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-ivantodorovich| image:: https://github.com/ivantodorovich.png?size=40px + :target: https://github.com/ivantodorovich + :alt: ivantodorovich + +Current `maintainer `__: + +|maintainer-ivantodorovich| + +This module is part of the `OCA/stock-logistics-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_stock_partner_delivery_window/__init__.py b/sale_stock_partner_delivery_window/__init__.py new file mode 100644 index 000000000000..0650744f6bc6 --- /dev/null +++ b/sale_stock_partner_delivery_window/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/sale_stock_partner_delivery_window/__manifest__.py b/sale_stock_partner_delivery_window/__manifest__.py new file mode 100644 index 000000000000..8b1aaad197da --- /dev/null +++ b/sale_stock_partner_delivery_window/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Sale Stock Partner Delivery Window", + "summary": "Use the partner's 'Delivery schedule preference' in Sales Orders", + "version": "19.0.1.0.0", + "author": "Camptocamp, Odoo Community Association (OCA)", + "maintainers": ["ivantodorovich"], + "website": "https://github.com/OCA/stock-logistics-workflow", + "license": "AGPL-3", + "category": "Sales", + "depends": ["sale_stock", "stock_partner_delivery_window"], + "auto_install": True, +} diff --git a/sale_stock_partner_delivery_window/models/__init__.py b/sale_stock_partner_delivery_window/models/__init__.py new file mode 100644 index 000000000000..31c481899626 --- /dev/null +++ b/sale_stock_partner_delivery_window/models/__init__.py @@ -0,0 +1,2 @@ +from . import res_partner +from . import sale_order_line diff --git a/sale_stock_partner_delivery_window/models/res_partner.py b/sale_stock_partner_delivery_window/models/res_partner.py new file mode 100644 index 000000000000..473c5825bde8 --- /dev/null +++ b/sale_stock_partner_delivery_window/models/res_partner.py @@ -0,0 +1,82 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from datetime import date, datetime, timedelta + +import pytz + +from odoo import fields, models +from odoo.exceptions import UserError +from odoo.tools.date_utils import start_of + + +class ResPartner(models.Model): + _inherit = "res.partner" + + def _next_available_delivery_date( + self, from_date: date | datetime | None = None + ) -> datetime: + """Compute the next available delivery date""" + # If from_date is not provided, use the current datetime + if from_date is None: # pragma: no cover + from_date = fields.Datetime.now() + # Pre-compute the from_datetime, in case from_date is a `date` + # We use the start of the day in the partner's timezone + tz = pytz.timezone(self.tz or self.env.company.partner_id.tz or "UTC") + from_datetime_tz_aware = tz.localize(fields.Datetime.to_datetime(from_date)) + from_datetime = from_datetime_tz_aware.astimezone(pytz.utc).replace(tzinfo=None) + # If the delivery is anytime, simply return the from_datetime + if self.delivery_time_preference == "anytime": + return from_datetime + # If it's workdays, we need to check if the from_datetime is a weekday + # or adjust accordingly + elif self.delivery_time_preference == "workdays": + weekday = from_datetime_tz_aware.weekday() + if weekday <= 4: + return from_datetime + return from_datetime + timedelta(days=7 - weekday) + # If we're using time windows, we search for the next available slot + # We use the tz-aware datetime, as windows are expressed in the partner's tz + elif self.delivery_time_preference == "time_windows": + for days_to_add in range(7): + next_date = ( + start_of( + from_datetime_tz_aware + timedelta(days=days_to_add), "day" + ) + if days_to_add + else from_datetime_tz_aware + ) + for window in self.delivery_time_window_ids: + # Check if the window is available for this day + weekdays = set( + map(int, window.time_window_weekday_ids.mapped("name")) + ) + if next_date.weekday() not in weekdays: + continue + start_time = window.get_time_window_start_time() + end_time = window.get_time_window_end_time() + # If we're adding days (evaluating today), and we're providing time, + # we must ensure it's within the window's time range, or at least + # the day range hasn't passed yet + if not days_to_add: + if not isinstance(from_date, datetime): + return from_datetime + elif start_time <= from_datetime_tz_aware.time() <= end_time: + return from_datetime + elif from_datetime_tz_aware.time() > end_time: + continue + # Otherwise, since we're looking at days ahead, simply pick the + # window's start time + return ( + datetime.combine(next_date, start_time) + .astimezone(pytz.utc) + .replace(tzinfo=None) + ) + else: # pragma: no cover + raise ValueError( + self.env._( + "Invalid delivery time preference: %s", + self.delivery_time_preference, + ) + ) + raise UserError(self.env._("No available delivery date found")) diff --git a/sale_stock_partner_delivery_window/models/sale_order_line.py b/sale_stock_partner_delivery_window/models/sale_order_line.py new file mode 100644 index 000000000000..c150b47d8f21 --- /dev/null +++ b/sale_stock_partner_delivery_window/models/sale_order_line.py @@ -0,0 +1,24 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + def _expected_date(self): + # OVERRIDE to account for the partner's delivery schedule preference + # The original method, called with `super()`, returns the earliest deliverable + # date based on our customer lead time. + # We want to pick up on that and compare against the partner's delivery schedule + # preference. If it doesn't match, we will postpone the expected date to the + # next available time window. + expected_date = super()._expected_date() + partner = self.order_id.partner_id + return partner._next_available_delivery_date(expected_date) + + @api.depends("order_id.partner_id") + def _compute_qty_at_date(self): + # OVERRIDE to add the `partner_id` to the dependencies + return super()._compute_qty_at_date() diff --git a/sale_stock_partner_delivery_window/pyproject.toml b/sale_stock_partner_delivery_window/pyproject.toml new file mode 100644 index 000000000000..4231d0cccb3d --- /dev/null +++ b/sale_stock_partner_delivery_window/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_stock_partner_delivery_window/readme/CONTRIBUTORS.md b/sale_stock_partner_delivery_window/readme/CONTRIBUTORS.md new file mode 100644 index 000000000000..948474c5cc5b --- /dev/null +++ b/sale_stock_partner_delivery_window/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- [Camptocamp](https://www.camptocamp.com) + - Iván Todorovich \<\> + - Gaëtan Vaujour \<\> diff --git a/sale_stock_partner_delivery_window/readme/DESCRIPTION.md b/sale_stock_partner_delivery_window/readme/DESCRIPTION.md new file mode 100644 index 000000000000..8f756e9bd2b8 --- /dev/null +++ b/sale_stock_partner_delivery_window/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +Use the partner's "Delivery schedule preference" to compute the Sales Order's +Expected Date, which ends up being propagated to the Delivery. diff --git a/sale_stock_partner_delivery_window/static/description/index.html b/sale_stock_partner_delivery_window/static/description/index.html new file mode 100644 index 000000000000..919cb2c3cd88 --- /dev/null +++ b/sale_stock_partner_delivery_window/static/description/index.html @@ -0,0 +1,436 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Sale Stock Partner Delivery Window

+ +

Beta License: AGPL-3 OCA/stock-logistics-workflow Translate me on Weblate Try me on Runboat

+

Use the partner’s “Delivery schedule preference” to compute the Sales +Order’s Expected Date, which ends up being propagated to the Delivery.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

ivantodorovich

+

This module is part of the OCA/stock-logistics-workflow project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/sale_stock_partner_delivery_window/tests/__init__.py b/sale_stock_partner_delivery_window/tests/__init__.py new file mode 100644 index 000000000000..b8e6040719da --- /dev/null +++ b/sale_stock_partner_delivery_window/tests/__init__.py @@ -0,0 +1 @@ +from . import test_partner_delivery_window diff --git a/sale_stock_partner_delivery_window/tests/test_partner_delivery_window.py b/sale_stock_partner_delivery_window/tests/test_partner_delivery_window.py new file mode 100644 index 000000000000..b500e0c946a1 --- /dev/null +++ b/sale_stock_partner_delivery_window/tests/test_partner_delivery_window.py @@ -0,0 +1,204 @@ +# Copyright 2026 Camptocamp SA (https://www.camptocamp.com). +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from freezegun import freeze_time + +from odoo import Command, fields + +from odoo.addons.stock_partner_delivery_window.tests.common import ( + PartnerDeliveryWindowCommon, +) + + +class TestSalePartnerDeliveryWindow(PartnerDeliveryWindowCommon): + @classmethod + def _create_order(cls, partner): + return cls.env["sale.order"].create( + { + "partner_id": partner.id, + "order_line": [ + Command.create( + {"product_id": cls.product.id, "product_uom_qty": 1} + ), + ], + } + ) + + @freeze_time("2020-04-02 10:00:00") # Thursday + def test_expected_date_anytime(self): + """Customer with no delivery preferences. + + Expected date = order creation time (no delays). + """ + order = self._create_order(self.customer_anytime) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-02 10:00:00", + "The same day is fine", + ) + + @freeze_time("2020-04-02 10:00:00") # Thursday + def test_expected_date_anytime_with_sale_delay(self): + """Customer with no preferences + product sale delay. + + Expected date = order date + sale_delay (2 days: Thu → Sat). + """ + self.product.sale_delay = 2 + order = self._create_order(self.customer_anytime) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-04 10:00:00", + "2 days after, because of the sale delay", + ) + + @freeze_time("2020-04-02 10:00:00") # Thursday + def test_expected_date_working_days_ok(self): + """Working-days-only customer, order on weekday (Thursday). + + Expected date = same day (already a valid working day). + """ + order = self._create_order(self.customer_working_days) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-02 10:00:00", + "The same day is fine", + ) + + @freeze_time("2020-04-04 10:00:00") # Saturday + def test_expected_date_working_days_on_saturday(self): + """Working-days-only customer, order on Saturday. + + Expected date = next Monday (first available working day). + """ + order = self._create_order(self.customer_working_days) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-06 10:00:00", + "Next Monday is the first available working day", + ) + + @freeze_time("2020-04-05 10:00:00") # Sunday + def test_expected_date_working_days_on_sunday(self): + """Working-days-only customer, order on Sunday. + + Expected date = next Monday (first available working day). + """ + order = self._create_order(self.customer_working_days) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-06 10:00:00", + "Next Monday is the first available working day", + ) + + @freeze_time("2020-04-02 10:00:00") # Thursday + def test_expected_date_working_days_with_sale_delay(self): + """Working-days-only customer + sale delay. + + • Order on Thursday, sale_delay = 2 days (Thursday → Saturday) + • Expected date = next Monday (first available working day) + """ + self.product.sale_delay = 2 + order = self._create_order(self.customer_working_days) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-06 10:00:00", + "Next Monday is the first available working day, after the sale delay", + ) + + @freeze_time("2020-04-02 10:00:00") # Thursday + def test_expected_date_time_windows_same_day(self): + """Time-window customer, order on valid delivery day (Thursday). + + Expected date = same day at current time. + """ + order = self._create_order(self.customer_time_window) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-02 10:00:00", + "The same day is fine", + ) + + @freeze_time("2020-04-03 10:00:00") # Friday + def test_expected_date_time_windows_next_available_day(self): + """Time-window customer, order on invalid day (Friday). + + • Delivery windows: Thursdays and Saturdays + • Expected date = next Saturday at 00:00 + """ + order = self._create_order(self.customer_time_window) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-04 00:00:00", + "Saturday is the next available day (Thursdays and Saturdays deliveries)", + ) + + @freeze_time("2020-04-02 10:00:00") # Thursday + def test_expected_date_time_windows_next_available_time(self): + """Time-window customer, order before delivery window (10am, window 2pm-6pm). + + Expected date = same day at window start (2pm), not next day. + """ + # The current day is ok, but not the time slot + # Expected date must be delayed just for a few hours + self.customer_time_window.delivery_time_window_ids.write( + {"time_window_start": 14.0, "time_window_end": 18.0} + ) + order = self._create_order(self.customer_time_window) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-02 14:00:00", + "The next available time slot is 2pm to 6pm", + ) + + @freeze_time("2020-04-02 20:00:00") # Thursday + def test_expected_date_time_windows_no_available_time(self): + """Time-window customer, order after delivery window (8pm, window 2pm-6pm). + + Expected date = next delivery day (Saturday) at window start (2pm). + """ + # The current day is ok, but not the time slot + # However, the slots have already passed for the day + self.customer_time_window.delivery_time_window_ids.write( + {"time_window_start": 14.0, "time_window_end": 18.0} + ) + order = self._create_order(self.customer_time_window) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-04 14:00:00", + "The next available day is Saturday from 2pm to 6pm", + ) + + @freeze_time("2020-04-02 10:00:00") # Thursday + def test_expected_date_time_windows_with_sale_delay(self): + """Time-window customer + sale delay. + + • Order on Thursday, sale_delay = 3 days (Thursday → Sunday) + • Expected date = next Thursday (next delivery day) + """ + self.product.sale_delay = 3 # Sunday + order = self._create_order(self.customer_time_window) + self.assertEqual( + fields.Datetime.to_string(order.expected_date), + "2020-04-09 00:00:00", + "Thursday is the next available day, after the sale delay", + ) + + @freeze_time("2020-04-02 10:00:00") # Thursday + def test_no_warning_on_picking_scheduled_date(self): + """Verify picking scheduled date matches order expected date. + + • Order confirmed with delivery window logic + • Picking scheduled_date = order expected_date + • No delivery window warning raised + """ + self.product.sale_delay = 3 # Sunday + order = self._create_order(self.customer_time_window) + # Same as test_expected_date_time_windows_with_sale_delay + # order.expected_date = "2020-04-09 00:00:00" + order.action_confirm() + self.assertEqual( + fields.Datetime.to_string(order.picking_ids.scheduled_date), + "2020-04-09 00:00:00", + "The scheduled date is the expected date", + ) + self.assertFalse(order.picking_ids.partner_delivery_window_warning) diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 000000000000..80f8693f9700 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,3 @@ +odoo-addon-partner_tz @ git+https://github.com/OCA/partner-contact.git@refs/pull/2252/head#subdirectory=partner_tz +odoo-addon-base_time_window @ git+https://github.com/OCA/server-tools.git@refs/pull/3490/head#subdirectory=base_time_window +odoo-addon-stock_partner_delivery_window @ git+https://github.com/OCA/stock-logistics-workflow.git@refs/pull/2219/head#subdirectory=stock_partner_delivery_window