60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Enums\SalaryAdjustmentType;
|
|
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::all();
|
|
|
|
if ($users->isEmpty()) {
|
|
$users = User::factory(5)->create();
|
|
}
|
|
|
|
// Generate payroll for the last 6 months
|
|
$months = collect(range(0, 5))->map(function ($i) {
|
|
return Carbon::now()->subMonths($i)->format('Y-m');
|
|
});
|
|
|
|
$currentMonth = Carbon::now()->format('Y-m');
|
|
|
|
foreach ($users as $user) {
|
|
foreach ($months as $month) {
|
|
if (Payroll::where('user_id', $user->id)->where('period_month', $month)->exists()) {
|
|
continue;
|
|
}
|
|
|
|
$payroll = Payroll::factory()->create([
|
|
'user_id' => $user->id,
|
|
'period_month' => $month,
|
|
'is_paid' => $month !== $currentMonth,
|
|
]);
|
|
|
|
PayrollAdjustment::factory(rand(1, 3))->create([
|
|
'payroll_id' => $payroll->id,
|
|
]);
|
|
|
|
$bonus = $payroll->adjustments()->where('type', SalaryAdjustmentType::BONUS)->sum('amount');
|
|
$deduction = $payroll->adjustments()->where('type', SalaryAdjustmentType::DEDUCTION)->sum('amount');
|
|
|
|
$payroll->update([
|
|
'bonus' => $bonus,
|
|
'deduction' => $deduction,
|
|
'total_salary' => $payroll->base_salary + $bonus - $deduction,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|