fix: prioritize employee profile branch over raw terminal branch in biometric logs and sync logic, add swap migration

This commit is contained in:
2026-06-05 12:09:44 +08:00
parent 700cae9a27
commit a3166b97d2
3 changed files with 167 additions and 8 deletions

View File

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