Files
aso/app/Console/Commands/AutoUpdateLogStatus.php

78 lines
2.3 KiB
PHP

<?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', '!=', 'canceled')
->get();
foreach ($cancelLogs as $log) {
RequestLogHistory::create([
'request_log_id' => $log->id,
'old_status' => $log->status,
'new_status' => 'canceled',
'notes' => 'Auto update status via cronjob',
'changed_at' => now(),
]);
$log->update(['status' => 'canceled']);
}
$this->info("Auto update selesai. Billing: " . count($billingLogs) . ", Cancel: " . count($cancelLogs));
}
}