Skip to content

Commit

Permalink
sale_delivery_date: Allow to ignore customer delivery preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
mmequignon committed Jan 12, 2024
1 parent 6c6915a commit e81f274
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
9 changes: 7 additions & 2 deletions sale_delivery_date/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,15 @@ def _delivery_date_from_expedition_date(
)
else:
earliest_delivery_date_naive = expedition_date
if not partner:
ignore_partner_window = self.env.context.get(
"sale_delivery_date__ignore_customer_window"
)
if not partner or ignore_partner_window:
# If no partner, do not apply delivery window
return earliest_delivery_date_naive
return self._get_next_open_customer_window(partner, calendar, from_date=earliest_delivery_date_naive)
return self._get_next_open_customer_window(
partner, calendar, from_date=earliest_delivery_date_naive
)


def _get_next_open_customer_window(self, partner, calendar, from_date=None):
Expand Down
9 changes: 8 additions & 1 deletion sale_delivery_date/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ def _get_next_expedition_date(self, from_date):
else:
return from_date

def _get_delivery_date_from_expedition_date(
self, next_expedition_date, partner, calendar, delays
):
return self.env["sale.order.line"]._delivery_date_from_expedition_date(
next_expedition_date, partner, calendar, delays
)

def _compute_expected_delivery_date(self):
"""Computes the expected delivery date.
Expand All @@ -104,7 +111,7 @@ def _compute_expected_delivery_date(self):
delays = record._get_delays()
partner = record.partner_id
calendar = record._get_warehouse_calendar()
delivery_date = sol_model._delivery_date_from_expedition_date(
delivery_date = record._get_delivery_date_from_expedition_date(
next_expedition_date, partner, calendar, delays
)
record.expected_delivery_date = delivery_date
Expand Down
43 changes: 43 additions & 0 deletions sale_delivery_date/tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,46 @@ def test_get_next_open_customer_window_no_partner_no_calendar(self):
order_line = self.env["sale.order.line"]
res = order_line._get_next_open_customer_window(partner, calendar, from_date=from_date)
self.assertEqual(from_date, res)

def test_get_delivery_date_from_expedition_date(self):
order_line_model = self.env["sale.order.line"]
customer = self.customer_warehouse_cutoff
calendar = self.calendar
order_line_model = self.env["sale.order.line"]
delays = (1, 1, 0) # customer_lead, security_lead, worload
# Customer is set to receive goods on wednesdays between 12 and 17
weekday_numbers = (2,) # Wednesday
time_window_ranges = [(12.00, 17.00), ]
self._set_partner_time_window(
self.customer_warehouse_cutoff, weekday_numbers, time_window_ranges
)
# For a customer with delivery preferences set to wednesdays,
# any date returned by _delivery_date_from_expedition_date should be a
# wednesday
delivery_date = order_line_model._delivery_date_from_expedition_date(
datetime(2024, 1, 8, 0), # 8th of january
customer, calendar, delays
)
self.assertEqual(str(delivery_date.date()), "2024-01-10")
# If a wednesday is used as expedition date, the next wednesday is returned
delivery_date = order_line_model._delivery_date_from_expedition_date(
datetime(2024, 1, 10, 0), # 8th of january
customer, calendar, delays
)
self.assertEqual(str(delivery_date.date()), "2024-01-17")
# If sale_delivery_date__ignore_customer_window is set in context,
# the next day is returned, instead of the next customer availability
order_line_model = order_line_model.with_context(
sale_delivery_date__ignore_customer_window=True
)
delivery_date = order_line_model._delivery_date_from_expedition_date(
datetime(2024, 1, 8, 0), # 8th of january
customer, calendar, delays
)
self.assertEqual(str(delivery_date.date()), "2024-01-09")
# If a wednesday is used as expedition date, the next day is returned
delivery_date = order_line_model._delivery_date_from_expedition_date(
datetime(2024, 1, 10, 0), # 8th of january
customer, calendar, delays
)
self.assertEqual(str(delivery_date.date()), "2024-01-11")

0 comments on commit e81f274

Please sign in to comment.