From 3f8ece62ea142b3c780b48c51bd340ee43f61d3f Mon Sep 17 00:00:00 2001 From: Mmequignon Date: Wed, 9 Aug 2023 08:22:51 +0200 Subject: [PATCH] sale_delivery_date: expected delivery - Always recompute --- sale_delivery_date/models/stock_picking.py | 34 +++++++------------ .../tests/test_delivery_date_in_the_past.py | 7 ++-- sale_delivery_date/tests/test_integration.py | 18 +++++----- 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/sale_delivery_date/models/stock_picking.py b/sale_delivery_date/models/stock_picking.py index e4bc1e06641..cb3d26295a7 100644 --- a/sale_delivery_date/models/stock_picking.py +++ b/sale_delivery_date/models/stock_picking.py @@ -57,30 +57,20 @@ def _compute_expected_delivery_date(self): We still try to keep this priority: commitment_date > expected_date > date_done > scheduled_date """ - today = fields.Date.today() + now = fields.Datetime.now() + sale_line_model = self.env["sale.order.line"] for record in self: - delivery_date = False commitment_date = record.sale_id.commitment_date - expected_date = record.sale_id.expected_date - # If we commited to deliver on a given date, we never fall back - # on the expected_date. - # The reason for that is that we might have set and unrealistic - # commitment_date at first. - # In such case, it's normal to be late, and we do not want to postpone. - if commitment_date: - if commitment_date.date() >= today: - delivery_date = commitment_date - elif expected_date and expected_date.date() >= today: - delivery_date = expected_date - if not delivery_date: - date_done = record.date_done or record.scheduled_date - sale_line_model = self.env["sale.order.line"] - partner = self.partner_id - warehouse = self.location_id.get_warehouse() - delays = self._get_delays() - delivery_date = sale_line_model._delivery_date_from_expedition_date( - date_done, partner, warehouse.calendar2_id, delays - ) + # Recompute everytime, and compare the new expected delivery date + # with the commitment date, as we do not want to deliver early. + partner = record.partner_id + warehouse = record.location_id.get_warehouse() + delays = record._get_delays() + delivery_date = sale_line_model._delivery_date_from_expedition_date( + now, partner, warehouse.calendar2_id, delays + ) + if commitment_date and commitment_date > delivery_date: + delivery_date = commitment_date record.expected_delivery_date = delivery_date @api.depends("location_id") diff --git a/sale_delivery_date/tests/test_delivery_date_in_the_past.py b/sale_delivery_date/tests/test_delivery_date_in_the_past.py index b647e83f391..4542d299df7 100644 --- a/sale_delivery_date/tests/test_delivery_date_in_the_past.py +++ b/sale_delivery_date/tests/test_delivery_date_in_the_past.py @@ -124,11 +124,10 @@ def test_delivery_date_as_late_scheduled_date(self): with freeze_time("2021-10-25"): # picking is handled late, order.commitment_date and # order.expected_date are outdated. - # expected_delivery_date is scheduled date with security_lead and - # customer delivery preferences applied. - # As customer is set to be delivered anytime, midnight is ok. + # expected_delivery_date is postponed from now (as the expedition date) + # with respect to customer's time window. td_security_lead = timedelta(days=picking.company_id.security_lead) - self.assertEqual(str(picking.expected_delivery_date), "2021-10-16 00:00:00") + self.assertEqual(str(picking.expected_delivery_date), "2021-10-26 00:00:00") def test_delivery_date_as_date_done(self): """Date done should be used if both commitment_date and diff --git a/sale_delivery_date/tests/test_integration.py b/sale_delivery_date/tests/test_integration.py index ac7f8375422..cde2d4c0ebe 100644 --- a/sale_delivery_date/tests/test_integration.py +++ b/sale_delivery_date/tests/test_integration.py @@ -195,15 +195,13 @@ def test_order_on_thursday_after_cutoff_late_commitment_date(self): # The scheduled date is the start of the previous attendance. # This is when we should have started working on this. self.assertEqual(str(picking.scheduled_date), "2023-07-04 08:00:00") # 10:00 UTC - # Before confirmation, expected_delivery_date is: - # scheduled_date + security_lead with time_window applied + # date_deadline is immutable. + # However, there might be cases where you cannot use a date in the past, + # a carrier for instance, whose API would refuse any date in the past. + # For suche cases, we can use picking.expected_delivery_date, which represents + # the delivery date, based on now as an expedition date, which takes into + # account customer delivery preferences. + # On 2023-07-06 16:00:00, best delivery date is the day after. self.assertEqual( - str(picking.expected_delivery_date), - "2023-07-05 05:30:00" # 07:30 UTC + str(picking.expected_delivery_date), "2023-07-07 05:30:00" # 07:30 UTC ) - # But once it's confirmed, it's: - # date_done + security_lead with time windows applied - picking.move_lines.quantity_done = 10.0 - picking._action_done() - self.assertEqual(str(picking.expected_delivery_date), "2023-07-07 05:30:00") -