Files
HRM-System/database/seeders/LegacyRosterSyncSeeder.php

60 lines
1.8 KiB
PHP

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Employee;
use App\Models\Shift;
use App\Models\AttendanceRecord;
use Illuminate\Support\Facades\File;
class LegacyRosterSyncSeeder extends Seeder
{
public function run()
{
$jsonPath = base_path('scratch/legacy_roster.json');
if (!File::exists($jsonPath)) {
$this->command->error("JSON file not found at $jsonPath");
return;
}
$roster = json_decode(File::get($jsonPath), true);
$total = count($roster);
$count = 0;
$this->command->getOutput()->progressStart($total);
foreach ($roster as $item) {
$employee = Employee::where('employee_id', $item['code'])->first();
if ($employee) {
$shift = null;
if ($item['shift_name']) {
$shift = Shift::where('name', $item['shift_name'])->first();
}
// Create or update the attendance record (Roster)
AttendanceRecord::updateOrCreate(
[
'employee_id' => $employee->user_id, // Attendance uses user_id as employee_id relationship
'date' => $item['date']
],
[
'shift_id' => $shift ? $shift->id : null,
'is_rest_day' => $item['is_rest_day'],
'status' => 'absent', // Roster placeholder
'created_by' => 1
]
);
}
$this->command->getOutput()->progressAdvance();
$count++;
}
$this->command->getOutput()->progressFinish();
$this->command->info("Synced $count roster records successfully.");
}
}