Files
HRM-System/scratch/sync_legacy_attendance_to_current.php

95 lines
3.0 KiB
PHP

<?php
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
use App\Models\AttendanceRecord;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
// User mappings: Legacy ID => Current ID
$userMap = [
2 => 6, // Ana Regina Mariano
3 => 7, // Rhea Jane Belchez
4 => 8, // Reynah Lyn Guevarra
5 => 9, // Mart Ace Guano
6 => 10, // Dinh Chavez
7 => 11, // Francis Eleazar Londonio
8 => 12, // Paul Rei Paas
9 => 13, // Princess Rose Acosta
10 => 14 // Donna Marie Moreno
];
$startDate = '2026-01-11';
$endDate = '2026-01-25';
echo "Clearing current attendance records for period $startDate to $endDate...\n";
AttendanceRecord::whereBetween('date', [$startDate, $endDate])->delete();
echo "Fetching legacy attendances...\n";
$legacyAttendances = DB::connection('legacy')->table('attendances')
->whereBetween('date', [$startDate, $endDate])
->get();
echo "Found " . $legacyAttendances->count() . " legacy attendance records.\n";
$inserted = 0;
foreach ($legacyAttendances as $la) {
if (!isset($userMap[$la->user_id])) {
continue;
}
$currentEmpId = $userMap[$la->user_id];
// Parse clock times
$clockIn = null;
$clockOut = null;
if ($la->check_in_time) {
$clockIn = Carbon::parse($la->check_in_time)->format('H:i');
}
if ($la->check_out_time) {
$clockOut = Carbon::parse($la->check_out_time)->format('H:i');
}
$isRestDay = ($la->status === 'rest_day' || $la->is_weekend == 1);
$isAbsent = ($la->status === 'absent');
$status = 'present';
if ($isAbsent) {
$status = 'absent';
} elseif ($la->status === 'on_leave') {
$status = 'on_leave';
} elseif ($isRestDay) {
$status = 'absent';
}
// Create Eloquent Model
$record = new AttendanceRecord();
$record->employee_id = $currentEmpId;
$record->date = $la->date;
$record->clock_in = $clockIn;
$record->clock_out = $clockOut;
$record->shift_id = $la->shift_id;
$record->is_late = ($la->late_hours > 0) ? 1 : 0;
$record->late_hours = $la->late_hours ?? 0.00;
$record->is_early_departure = ($la->early_hours > 0) ? 1 : 0;
$record->early_hours = $la->early_hours ?? 0.00;
$record->is_absent = $isAbsent ? 1 : 0;
$record->is_holiday = $la->is_holiday ?? 0;
$record->is_weekend = $la->is_weekend ?? 0;
$record->is_rest_day = $isRestDay ? 1 : 0;
$record->status = $status;
$record->created_by = 1;
// Calculate hours and Night Differential
$record->processAttendance(false); // Do not auto-calculate status, keep our mapped status
// Force set night diff hours from legacy if they exist (though legacy didn't have column, let's recalculate it)
$record->save();
$inserted++;
}
echo "Successfully synced and processed $inserted attendance records to current database! ✅\n";