feat: implement Payroll and PayrollAdjustment factories and seeders for database initialization

This commit is contained in:
Yoga Pangestu 2026-04-22 22:25:11 +07:00
parent f84173e8aa
commit cf4e2f5d10
4 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace Database\Factories;
use App\Enums\SalaryAdjustmentType;
use App\Models\Payroll;
use App\Models\PayrollAdjustment;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<PayrollAdjustment>
*/
class PayrollAdjustmentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$type = fake()->randomElement(SalaryAdjustmentType::cases());
return [
'payroll_id' => Payroll::factory(),
'type' => $type,
'description' => $type === SalaryAdjustmentType::BONUS
? fake()->randomElement(['Lembur', 'Bonus Target', 'Bonus Tahunan', 'Tunjangan Transport'])
: fake()->randomElement(['Keterlambatan', 'Kasbon', 'BPJS', 'Pajak']),
'amount' => fake()->numberBetween(10000, 200000),
];
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Database\Factories;
use App\Models\Payroll;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Payroll>
*/
class PayrollFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$baseSalary = fake()->numberBetween(2500000, 5000000);
$bonus = fake()->optional(0.7, 0)->passthrough(fake()->numberBetween(100000, 500000));
$deduction = fake()->optional(0.3, 0)->passthrough(fake()->numberBetween(50000, 200000));
$totalSalary = $baseSalary + $bonus - $deduction;
$isPaid = fake()->boolean(80);
return [
'user_id' => User::factory(),
'period_month' => fake()->dateTimeBetween('-1 year', 'now')->format('Y-m'),
'base_salary' => $baseSalary,
'bonus' => $bonus,
'deduction' => $deduction,
'total_salary' => $totalSalary,
'is_paid' => $isPaid,
'paid_at' => $isPaid ? fake()->dateTimeBetween('-1 month', 'now') : null,
];
}
}

View File

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

View File

@ -0,0 +1,56 @@
<?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');
});
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,
]);
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,
]);
}
}
}
}