store/app/Console/Commands/SendAttendanceReminder.php

65 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\EmployeeStatus;
use App\Models\Attendance;
use App\Models\Employee;
use App\Services\System\PushNotificationService;
use App\Settings\HrSettings;
use Carbon\Carbon;
use Illuminate\Console\Command;
class SendAttendanceReminder extends Command
{
protected $signature = 'attendance:send-reminder';
protected $description = 'Kirim pengingat presensi ke pegawai 30 menit sebelum jam masuk';
public function handle(PushNotificationService $pushNotificationService): int
{
$settings = app(HrSettings::class);
$scheduledTime = $settings->scheduled_check_in_time;
if ($scheduledTime === '00:00') {
$this->info('Jam masuk kerja belum diatur. Lewatkan.');
return self::SUCCESS;
}
$now = Carbon::now();
$scheduledCheckIn = Carbon::createFromFormat('Y-m-d H:i', $now->format('Y-m-d').' '.$scheduledTime);
$employees = Employee::query()
->where('status', EmployeeStatus::ACTIVE)
->whereNotNull('user_id')
->get();
$sentCount = 0;
foreach ($employees as $employee) {
$alreadyCheckedIn = Attendance::query()
->where('employee_id', $employee->id)
->whereDate('attendance_date', $now->toDateString())
->exists();
if ($alreadyCheckedIn) {
continue;
}
$pushNotificationService->sendToUser(
'⏰ Pengingat Presensi',
"Jangan lupa melakukan presensi masuk sebelum pukul {$scheduledTime}.",
$employee->user_id,
'/admin/hr/attendances',
);
$sentCount++;
}
$this->info("Pengingat presensi dikirim ke {$sentCount} pegawai.");
return self::SUCCESS;
}
}