cronjob update status otomatis

This commit is contained in:
2025-07-18 09:22:33 +07:00
parent b6e8bbb79b
commit 05bed90598
4 changed files with 134 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\RequestLog; // Sesuaikan nama modelnya
use App\Models\RequestLogHistory;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class AutoUpdateLogStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'log:auto-update-status';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update status LOG otomatis berdasarkan umur data';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$now = Carbon::now();
$oneMonthAgo = $now->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));
}
}

View File

@@ -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');
}
/**

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RequestLogHistory extends Model
{
use HasFactory;
protected $fillable = [
'request_log_id',
'old_status',
'new_status',
'notes',
'changed_at',
];
public $timestamps = true;
}

View File

@@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('request_log_histories', function (Blueprint $table) {
$table->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');
}
};