[FIX] website_sale_aplicoop: scope draft lookup to active cycle window

The /eskaera/load-draft endpoint was returning previous-cycle drafts when
group_order.cutoff_date was still in the future but group_order.pickup_date
had not yet been recomputed (its compute only depends on pickup_day and
start_date). Both the stale group_order.pickup_date and the old draft's
pickup_date held the same past value, so the exact-pickup-date filter in
_find_recent_draft_order matched the stale draft and the cart was
repopulated with old products immediately after being cleared.

Replace the pickup_date exact-match + current-week fallback with a single
cutoff-anchored window: create_date in [cutoff_date - 6 days, cutoff_date].
Drafts created outside that window belong to a previous cycle and must
not be reused. The change applies to all four callers (load-draft,
clear-cart, save-order, confirm) so the merge/confirm paths also stop
attaching to stale drafts.

Covered by a new regression test that mirrors the production setup
(matching pickup_date, draft create_date backdated 10 days).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
GitHub Copilot 2026-05-29 17:40:48 +02:00
parent 6125515772
commit 3b8cb7582b
2 changed files with 101 additions and 13 deletions

View file

@ -63,28 +63,29 @@ def _get_salesperson_for_order(self, partner):
def _find_recent_draft_order(self, partner_id, group_order, request_obj=None):
"""Return the active-cycle draft sale.order for the partner, or empty.
The active ordering period ends at group_order.cutoff_date and begins
6 days earlier (weekly cycle). Drafts created outside this window belong
to a previous cycle and must not be reused otherwise stale carts come
back when the user re-enters the order page.
"""
req = request_obj or request
from datetime import datetime
from datetime import timedelta
today = datetime.now().date()
start_of_week = today - timedelta(days=today.weekday())
end_of_week = start_of_week + timedelta(days=6)
if not group_order or not group_order.cutoff_date:
return req.env["sale.order"]
period_start = group_order.cutoff_date - timedelta(days=6)
period_end = group_order.cutoff_date
domain = [
("partner_id", "=", partner_id),
("group_order_id", "=", group_order.id),
("state", "=", "draft"),
("create_date", ">=", f"{period_start} 00:00:00"),
("create_date", "<=", f"{period_end} 23:59:59"),
]
if group_order.pickup_date:
domain.append(("pickup_date", "=", group_order.pickup_date))
else:
domain.extend(
[
("create_date", ">=", f"{start_of_week} 00:00:00"),
("create_date", "<=", f"{end_of_week} 23:59:59"),
]
)
return (
req.env["sale.order"].sudo().search(domain, order="create_date desc", limit=1)