farhantock
7 months ago
3 changed files with 95 additions and 22 deletions
@ -0,0 +1,73 @@
|
||||
<?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() |
||||
{ |
||||
$projects = Project::whereNull('deleted_at')->get(); |
||||
$totalProjects = $projects->count(); |
||||
$updatedProjects = []; |
||||
|
||||
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."; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue