86 lines
2.8 KiB
PHP
86 lines
2.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;
|
|
use Carbon\Carbon;
|
|
|
|
class PropagateRosterSeeder extends Seeder
|
|
{
|
|
public function run()
|
|
{
|
|
$jsonPath = database_path('data/employee_patterns.json');
|
|
|
|
if (!File::exists($jsonPath)) {
|
|
$this->command->error("Patterns file not found.");
|
|
return;
|
|
}
|
|
|
|
$patterns = json_decode(File::get($jsonPath), true);
|
|
|
|
// Months to seed: April and May 2026
|
|
$months = [
|
|
['year' => 2026, 'month' => 4],
|
|
['year' => 2026, 'month' => 5],
|
|
];
|
|
|
|
$employees = Employee::all();
|
|
$total = $employees->count();
|
|
|
|
foreach ($months as $m) {
|
|
$startDate = Carbon::create($m['year'], $m['month'], 1);
|
|
$endDate = $startDate->copy()->endOfMonth();
|
|
|
|
$this->command->info("Propagating patterns into " . $startDate->format('F Y') . "...");
|
|
$this->command->getOutput()->progressStart($total);
|
|
|
|
foreach ($employees as $emp) {
|
|
$pattern = $patterns[$emp->employee_id] ?? null;
|
|
|
|
if (!$pattern) {
|
|
$this->command->getOutput()->progressAdvance();
|
|
continue;
|
|
}
|
|
|
|
$current = $startDate->copy();
|
|
while ($current->lte($endDate)) {
|
|
$pyDow = $current->dayOfWeekIso - 1; // 0=Mon, 6=Sun
|
|
$assignment = $pattern[$pyDow] ?? null;
|
|
|
|
if ($assignment) {
|
|
$shift = null;
|
|
if ($assignment['shift']) {
|
|
$shift = Shift::where('name', $assignment['shift'])->first();
|
|
}
|
|
|
|
AttendanceRecord::updateOrCreate(
|
|
[
|
|
'employee_id' => $emp->user_id,
|
|
'date' => $current->toDateString()
|
|
],
|
|
[
|
|
'shift_id' => $shift ? $shift->id : null,
|
|
'is_rest_day' => $assignment['is_rd'],
|
|
'status' => $assignment['is_rd'] ? 'rest_day' : 'absent',
|
|
'created_by' => 1
|
|
]
|
|
);
|
|
}
|
|
$current->addDay();
|
|
}
|
|
|
|
$this->command->getOutput()->progressAdvance();
|
|
}
|
|
|
|
$this->command->getOutput()->progressFinish();
|
|
}
|
|
|
|
$this->command->info("Propagated roster for April and May 2026 successfully.");
|
|
}
|
|
}
|