cronjob update status otomatis
This commit is contained in:
77
app/Console/Commands/AutoUpdateLogStatus.php
Normal file
77
app/Console/Commands/AutoUpdateLogStatus.php
Normal 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));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user