fix shift

This commit is contained in:
2026-04-20 15:50:57 +08:00
parent 5a059a49fe
commit c5b3a4623c
415 changed files with 14547 additions and 14402 deletions

View File

@@ -240,16 +240,21 @@ class ShiftController extends Controller
$usersRaw = $query->get();
// Fetch daily shifts and REST DAY overrides dynamically directly from the Attendance engine records!
$dailyShifts = \App\Models\AttendanceRecord::with('shift')
$dailyShifts = \App\Models\AttendanceRecord::withoutGlobalScopes()
->with('shift')
->whereIn('employee_id', $usersRaw->pluck('id'))
->whereBetween('date', [$startDate->format('Y-m-d'), $endDate->format('Y-m-d')])
->get()
->groupBy('employee_id');
->groupBy(function($item) {
return (int) $item->employee_id;
});
$users = $usersRaw->map(function($user) use ($dates, $dailyShifts) {
$grid = [];
$userShifts = $dailyShifts->has($user->id) ? $dailyShifts[$user->id]->keyBy(function($item) {
return \Carbon\Carbon::parse($item->date)->format('Y-m-d');
$userId = (int) $user->id;
$userShifts = $dailyShifts->has($userId) ? $dailyShifts[$userId]->keyBy(function($item) {
// Force the date to the beginning of the day in Y-m-d format for exact mapping
return \Carbon\Carbon::parse($item->date)->startOfDay()->format('Y-m-d');
}) : collect();
foreach ($dates as $d) {
@@ -266,28 +271,48 @@ class ShiftController extends Controller
$shiftName = 'Rest Day';
$shiftId = null;
} else if ($record->shift) {
// Extract shift safely from relationship
$status = strtoupper(substr($record->shift->name, 0, 1));
$shiftName = $record->shift->name;
// Map shift standard letters based on name
$name = $record->shift->name;
if (stripos($name, 'Morning') !== false) $status = 'M';
else if (stripos($name, 'Evening') !== false) $status = 'E';
else if (stripos($name, 'Night') !== false) $status = 'N';
else $status = strtoupper(substr($name, 0, 1));
$shiftName = $name;
$shiftId = $record->shift->id;
$startTime = $record->shift->start_time;
$endTime = $record->shift->end_time;
} 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));
$shiftName = $user->employee->shift->name;
$name = $user->employee->shift->name;
if (stripos($name, 'Morning') !== false) $status = 'M';
else if (stripos($name, 'Evening') !== false) $status = 'E';
else if (stripos($name, 'Night') !== false) $status = 'N';
else $status = strtoupper(substr($name, 0, 1));
$shiftName = $name;
$shiftId = $user->employee->shift->id;
$startTime = $user->employee->shift->start_time;
$endTime = $user->employee->shift->end_time;
}
} else if ($user->employee && $user->employee->shift) {
// Completely blank record; safely map to their default assigned Employee Shift!
// Removed hardcoded Sat/Sun filters because `attendance_records` strictly dictates actual weekend behavior now!
$status = strtoupper(substr($user->employee->shift->name, 0, 1));
$shiftName = $user->employee->shift->name;
$name = $user->employee->shift->name;
if (stripos($name, 'Morning') !== false) $status = 'M';
else if (stripos($name, 'Evening') !== false) $status = 'E';
else if (stripos($name, 'Night') !== false) $status = 'N';
else $status = strtoupper(substr($name, 0, 1));
$shiftName = $name;
$shiftId = $user->employee->shift->id;
$startTime = $user->employee->shift->start_time;
$endTime = $user->employee->shift->end_time;
}
$grid[$dateKey] = [
'status' => $status,
'shift_name' => $shiftName,
'shift_id' => $shiftId ?? ''
'shift_id' => $shiftId ?? '',
'start_time' => $startTime ?? null,
'end_time' => $endTime ?? null
];
}
@@ -336,19 +361,30 @@ class ShiftController extends Controller
$period = \Carbon\CarbonPeriod::create($startDate, $endDate);
$companyId = getCompanyId(creatorId());
foreach ($period as $date) {
\App\Models\AttendanceRecord::updateOrCreate(
$isRestDay = $validated['is_rest_day'] ?? false;
$targetShiftId = $isRestDay ? null : ($validated['shift_id'] ?? null);
$record = \App\Models\AttendanceRecord::updateOrCreate(
[
'employee_id' => $validated['employee_id'],
'date' => $date->format('Y-m-d'),
],
[
'shift_id' => $validated['is_rest_day'] ? null : $validated['shift_id'],
'is_rest_day' => $validated['is_rest_day'] ?? false,
'status' => $validated['is_rest_day'] ? 'absent' : 'present', // Default status for scheduled shift
'created_by' => creatorId(),
'shift_id' => $targetShiftId,
'is_rest_day' => $isRestDay,
'status' => $isRestDay ? 'absent' : 'present', // Default status for scheduled shift
'attendance_policy_id' => ($validated['attendance_policy_id'] ?? null),
'created_by' => $companyId, // Ensure it's owned by the company for visibility
]
);
\Illuminate\Support\Facades\Log::info("Shift Record Updated", [
'id' => $record->id,
'is_dirty' => $record->wasRecentlyCreated || $record->wasChanged(),
'data' => $record->toArray()
]);
}
return redirect()->back()->with('success', __('Shifts assigned successfully.'));