From 9ff926765d97eb831b7e4ee0791b0120cc38aa39 Mon Sep 17 00:00:00 2001 From: Muhammad Sulaiman Yusuf Date: Wed, 23 Nov 2022 14:05:58 +0700 Subject: [PATCH] recalc progress proportionally --- app/Models/Activity.php | 16 ++++++++++------ app/Models/ReportActivityMaterial.php | 11 ++++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 0220975..b8da186 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -104,14 +104,18 @@ class Activity extends Model private function updatePersentaseProgress() { - $siblings = Activity::where("parent_id", $this->parent_id); - $sumProgress = $siblings->sum("persentase_progress"); - $totalChild = $siblings->count(); - - $this->persentage_progress = $sumProgress / $totalChild; if($parent = Activity::find($this->parent_id)){ + $parentActWeight = $parent->bobot_planning; + + $totalChildProportionalProgress = 0; + $childs = Activity::where("parent_id", $parent->id)->get(); + foreach($childs as $child){ + $currentActWeight = $child->bobot_planning; + $currentActProportionalProgress = ($currentActWeight / $parentActWeight) * $child->persentase_progress; + $totalChildProportionalProgress += $currentActProportionalProgress; + } $parent->update([ - "persentase_progress" => $sumProgress / $totalChild, + "persentase_progress" => $totalChildProportionalProgress ]); } } diff --git a/app/Models/ReportActivityMaterial.php b/app/Models/ReportActivityMaterial.php index 3caac56..75b0817 100644 --- a/app/Models/ReportActivityMaterial.php +++ b/app/Models/ReportActivityMaterial.php @@ -33,18 +33,23 @@ class ReportActivityMaterial extends Model $activity = Activity::find($data->activity_id); $assignedMaterial = AssignMaterial::find($data->assign_material_id); - $activity->biaya_actual += floatval($assignedMaterial->budget) * floatval($data->qty); + $biayaActual = $activity->biaya_actual + floatval($assignedMaterial->budget) * floatval($data->qty); $dataPlan = AssignMaterial::where('activity_id', $activity->id)->get(); if($dataPlan[0]->status_activity == 'done'){ - $activity->persentase_progress = 100; + $percentage = 100; } else { $totalPlan = $dataPlan->sum('qty_planning'); $totalVolumeActual = ReportActivityMaterial::where('activity_id', '=', $activity->id)->sum("qty"); $percentage = ($totalVolumeActual * 100) / $totalPlan; - $activity->persentase_progress = $percentage >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; + $percentage = $percentage >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; } + $activity->update([ + "persentase_progress" => $percentage, + "biaya_actual" => $biayaActual, + ]); + $activity->save(); });