Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.3 KiB

<?php
namespace App\Console\Commands;
use App\Helpers\MasterFunctionsHelper;
use App\Models\Project;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class ScheduleHealth extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'calculate:ScheduleHealth';
/**
* The console command description.
*
* @var string
*/
protected $description = 'for calculate ScheduleHealth per project';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
// Here is an optimized version of the code using the update method instead of save:
public function handle()
{
echo "\n------------------------------------------\n";
echo "Command Start. \n";
Log::info("Command Start.");
$projects = Project::get();
$totalProjects = $projects->count();
$updatedProjects = [];
Log::info($totalProjects);
foreach ($projects as $index => $project) {
$project->scurve = MasterFunctionsHelper::getSCurve($project->id);
if ($project->scurve && $project->scurve[0]) {
$planningArray = $project->scurve[0]['data']['percentagePlan'];
$actualArray = $project->scurve[0]['data']['percentageReal'];
$planningProgress = !empty($planningArray) ? end($planningArray) : 0;
$actualProgress = !empty($actualArray) ? end($actualArray) : 0;
$selisihProgress = $planningProgress - $actualProgress;
$scheduleHealth = ($selisihProgress > 0 && $selisihProgress <= 20) ? 'warning' : (($selisihProgress == 0) ? 'on-schedule' : 'behind-schedule');
Log::info("Updating project with ID: " . $project->id . " - Schedule Health: " . $scheduleHealth);
$project->update(['schedule_health' => $scheduleHealth]);
$updatedProjects[] = $project->id;
}
}
if ($totalProjects > 0) {
echo "\n------------------------------------------\n";
echo "All projects have been updated.";
}
}
}