[FIX] website_sale_aplicoop: robust cutoff check in _confirm_linked_sale_orders

Three guards before confirming sale orders:
1. Skip if no cutoff_date (no cutoff_day configured): avoids blindly confirming
   all draft orders for group orders without a weekly cycle defined
2. Skip if cutoff_date >= today: cycle still open
3. Skip if cutoff_date >= 7 days ago: stale date from a previous cycle
   (protects against cron gaps and confirms only within 1-6 days after cutoff)
This commit is contained in:
snt 2026-03-30 19:23:19 +02:00
parent cd68e8bb5b
commit 7fc42625a3

View file

@ -667,9 +667,20 @@ class GroupOrder(models.Model):
"""
self.ensure_one()
# Only confirm if cutoff date has strictly passed (cutoff day itself is still open)
today = fields.Date.today()
if self.cutoff_date and self.cutoff_date >= today:
# Skip if no cutoff date - without a cutoff_day there is no weekly cycle
# to close automatically (would confirm ALL draft orders indiscriminately)
if not self.cutoff_date:
_logger.info(
"Cron: Skipping group order %s (%s) - no cutoff_date configured",
self.id,
self.name,
)
return
# Skip if cutoff hasn't passed yet (the day is still open for orders)
if self.cutoff_date >= today:
_logger.info(
"Cron: Skipping group order %s (%s) - cutoff date %s not yet passed",
self.id,
@ -678,6 +689,21 @@ class GroupOrder(models.Model):
)
return
# Skip if cutoff was 7+ days ago: it belongs to an already-processed cycle.
# This guards against stale stored dates (e.g. cron was down for several days)
# and prevents confirming orders that were just created for the NEW cycle.
days_since_cutoff = (today - self.cutoff_date).days
if days_since_cutoff >= 7:
_logger.info(
"Cron: Skipping group order %s (%s) - cutoff date %s is stale "
"(%d days ago, already processed)",
self.id,
self.name,
self.cutoff_date,
days_since_cutoff,
)
return
SaleOrder = self.env["sale.order"].sudo()
sale_orders = SaleOrder.search(
[