58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\IsPaid;
|
|
use App\Models\Payroll;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
|
|
class GeneratePayrolls extends Command
|
|
{
|
|
protected $signature = 'payroll:generate {--current} {--next}';
|
|
|
|
protected $description = 'Generate payroll data for the current month or the next month. Default: next month.';
|
|
|
|
public function handle(): void
|
|
{
|
|
$useNextMonth = ! $this->option('current');
|
|
|
|
$targetDate = $useNextMonth ? Carbon::now()->addMonth() : Carbon::now();
|
|
$periodMonth = $targetDate->format('Y-m');
|
|
$periodLabel = $targetDate->translatedFormat('F Y');
|
|
|
|
$users = User::whereHas('employee')
|
|
->active()
|
|
->withoutDeveloper()
|
|
->with('employee')
|
|
->get();
|
|
|
|
if ($users->isEmpty()) {
|
|
$this->warn('Tidak ada pegawai.');
|
|
|
|
return;
|
|
}
|
|
|
|
$created = 0;
|
|
foreach ($users as $user) {
|
|
$payroll = Payroll::firstOrCreate([
|
|
'user_id' => $user->id,
|
|
'period_month' => $periodMonth,
|
|
], [
|
|
'base_salary' => $user->employee->base_salary,
|
|
'bonus' => 0,
|
|
'deduction' => 0,
|
|
'total_salary' => $user->employee->base_salary,
|
|
'is_paid' => IsPaid::NOT_PAID,
|
|
]);
|
|
|
|
if ($payroll->wasRecentlyCreated) {
|
|
$created++;
|
|
}
|
|
}
|
|
|
|
$this->info("Payroll untuk bulan {$periodLabel} berhasil digenerate. Total data baru: {$created}");
|
|
}
|
|
}
|