CRM/backend/app/Services/FollowUpReminderService.php

36 خطوط
1.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\FollowUp;
class FollowUpReminderService
{
public function sendDueReminders(): int
{
$count = 0;
FollowUp::with('lead:id,first_name,last_name,company')
->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;
}
}