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( [