feat: Add PayrollSeeder to populate payroll data for users with Admin and Leader roles

This commit is contained in:
Yoga Pangestu 2026-04-24 13:14:55 +07:00
parent a9dd91fefd
commit de10185684
2 changed files with 57 additions and 0 deletions

View File

@ -53,6 +53,7 @@ public function run(): void
ArticleSeeder::class,
OrderSeeder::class,
ExpenseSeeder::class,
PayrollSeeder::class,
]);
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace Database\Seeders;
use App\Enums\IsPaid;
use App\Models\Payroll;
use App\Models\PayrollAdjustment;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
class PayrollSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$users = User::whereHas('roles', function ($q) {
$q->whereIn('name', ['Admin', 'Leader']);
})->get();
if ($users->isEmpty()) {
return;
}
$now = Carbon::now();
foreach ($users as $user) {
// 12 months backward including current month
for ($i = 0; $i < 12; $i++) {
$month = $now->copy()->subMonths($i);
$isCurrentMonth = $i === 0;
$payroll = Payroll::factory()
->for($user)
->withPeriodMonth($month->format('Y-m'))
->create([
'is_paid' => $isCurrentMonth ? IsPaid::NOT_PAID : IsPaid::PAID,
'paid_at' => $isCurrentMonth ? null : $month->copy()->addDays(28),
'created_at' => $month->copy()->addDays(20),
'updated_at' => $month->copy()->addDays(20),
]);
// Create some adjustments
PayrollAdjustment::factory()
->count(rand(0, 3))
->create([
'payroll_id' => $payroll->id,
'created_at' => $month->copy()->addDays(22),
'updated_at' => $month->copy()->addDays(22),
]);
}
}
}
}