From 1762422e293cbd73b4b41c466e9a706c71fe6fb0 Mon Sep 17 00:00:00 2001 From: ibnu Date: Fri, 1 Sep 2023 01:09:51 +0700 Subject: [PATCH] update logic get actual date from header activity --- app/Http/Controllers/ActivityController.php | 58 ++++++++++++++++++--- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index 0cc4563..03990d9 100644 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -48,9 +48,9 @@ class ActivityController extends Controller $dataHeader->planned_start = isset($dataHeader->planned_start) ? date_format(date_create($dataHeader->planned_start), "Y-m-d H:i:s") : NULL; $dataHeader->planned_end = isset($dataHeader->planned_end) ? date_format(date_create($dataHeader->planned_end), "Y-m-d H:i:s") : NULL; - $actualStart = $this->getActivityFirst($dataHeader->id); + $actualStart = $this->getFirstLastDateActivity($dataHeader->id, "start"); $dataHeader->actual_start = date_format(date_create($actualStart), "Y-m-d"); - $actualEnd = $this->getActivityLast($dataHeader->id); + $actualEnd = $this->getFirstLastDateActivity($dataHeader->id, "end"); $dataHeader->actual_end = isset($actualEnd) ? date_format(date_create($actualEnd), "Y-m-d") : NULL; $dataHeader->type = "header"; @@ -79,9 +79,9 @@ class ActivityController extends Controller $objRow->planned_start = isset($objRow->planned_start) ? date_format(date_create($objRow->planned_start), "Y-m-d H:i:s") : NULL; $objRow->planned_end = isset($objRow->planned_end) ? date_format(date_create($objRow->planned_end), "Y-m-d H:i:s") : NULL; - $actualStart = $this->getActivityFirst($objRow->id); + $actualStart = $this->getFirstLastDateActivity($objRow->id, "start"); $objRow->actual_start = isset($actualStart) ? date_format(date_create($actualStart), "Y-m-d") : NULL; - $actualEnd = $this->getActivityLast($objRow->id); + $actualEnd = $this->getFirstLastDateActivity($objRow->id, "end"); $objRow->actual_end = isset($actualEnd) ? date_format(date_create($actualEnd), "Y-m-d") : NULL; $objRow->progress = $objRow->persentase_progress / 100; @@ -141,10 +141,10 @@ class ActivityController extends Controller } } else { $objRow->type = "project"; - $actualStart = $this->getActivityFirst($objRow->id); + $actualStart = $this->getFirstLastDateActivity($objRow->id, "start"); $objRow->actual_start = isset($actualStart) ? date_format(date_create($actualStart), "Y-m-d") : NULL; - $actualEnd = $this->getActivityLast($objRow->id); + $actualEnd = $this->getFirstLastDateActivity($objRow->id, "end"); $objRow->actual_end = isset($actualEnd) ? date_format(date_create($actualEnd), "Y-m-d") : NULL; } $finalData[] = $objRow; @@ -167,6 +167,52 @@ class ActivityController extends Controller } + public function getFirstLastDateActivity($id, $params){ + if($params == "start"){ + $data = Activity::select('id', 'parent_id', 'name', 'actual_start')->where('parent_id', $id)->get(); + }else{ + $data = Activity::select('id', 'parent_id', 'name', 'actual_end')->where('parent_id', $id)->get(); + } + $dataFinal=[]; + foreach ($data as $val) { + $activity = $this->getchildActivityForActual($val->id, $params); + $dataFinal[] = $val; + $dataFinal = array_merge($dataFinal, $activity); + + } + if($params == "start"){ + $actualStartValues = array_column(array_filter($dataFinal, function($item) { + return isset($item['actual_start']); + }), 'actual_start'); + + $returnActualStartOrEnd = count($actualStartValues) > 0 ? min($actualStartValues) : null; + }else{ + $actualEndValues = array_column(array_filter($dataFinal, function($item) { + return isset($item['actual_end']); + }), 'actual_end'); + + $returnActualStartOrEnd = count($actualEndValues) > 0 ? max($actualEndValues) : null; + } + + // return json_encode(["min"=>$minActualStart, "max"=>$maxActualStart]); + return $returnActualStartOrEnd; + } + + public function getchildActivityForActual($parent, $params){ + if($params == "start"){ + $activity = Activity::select('id', 'actual_start')->where('parent_id', $parent)->get(); + }else{ + $activity = Activity::select('id', 'actual_end')->where('parent_id', $parent)->get(); + } + $temp = []; + foreach($activity as $val1){ + $getChild = $this->getchildActivityForActual($val1->id, $params); + $temp[] = $val1; + $temp = array_merge($temp, $getChild); + } + return $temp; + } + public function getActivityLast($parentId){ $activity = Activity::where('parent_id', $parentId)->orderByRaw('actual_end DESC')->first(); if (!isset($activity)) {