diff --git a/app/Http/Controllers/HierarchyFtthController.php b/app/Http/Controllers/HierarchyFtthController.php index 70c56fb..1a3ca56 100644 --- a/app/Http/Controllers/HierarchyFtthController.php +++ b/app/Http/Controllers/HierarchyFtthController.php @@ -100,9 +100,43 @@ class HierarchyFtthController extends Controller ], 500); } } + + public function countProgress($project_id) { + $ftthIds = VersionGantt::select('hierarchy_ftth_id') + ->where('proyek_id', $project_id) + ->groupBy('hierarchy_ftth_id') + ->get(); + + foreach ($ftthIds as $ftthId) { + $gantts = VersionGantt::where('hierarchy_ftth_id', $ftthId->hierarchy_ftth_id)->sum('progress'); + $ganttCount = VersionGantt::where('hierarchy_ftth_id', $ftthId->hierarchy_ftth_id)->count(); + + $ftth = HierarchyFtth::find($ftthId->hierarchy_ftth_id); + $round = $gantts/$ganttCount; + $ftth->progress = round($round, 2); + $ftth->save(); + + if($ftth->parent_id){ + $this->countParent($ftth); + } + } + } + + public function countParent($ftth){ + $parent = HierarchyFtth::find($ftth->parent_id); + $children = HierarchyFtth::where('parent_id', $ftth->parent_id)->sum('progress'); + $childrenCount = HierarchyFtth::where('parent_id', $ftth->parent_id)->count(); + $round = $children/$childrenCount; + $parent->progress = round($round, 2); + $parent->save(); + if($parent->parent_id) { + $this->countParent($parent); + } + } public function getTreeByProject($project_id) { + $this->countProgress(intval($project_id)); $data = HierarchyFtth::where('project_id', $project_id)->whereNull('parent_id')->orderByRaw('id ASC')->get(); $finalData = []; foreach($data as $objRow){ diff --git a/app/Http/Controllers/VersionGanttController.php b/app/Http/Controllers/VersionGanttController.php index c7b9f65..4dc9450 100644 --- a/app/Http/Controllers/VersionGanttController.php +++ b/app/Http/Controllers/VersionGanttController.php @@ -83,20 +83,9 @@ class VersionGanttController extends Controller $dataBuilder = $this->setUpPayload($payload, 'm_version_gantt'); $builder = $dataBuilder['builder']; // $countBuilder = $dataBuilder['count']; - $progress = $this->ganttProgress($request->columns[0]["name"], $request->columns[0]["value"]); + $this->ganttProgress($request->columns[0]["name"], $request->columns[0]["value"]); $dataGet = $builder->get(); - // Loop through $dataGet collection - foreach ($dataGet as $item) { - // Loop through $progress collection - foreach ($progress as $progressItem) { - // Check if both have the same id - if ($item->id == $progressItem->id) { - // Set the progress of $item to the corresponding progress of $progressItem - $item->progress = $progressItem->persentase_progress; - break; - } - } - } + // $totalRecord = $countBuilder->count(); return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet], 200); } @@ -108,8 +97,12 @@ class VersionGanttController extends Controller ->where('m_activity.type_activity', "project") ->where('m_activity.parent_id', null) ->get(); - - return $progress; + foreach($progress as $item) { + if($item->persentase_progress){ + $item->progress = $item->persentase_progress; + $item->save(); + } + } } public function list() diff --git a/app/Models/HierarchyFtth.php b/app/Models/HierarchyFtth.php index 649f1e5..9a94bfc 100644 --- a/app/Models/HierarchyFtth.php +++ b/app/Models/HierarchyFtth.php @@ -12,6 +12,6 @@ class HierarchyFtth extends Model const UPDATED_AT = 'updated_at'; protected $fillable = [ - 'name', 'parent_id', 'project_id', 'created_at', 'updated_at' + 'name', 'parent_id', 'project_id', 'created_at', 'updated_at', 'progress' ]; }