Update ShiftController.php

This commit is contained in:
2026-04-20 15:35:59 +08:00
parent bfec5d40d9
commit 5a059a49fe

View File

@@ -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));