period = $period; } /** * Execute the job. */ public function handle() { // Lấy tất cả người dùng $users = User::all(); foreach ($users as $key => $user) { // if($user->id != 2){ // continue; // } // Kiểm tra dựa trên period (Sáng 'S' hoặc Chiều 'C') if ($this->period === 'S') { $this->checkMorning($user->id); } elseif ($this->period === 'C') { $this->checkAfternoon($user->id); } else { // Nếu không có period, kiểm tra cả sáng và chiều $this->checkMorning($user->id); $this->checkAfternoon($user->id); } } } /** * Kiểm tra check-in/check-out buổi sáng và tạo ticket nếu thiếu. * @param int $userId */ public function checkMorning($userId) { $morningCheckIn = Carbon::createFromTime(7, 30); $morningCheckOut = Carbon::createFromTime(11, 30); $checkDeadline = Carbon::createFromTime(12, 0); // Hạn kiểm tra cho buổi sáng $today = Carbon::today(); // Lấy tất cả tracking của user cho buổi sáng (từ 7:30 đến 12:00) $morningRecords = Tracking::where('user_id', $userId) ->whereBetween('time_string', [$today->copy()->setTime(7, 30), $checkDeadline]) ->get(); $hasMorningCheckIn = $morningRecords->where('status', 'check in')->isNotEmpty(); $hasMorningCheckOut = $morningRecords->where('status', 'check out')->isNotEmpty(); // Nếu không có check-in hoặc check-out, tạo ticket if (!$hasMorningCheckIn && !$hasMorningCheckOut) { Ticket::create([ 'user_id' => $userId, 'start_date' => $today->format('Y-m-d'), 'start_period' => 'S', // Morning 'end_date' => $today->format('Y-m-d'), 'end_period' => 'S', // Morning 'type' => 'UNAUTHORIZEDLEAVE', 'reason' => 'No check in/out for morning', 'status' => 'WAITING', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now() ]); } } /** * Kiểm tra check-in/check-out buổi chiều và tạo ticket nếu thiếu. * @param int $userId */ public function checkAfternoon($userId) { $afternoonCheckIn = Carbon::createFromTime(13, 0); $afternoonCheckOut = Carbon::createFromTime(17, 0); $checkDeadline = Carbon::createFromTime(17, 30); // Hạn kiểm tra cho buổi chiều $today = Carbon::today(); // Lấy tất cả tracking của user cho buổi chiều (từ 13:00 đến 17:30) $afternoonRecords = Tracking::where('user_id', $userId) ->whereBetween('time_string', [$today->copy()->setTime(13, 0), $checkDeadline]) ->get(); $hasAfternoonCheckIn = $afternoonRecords->where('status', 'check in')->isNotEmpty(); $hasAfternoonCheckOut = $afternoonRecords->where('status', 'check out')->isNotEmpty(); // Nếu không có check-in hoặc check-out, tạo ticket if (!$hasAfternoonCheckIn && !$hasAfternoonCheckOut) { Ticket::create([ 'user_id' => $userId, 'start_date' => $today->format('Y-m-d'), 'start_period' => 'C', // Afternoon 'end_date' => $today->format('Y-m-d'), 'end_period' => 'C', // Afternoon 'type' => 'UNAUTHORIZEDLEAVE', 'reason' => 'No check in/out for afternoon', 'status' => 'WAITING', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now() ]); } } }