feat: add local agent logging and bulk sync overwrite option

This commit is contained in:
2026-05-25 11:52:10 +08:00
parent ce91ed0346
commit 02632bb92a
6 changed files with 165 additions and 44 deletions

View File

@@ -165,6 +165,8 @@ class BiometricAttendanceController extends Controller
return redirect()->back()->with('error', __('Permission Denied.'));
}
$overwrite = $request->boolean('overwrite', false);
// Fetch all pending records
$pendingRecords = \App\Models\BiometricAttendance::where('sync_status', 'pending')->get();
@@ -173,6 +175,8 @@ class BiometricAttendanceController extends Controller
});
$syncedCount = 0;
$updatedCount = 0;
foreach ($groupedAttendances as $key => $dayEntries) {
$sorted = $dayEntries->sortBy('punch_time');
$firstEntry = $sorted->first();
@@ -185,8 +189,36 @@ class BiometricAttendanceController extends Controller
$clockInTime = $firstEntry->punch_time->format('H:i:s');
$clockOutTime = $lastEntry->punch_time->format('H:i:s');
$exists = AttendanceRecord::where('employee_id', $employee->user_id)->where('date', $attedanceDate)->whereIn('created_by', getCompanyAndUsersId())->exists();
if (!$exists) {
$attendance = AttendanceRecord::where('employee_id', $employee->user_id)
->where('date', $attedanceDate)
->whereIn('created_by', getCompanyAndUsersId())
->first();
if ($attendance) {
if ($overwrite) {
// Overwrite existing record
$shift = Shift::where('id', $employee->shift_id)->where('status', 'active')->first() ?? Shift::whereIn('created_by', getCompanyAndUsersId())->where('status', 'active')->first();
$policy = AttendancePolicy::where('id', $employee->attendance_policy_id)->where('status', 'active')->first() ?? AttendancePolicy::whereIn('created_by', getCompanyAndUsersId())->where('status', 'active')->first();
$attendance->biometric_id = $firstEntry->id;
$attendance->branch_id = $firstEntry->branch_id;
$attendance->shift_id = $shift?->id;
$attendance->attendance_policy_id = $policy?->id;
$attendance->clock_in = $clockInTime;
$attendance->clock_out = $clockOutTime;
$attendance->save();
$attendance->fresh();
$attendance->processAttendance();
$updatedCount++;
}
// Mark as synced so they don't pile up in pending
foreach ($dayEntries as $entry) {
$entry->update(['sync_status' => 'synced']);
}
} else {
$shift = Shift::where('id', $employee->shift_id)->where('status', 'active')->first() ?? Shift::whereIn('created_by', getCompanyAndUsersId())->where('status', 'active')->first();
$policy = AttendancePolicy::where('id', $employee->attendance_policy_id)->where('status', 'active')->first() ?? AttendancePolicy::whereIn('created_by', getCompanyAndUsersId())->where('status', 'active')->first();
@@ -211,15 +243,16 @@ class BiometricAttendanceController extends Controller
}
$syncedCount++;
} else {
// Already exists, just mark as synced so they don't pile up in pending
foreach ($dayEntries as $entry) {
$entry->update(['sync_status' => 'synced']);
}
}
}
}
return redirect()->back()->with('success', __("Bulk sync completed. Synced {$syncedCount} new records."));
$message = __("Bulk sync completed. Synced :synced new records.", ['synced' => $syncedCount]);
if ($updatedCount > 0) {
$message .= " " . __("Overwrote :updated existing records.", ['updated' => $updatedCount]);
}
return redirect()->back()->with('success', $message);
}
public function sync(Request $request, $id)