[FIX] website_sale_aplicoop: harden group order cron

This commit is contained in:
snt 2026-03-31 19:36:20 +02:00
parent 813c8071d9
commit 331a2e8944
4 changed files with 126 additions and 70 deletions

View file

@ -645,16 +645,65 @@ class GroupOrder(models.Model):
Only updates orders in 'draft' or 'open' states.
"""
orders = self.search([("state", "in", ["draft", "open"])])
_logger.info("Cron: Updating dates for %d active group orders", len(orders))
cron_started_at = fields.Datetime.now()
_logger.info(
"Cron: Starting group.order date update at %s for %d active orders (ids=%s)",
cron_started_at,
len(orders),
orders.ids,
)
processed_orders = 0
failed_orders = []
for order in orders:
# Confirm BEFORE recomputing dates: cutoff_date still points to the
# current cycle's cutoff (today or past), so the check works correctly.
# After confirmation, recompute dates so they advance to the next cycle.
order._confirm_linked_sale_orders()
order._compute_cutoff_date()
order._compute_pickup_date()
order._compute_delivery_date()
_logger.info("Cron: Date update completed")
before_values = {
"state": order.state,
"period": order.period,
"start_date": order.start_date,
"end_date": order.end_date,
"cutoff_day": order.cutoff_day,
"pickup_day": order.pickup_day,
"cutoff_date": order.cutoff_date,
"pickup_date": order.pickup_date,
"delivery_date": order.delivery_date,
"home_delivery": order.home_delivery,
}
_logger.info(
"Cron: Processing group order %s (%s) with values before recompute: %s",
order.id,
order.name,
before_values,
)
try:
# Confirm BEFORE recomputing dates: cutoff_date still points to the
# current cycle's cutoff (today or past), so the check works correctly.
# After confirmation, recompute dates so they advance to the next cycle.
order._confirm_linked_sale_orders()
order._compute_cutoff_date()
order._compute_pickup_date()
order._compute_delivery_date()
processed_orders += 1
_logger.info(
"Cron: Finished group order %s (%s). Dates after recompute: cutoff=%s, pickup=%s, delivery=%s",
order.id,
order.name,
order.cutoff_date,
order.pickup_date,
order.delivery_date,
)
except Exception:
failed_orders.append(order.id)
_logger.exception(
"Cron: Error while processing group order %s (%s). Initial values were: %s",
order.id,
order.name,
before_values,
)
_logger.info(
"Cron: Date update completed. processed=%d failed=%d failed_ids=%s",
processed_orders,
len(failed_orders),
failed_orders,
)
def _confirm_linked_sale_orders(self):
"""Confirm draft/sent sale orders linked to this group order.
@ -669,13 +718,26 @@ class GroupOrder(models.Model):
today = fields.Date.today()
if not self.cutoff_date:
_logger.warning(
"Cron: Group order %s (%s) has no cutoff_date (state=%s, period=%s, cutoff_day=%s, start_date=%s). Skipping sale order confirmation for this cycle.",
self.id,
self.name,
self.state,
self.period,
self.cutoff_day,
self.start_date,
)
return
# Skip if cutoff hasn't passed yet (the cycle is still open for orders)
if self.cutoff_date >= today:
_logger.info(
"Cron: Skipping group order %s (%s) - cutoff date %s not yet passed",
"Cron: Skipping group order %s (%s) - cutoff date %s not yet passed (today=%s)",
self.id,
self.name,
self.cutoff_date,
today,
)
return