where('status', 'pending') ->where('scheduled_at', '<=', now()) ->orderBy('id') ->chunkById(100, function ($followUps) use (&$count): void { foreach ($followUps as $followUp) { $followUp->update(['is_overdue' => $followUp->scheduled_at->isPast()]); $leadName = $followUp->lead?->full_name ?: ($followUp->lead?->company ?? "لید {$followUp->lead_id}"); $notification = NotificationService::sendOnce( $followUp->user_id, $followUp->is_overdue ? 'پیگیری عقب‌افتاده' : 'یادآوری پیگیری', $followUp->is_overdue ? "پیگیری لید {$leadName} عقب افتاده است" : "موعد پیگیری لید {$leadName} رسیده است", $followUp->is_overdue ? 'overdue_follow_up' : 'follow_up', ['follow_up_id' => $followUp->id, 'lead_id' => $followUp->lead_id], ); if ($notification) { $count++; } } }); return $count; } }