From a3166b97d2d71dade43b32ef95be571c74d8535e Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 5 Jun 2026 12:09:44 +0800 Subject: [PATCH] fix: prioritize employee profile branch over raw terminal branch in biometric logs and sync logic, add swap migration --- .../BiometricAttendanceController.php | 16 +-- ...20911_swap_biometric_employee_branches.php | 62 ++++++++++++ docs/PLAN-employee-branch-swap.md | 97 +++++++++++++++++++ 3 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 database/migrations/2026_06_05_120911_swap_biometric_employee_branches.php create mode 100644 docs/PLAN-employee-branch-swap.md diff --git a/app/Http/Controllers/BiometricAttendanceController.php b/app/Http/Controllers/BiometricAttendanceController.php index fc617a7c6..35310c2e2 100644 --- a/app/Http/Controllers/BiometricAttendanceController.php +++ b/app/Http/Controllers/BiometricAttendanceController.php @@ -165,7 +165,7 @@ class BiometricAttendanceController extends Controller // Eager-load employees with shifts to prevent N+1 queries $empIds = $records->pluck('biometric_emp_id')->unique(); - $employees = \App\Models\Employee::with(['user', 'shift']) + $employees = \App\Models\Employee::with(['user', 'shift', 'branch']) ->whereIn('biometric_emp_id', $empIds) ->get() ->keyBy('biometric_emp_id'); @@ -203,7 +203,7 @@ class BiometricAttendanceController extends Controller 'date' => $workDate, 'clock_in' => $clockIn, 'clock_out' => $clockOut, - 'terminal' => $firstEntry->branch ? $firstEntry->branch->name : ($firstEntry->terminal_alias ? trim(str_replace('(Agent)', '', $firstEntry->terminal_alias)) : 'Agent'), + 'terminal' => ($employee && $employee->branch) ? $employee->branch->name : ($firstEntry->branch ? $firstEntry->branch->name : ($firstEntry->terminal_alias ? trim(str_replace('(Agent)', '', $firstEntry->terminal_alias)) : 'Agent')), 'sync_status' => $firstEntry->sync_status, ]; }) @@ -445,7 +445,7 @@ class BiometricAttendanceController extends Controller } $attendance->biometric_id = $firstEntry->id; - $attendance->branch_id = $firstEntry->branch_id; + $attendance->branch_id = $employee->branch_id ?? $firstEntry->branch_id; $attendance->clock_in = $clockInTime; $attendance->clock_out = $clockOutTime; $attendance->save(); @@ -466,7 +466,7 @@ class BiometricAttendanceController extends Controller $attendance = new AttendanceRecord(); $attendance->employee_id = $employee->user_id; - $attendance->branch_id = $firstEntry->branch_id; + $attendance->branch_id = $employee->branch_id ?? $firstEntry->branch_id; $attendance->biometric_id = $firstEntry->id; $attendance->shift_id = $shift?->id; $attendance->attendance_policy_id = $policy?->id; @@ -509,7 +509,7 @@ class BiometricAttendanceController extends Controller $clockInTime = $request->clock_in; $clockOutTime = $request->clock_out; - $employee = Employee::with(['user', 'shift'])->whereIn('created_by', getCompanyAndUsersId())->where('biometric_emp_id', $biometricEmpId)->first(); + $employee = Employee::with(['user', 'shift', 'branch'])->whereIn('created_by', getCompanyAndUsersId())->where('biometric_emp_id', $biometricEmpId)->first(); if ($employee) { // Check if record already exists @@ -548,7 +548,7 @@ class BiometricAttendanceController extends Controller $attendance = new AttendanceRecord(); $attendance->employee_id = $employee->user_id; - $attendance->branch_id = $biometricRecord ? $biometricRecord->branch_id : null; + $attendance->branch_id = $employee->branch_id ?? ($biometricRecord ? $biometricRecord->branch_id : null); $attendance->biometric_id = $biometricId; $attendance->shift_id = $shift?->id; $attendance->attendance_policy_id = $policy?->id; @@ -625,7 +625,7 @@ class BiometricAttendanceController extends Controller if ($attendance) { // Update the existing record since biometrics is the source of truth $attendance->biometric_id = $clockInId; - $attendance->branch_id = $clockInRecord->branch_id; + $attendance->branch_id = $employee->branch_id ?? $clockInRecord->branch_id; if (empty($attendance->shift_id)) { $attendance->shift_id = $shift?->id; } @@ -640,7 +640,7 @@ class BiometricAttendanceController extends Controller $attendance = new AttendanceRecord(); $attendance->employee_id = $employee->user_id; $attendance->biometric_id = $clockInId; - $attendance->branch_id = $clockInRecord->branch_id; + $attendance->branch_id = $employee->branch_id ?? $clockInRecord->branch_id; $attendance->shift_id = $shift?->id; $attendance->attendance_policy_id = $policy?->id; $attendance->date = $attedanceDate; diff --git a/database/migrations/2026_06_05_120911_swap_biometric_employee_branches.php b/database/migrations/2026_06_05_120911_swap_biometric_employee_branches.php new file mode 100644 index 000000000..0c5e9add8 --- /dev/null +++ b/database/migrations/2026_06_05_120911_swap_biometric_employee_branches.php @@ -0,0 +1,62 @@ +