diff --git a/app/Console/Commands/AutoUpdateLogStatus.php b/app/Console/Commands/AutoUpdateLogStatus.php new file mode 100644 index 00000000..56e20079 --- /dev/null +++ b/app/Console/Commands/AutoUpdateLogStatus.php @@ -0,0 +1,77 @@ +copy()->subMonth(); + $sixMonthsAgo = $now->copy()->subMonths(6); + + // Waiting billing: >1 bulan & <6 bulan + $billingLogs = RequestLog::where('final_log', 0) + ->whereNull('deleted_at') + ->whereBetween('submission_date', [$sixMonthsAgo, $oneMonthAgo]) + ->where('status', '!=', 'waiting_billing') + ->get(); + + foreach ($billingLogs as $log) { + RequestLogHistory::create([ + 'request_log_id' => $log->id, + 'old_status' => $log->status, + 'new_status' => 'waiting_billing', + 'notes' => 'Auto update status via cronjob', + 'changed_at' => now(), + ]); + $log->update(['status' => 'waiting_billing']); + } + + // Cancel: >6 bulan + $cancelLogs = RequestLog::where('final_log', 0) + ->whereNull('deleted_at') + ->where('submission_date', '<', $sixMonthsAgo) + ->where('status', '!=', 'cancel') + ->get(); + + foreach ($cancelLogs as $log) { + RequestLogHistory::create([ + 'request_log_id' => $log->id, + 'old_status' => $log->status, + 'new_status' => 'cancel', + 'notes' => 'Auto update status via cronjob', + 'changed_at' => now(), + ]); + $log->update(['status' => 'cancel']); + } + + $this->info("Auto update selesai. Billing: " . count($billingLogs) . ", Cancel: " . count($cancelLogs)); + + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index d8bc1d29..5489a453 100755 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -16,6 +16,7 @@ class Kernel extends ConsoleKernel protected function schedule(Schedule $schedule) { // $schedule->command('inspire')->hourly(); + $schedule->command('log:auto-update-status')->dailyAt('01:00'); } /** diff --git a/app/Models/RequestLogHistory.php b/app/Models/RequestLogHistory.php new file mode 100644 index 00000000..f96f4974 --- /dev/null +++ b/app/Models/RequestLogHistory.php @@ -0,0 +1,20 @@ +id(); + $table->foreignId('request_log_id')->constrained()->onDelete('cascade'); + $table->string('old_status'); + $table->string('new_status'); + $table->text('notes')->nullable(); + $table->timestamp('changed_at'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('request_log_histories'); + } +};