From 7fc42625a3e72bae44dbf1da0ecb8649cb7ac7b8 Mon Sep 17 00:00:00 2001 From: snt Date: Mon, 30 Mar 2026 19:23:19 +0200 Subject: [PATCH] [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) --- website_sale_aplicoop/models/group_order.py | 30 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/website_sale_aplicoop/models/group_order.py b/website_sale_aplicoop/models/group_order.py index 8bfe7dd..bdfeb44 100644 --- a/website_sale_aplicoop/models/group_order.py +++ b/website_sale_aplicoop/models/group_order.py @@ -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( [