diff --git a/models/RecurringTicketModel.php b/models/RecurringTicketModel.php index 58a1cf0..d6dc988 100644 --- a/models/RecurringTicketModel.php +++ b/models/RecurringTicketModel.php @@ -231,9 +231,33 @@ class RecurringTicketModel */ public function toggleActive($recurringId) { - $sql = "UPDATE recurring_tickets SET is_active = NOT is_active WHERE recurring_id = ?"; - $stmt = $this->conn->prepare($sql); - $stmt->bind_param('i', $recurringId); + $recurring = $this->getById($recurringId); + if (!$recurring) { + return ['success' => false]; + } + + $newActive = $recurring['is_active'] ? 0 : 1; + + if ($newActive) { + // Re-enabling: recompute next_run_at from now, as if the schedule + // were freshly created. Otherwise a schedule paused while + // next_run_at was still in the future, then re-enabled after that + // date has passed, would fire immediately on the next cron tick + // instead of waiting for its next natural occurrence. + $nextRun = $this->calculateNextRunTime( + $recurring['schedule_type'], + $recurring['schedule_day'], + $recurring['schedule_time'] + ); + $sql = "UPDATE recurring_tickets SET is_active = ?, next_run_at = ? WHERE recurring_id = ?"; + $stmt = $this->conn->prepare($sql); + $stmt->bind_param('isi', $newActive, $nextRun, $recurringId); + } else { + $sql = "UPDATE recurring_tickets SET is_active = ? WHERE recurring_id = ?"; + $stmt = $this->conn->prepare($sql); + $stmt->bind_param('ii', $newActive, $recurringId); + } + $success = $stmt->execute(); $stmt->close(); return ['success' => $success];