|
@@ -9,6 +9,7 @@ use App\Models\Project;
|
|
|
use App\Models\Requirement;
|
|
|
use App\Models\Task;
|
|
|
use App\Models\User;
|
|
|
+use Carbon\Carbon;
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
class ProjectGanttService
|
|
@@ -26,27 +27,78 @@ class ProjectGanttService
|
|
|
if (! isset($groupTasks[$groupKey])) {
|
|
|
continue;
|
|
|
}
|
|
|
- $group_label = $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey];
|
|
|
- foreach ($groupTasks[$groupKey] as $tasks){
|
|
|
- $tasks->group_label_id=$group_label['id'];
|
|
|
- $tasks->group_label_name=$group_label['name'];
|
|
|
- $tasks->group=$group;
|
|
|
+
|
|
|
+ $treeTasks = make_tree($groupTasks[$groupKey]->toArray());
|
|
|
+
|
|
|
+ $groupUniqueKey = uniqid();
|
|
|
+ $items[] = $this->topGroupFormat(
|
|
|
+ $groupUniqueKey,
|
|
|
+ isset($groupNamesKeyBy[$groupKey]) ? $groupNamesKeyBy[$groupKey]['name'] : "Empty"
|
|
|
+ );
|
|
|
+
|
|
|
+ $tasks = $this->flattenTasks($treeTasks);
|
|
|
+
|
|
|
+ foreach ($tasks as $task) {
|
|
|
+ $items[] = $this->taskFormat($task, $groupUniqueKey);
|
|
|
}
|
|
|
- $items[] = [
|
|
|
-// 'group' => $group,
|
|
|
-// 'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey],
|
|
|
- ProjectGanttResource::collection($groupTasks[$groupKey]),
|
|
|
- ];
|
|
|
- };
|
|
|
- $result=[];
|
|
|
- foreach ($items as $item){
|
|
|
- foreach ($item as $it){
|
|
|
- foreach ($it as $i){
|
|
|
- $result[]= $i;
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $items;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function topGroupFormat(string $id, string $name)
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ 'id' => $id,
|
|
|
+ 'text' => $name,
|
|
|
+ 'parent' => 0,
|
|
|
+ 'start_date' => null,
|
|
|
+ 'duration' => null,
|
|
|
+ 'progress' => 0,
|
|
|
+ 'open' => true,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function taskFormat(array $task, string $topId)
|
|
|
+ {
|
|
|
+ $progress = 0;
|
|
|
+ $begin = $task['begin'] ? Carbon::parse($task['begin']) : Carbon::parse($task['created_at']);
|
|
|
+ $end = $task['end'] ? Carbon::parse($task['end']) : $begin->copy()->addYears(2);
|
|
|
+ $now = Carbon::now();
|
|
|
+
|
|
|
+ if ($now->gt($end)) {
|
|
|
+ $progress = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($now->gt($begin) && $end->gt($now)) {
|
|
|
+ $totalDay = $end->diffInDays($begin);
|
|
|
+ $day = $now->diffInDays($begin);
|
|
|
+ $progress = (float)number_format($day / $totalDay, 4);
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'id' => $task['id'],
|
|
|
+ 'text' => $task['name'],
|
|
|
+ 'parent' => $task['parent_id'] > 0 ? $task['parent_id'] : $topId ,
|
|
|
+ 'start_date' => $task['begin'],
|
|
|
+ 'duration' => $end->diffInDays($begin),
|
|
|
+ 'progress' => $progress,
|
|
|
+ 'open' => true,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function flattenTasks($tasks) {
|
|
|
+ $items = [];
|
|
|
+
|
|
|
+ foreach ($tasks as $task) {
|
|
|
+ $items[] = $task;
|
|
|
+ if (!empty($task['children'])) {
|
|
|
+ $items = array_merge($items, $this->flattenTasks($task['children']));
|
|
|
}
|
|
|
+ unset($task['children']);
|
|
|
}
|
|
|
- return $result;
|
|
|
+
|
|
|
+ return $items;
|
|
|
}
|
|
|
|
|
|
protected function getGroupNamesKeyBy(Collection $groups, string $group): array
|