From 5a059a49fef36d54a63b8c62a171e5b043c5fe8d Mon Sep 17 00:00:00 2001 From: dvappnnt Date: Mon, 20 Apr 2026 15:35:59 +0800 Subject: [PATCH] Update ShiftController.php --- app/Http/Controllers/ShiftController.php | 30 ++++++++++-------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/ShiftController.php b/app/Http/Controllers/ShiftController.php index 680ac41d9..e89ad9bfa 100644 --- a/app/Http/Controllers/ShiftController.php +++ b/app/Http/Controllers/ShiftController.php @@ -240,22 +240,17 @@ class ShiftController extends Controller $usersRaw = $query->get(); // Fetch daily shifts and REST DAY overrides dynamically directly from the Attendance engine records! - $dailyShifts = \Illuminate\Support\Facades\DB::table('attendance_records') - ->leftJoin('shifts', 'attendance_records.shift_id', '=', 'shifts.id') - ->whereBetween('attendance_records.date', [$startDate->format('Y-m-d'), $endDate->format('Y-m-d')]) - ->select( - 'attendance_records.employee_id as user_id', - 'attendance_records.date', - 'attendance_records.is_rest_day', - 'attendance_records.shift_id', - 'shifts.name as shift_name' - ) + $dailyShifts = \App\Models\AttendanceRecord::with('shift') + ->whereIn('employee_id', $usersRaw->pluck('id')) + ->whereBetween('date', [$startDate->format('Y-m-d'), $endDate->format('Y-m-d')]) ->get() - ->groupBy('user_id'); + ->groupBy('employee_id'); $users = $usersRaw->map(function($user) use ($dates, $dailyShifts) { $grid = []; - $userShifts = $dailyShifts->has($user->id) ? $dailyShifts[$user->id]->keyBy('date') : collect(); + $userShifts = $dailyShifts->has($user->id) ? $dailyShifts[$user->id]->keyBy(function($item) { + return \Carbon\Carbon::parse($item->date)->format('Y-m-d'); + }) : collect(); foreach ($dates as $d) { $dateKey = $d['date']; @@ -266,16 +261,15 @@ class ShiftController extends Controller if ($userShifts->has($dateKey)) { $record = $userShifts[$dateKey]; - // If attendance record specifically marks it as a globally tracked Rest Day if ($record->is_rest_day) { $status = 'RD'; $shiftName = 'Rest Day'; $shiftId = null; - } else if ($record->shift_name) { - // Extract shift safely - $status = strtoupper(substr($record->shift_name, 0, 1)); - $shiftName = $record->shift_name; - $shiftId = $record->shift_id; + } else if ($record->shift) { + // Extract shift safely from relationship + $status = strtoupper(substr($record->shift->name, 0, 1)); + $shiftName = $record->shift->name; + $shiftId = $record->shift->id; } else if ($user->employee && $user->employee->shift) { // Record exists, but shift mapping is blank. Fallback to employee profile shift. $status = strtoupper(substr($user->employee->shift->name, 0, 1));