parfum/database/seeders/PayrollSeeder.php

57 lines
1.7 KiB
PHP

<?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),
]);
}
}
}
}