feat: implement Payroll and PayrollAdjustment factories and seeders for database initialization
This commit is contained in:
parent
f84173e8aa
commit
cf4e2f5d10
33
database/factories/PayrollAdjustmentFactory.php
Normal file
33
database/factories/PayrollAdjustmentFactory.php
Normal 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),
|
||||
];
|
||||
}
|
||||
}
|
||||
38
database/factories/PayrollFactory.php
Normal file
38
database/factories/PayrollFactory.php
Normal 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -20,6 +20,7 @@ public function run(): void
|
||||
ExpenseSeeder::class,
|
||||
PurchaseSeeder::class,
|
||||
OrderSeeder::class,
|
||||
PayrollSeeder::class,
|
||||
GeneralSettingSeeder::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\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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user