feat: Add PayrollSeeder to populate payroll data for users with Admin and Leader roles
This commit is contained in:
parent
a9dd91fefd
commit
de10185684
@ -53,6 +53,7 @@ public function run(): void
|
|||||||
ArticleSeeder::class,
|
ArticleSeeder::class,
|
||||||
OrderSeeder::class,
|
OrderSeeder::class,
|
||||||
ExpenseSeeder::class,
|
ExpenseSeeder::class,
|
||||||
|
PayrollSeeder::class,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
56
database/seeders/PayrollSeeder.php
Normal file
56
database/seeders/PayrollSeeder.php
Normal 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),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user