55 lines
1.8 KiB
PHP
55 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\IsPaid;
|
|
use App\Jobs\StorePushNotification;
|
|
use App\Models\Payroll;
|
|
use App\Models\PushNotification;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
|
|
class ClosePayrolls extends Command
|
|
{
|
|
protected $signature = 'payroll:close {--current} {--month= : Target month in Y-m format}';
|
|
|
|
protected $description = 'Mark payrolls as paid for the current month or the specified month. Default: current month.';
|
|
|
|
public function handle(): void
|
|
{
|
|
$targetMonth = $this->option('month') ?: Carbon::now()->format('Y-m');
|
|
|
|
if ($this->option('current')) {
|
|
$targetMonth = Carbon::now()->format('Y-m');
|
|
}
|
|
|
|
$updatedCount = Payroll::where('period_month', $targetMonth)
|
|
->where('is_paid', IsPaid::NOT_PAID)
|
|
->update([
|
|
'is_paid' => IsPaid::PAID,
|
|
'paid_at' => now(),
|
|
]);
|
|
|
|
if ($updatedCount === 0) {
|
|
$this->warn('Tidak ada payroll yang perlu ditutup untuk bulan '.formatDateLocalized($targetMonth, 'F Y').'.');
|
|
|
|
return;
|
|
}
|
|
|
|
$userIds = Payroll::where('period_month', $targetMonth)
|
|
->where('is_paid', IsPaid::PAID)
|
|
->pluck('user_id')
|
|
->unique();
|
|
|
|
StorePushNotification::dispatch(
|
|
PushNotification::whereIn('user_id', $userIds)->get(),
|
|
[
|
|
'title' => 'Gaji Sudah Cair 💸',
|
|
'body' => 'Kerja kerasmu terbayar. Gaji bulan '.formatDateLocalized($targetMonth, 'F Y').' sudah kami transfer. Semoga harimu menyenangkan.',
|
|
],
|
|
);
|
|
|
|
$this->info('Payroll untuk bulan '.formatDateLocalized($targetMonth, 'F Y')." berhasil ditandai selesai. Total: {$updatedCount}");
|
|
}
|
|
}
|