Editing Employee Attendance and Check-ins

This commit is contained in:
2026-10-07 17:51:03 +03:30
parent 694f41968a
commit ce2ce49ffb
7 changed files with 134 additions and 6 deletions
+62 -3
View File
@@ -609,18 +609,38 @@ class AttendanceController extends Controller
}
private function viewerIsAdmin(): bool
{
$viewer = User::where('user_name', $_COOKIE['user_identity'] ?? '')->first();
return $viewer && in_array((string) $viewer->is_admin, ['1', '2'], true);
}
public function profile(Request $request)
{
$username = $request->query('username') ?: $_COOKIE['user_identity'];
$user = User::where('user_name', $username)->first();
return view('attendance.profile', ['user' => $user]);
return view('attendance.profile', [
'user' => $user,
'viewerIsAdmin' => $this->viewerIsAdmin(),
]);
}
public function updateProfile(Request $request)
{
// dd($request->all());
$user = User::where('user_name', $_COOKIE['user_identity'])->first();
$viewerIsAdmin = $this->viewerIsAdmin();
$targetUsername = ($viewerIsAdmin && $request->filled('target_username'))
? $request->input('target_username')
: $_COOKIE['user_identity'];
$user = User::where('user_name', $targetUsername)->first();
if (!$user) {
return redirect()->back()->with('message', [
'color' => '#ff8800ff',
'text' => 'کاربر یافت نشد!'
]);
}
if ($user->password != $request->password) {
$request->validate([
@@ -645,14 +665,53 @@ class AttendanceController extends Controller
$user->is_admin = $request->role == 1 ? 1 : null;
}
$workStartChanged = false;
if ($viewerIsAdmin && $request->has('work_start_time')) {
$raw = trim((string) $this->fa2en_numbers($request->input('work_start_time')));
if ($raw === '') {
$newStart = null;
} elseif (preg_match('/^([01]?\d|2[0-3]):([0-5]\d)$/', $raw, $m)) {
$newStart = sprintf('%02d:%02d:00', (int) $m[1], (int) $m[2]);
} else {
return redirect()->back()->with('message', [
'color' => '#ff4400ff',
'text' => 'ساعت شروع کار نامعتبر است (مثال: 15:00).'
]);
}
$oldStart = $user->work_start_time ? substr((string) $user->work_start_time, 0, 8) : null;
if ($oldStart !== $newStart) {
$user->work_start_time = $newStart;
$workStartChanged = true;
}
}
$user->save();
if ($workStartChanged) {
$this->resyncCurrentMonthLate($user->user_name);
}
return redirect()->back()->with('message', [
'color' => '#00c300ff',
'text' => 'اطلاعات با موفقیت ویرایش شد.'
]);
}
private function resyncCurrentMonthLate(string $userIdentity): void
{
$service = app(LatePenaltyService::class);
$service->forgetWorkStart($userIdentity);
[$monthStart, $monthEnd] = $service->jalaliMonthBounds(Carbon::now());
Attendance::query()
->where('user_identity', $userIdentity)
->whereBetween('entered_at', [$monthStart, $monthEnd])
->orderBy('entered_at')
->get()
->each(fn (Attendance $attendance) => $service->syncAttendance($attendance));
}
private function dailyReport($userId, $userName)
{
$tasks = Tasks::with(['duration'])