From fb1ad297416ad1dadc5665d209407397ec21be08 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Thu, 30 Apr 2026 14:03:51 +0700 Subject: [PATCH] feat: add automated monthly payroll generation and update unpaid previous month records --- app/Console/Commands/GeneratePayrollCommand.php | 8 ++++++++ routes/console.php | 3 +++ 2 files changed, 11 insertions(+) diff --git a/app/Console/Commands/GeneratePayrollCommand.php b/app/Console/Commands/GeneratePayrollCommand.php index da05cd3..6ee6597 100644 --- a/app/Console/Commands/GeneratePayrollCommand.php +++ b/app/Console/Commands/GeneratePayrollCommand.php @@ -42,6 +42,14 @@ public function handle() try { DB::transaction(function () use ($users, $periodMonth, &$count) { + $previousMonth = Carbon::parse($periodMonth)->subMonth()->format('Y-m'); + Payroll::where('period_month', $previousMonth) + ->where('is_paid', false) + ->update([ + 'is_paid' => true, + 'paid_at' => Carbon::now(), + ]); + foreach ($users as $user) { $exists = Payroll::where('user_id', $user->id) ->where('period_month', $periodMonth) diff --git a/routes/console.php b/routes/console.php index 3c9adf1..233a288 100644 --- a/routes/console.php +++ b/routes/console.php @@ -2,7 +2,10 @@ use Illuminate\Foundation\Inspiring; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\Schedule; Artisan::command('inspire', function () { $this->comment(Inspiring::quote()); })->purpose('Display an inspiring quote'); + +Schedule::command('payroll:generate')->monthlyOn(1, '00:00');