0, // The sum of all tasks that are under review 'total' => 0, // Sum of all tasks 'overdue' => 0, // The sum of all overworked tasks 'spent_time' => 0, // The sum of all the minutes spent on tasks 'estimated_time' => 0, // The sum of all the minutes spent performing tasks is estimated 'over_spent_time' => 0, // The sum of all the minutes spent overworking tasks 'under_spent_time' => 0, // The sum of all the minutes left until estimated time ]; public function index(Request $request, int $business, ?int $project = null) { $builder = DB::table('tasks')->where('business_id','=',$business); if ($project) { $builder->where('project_id', '=', $project); } $tasks = $builder->get(); $tags = DB::table('tag_task') ->whereIn('task_id', $tasks->pluck('id')->toArray()) ->get() ->groupBy('task_id') ->toArray(); $result = []; foreach ($tasks as $task) { $this->addProjects($result, $task); $this->addSprints($result, $task); $this->addSystems($result, $task); $this->addTags($result, $task, $tags[$task->id] ?? []); } return $result; } public function addProjects(&$result, $task) { $key = "projects.{$task->project_id}"; $this->subset($key, $result, $task); } public function addSprints(&$result, $task) { $key = "projects.{$task->project_id}."; $key .= 'sprints.'; $key .= ($task->sprint_id ?? 0) . '.'; $key .= ($task->assignee_id ?? 0) . '.'; $key .= $task->workflow_id . '.'; $key .= $task->status_id; $this->subset($key, $result, $task); } public function addSystems(&$result, $task) { $key = "projects.{$task->project_id}."; $key .= 'systems.'; $key .= ($task->system_id ?? 0) . '.'; $key .= ($task->assignee_id ?? 0) . '.'; $key .= $task->workflow_id . '.'; $key .= $task->status_id; $this->subset($key, $result, $task); } public function addTags(&$result, $task, $tags) { foreach ($tags as $tag) { $key = "projects.{$task->project_id}."; $key .= 'tags.'; $key .= $tag->id . '.'; $key .= ($task->assignee_id ?? 0) . '.'; $key .= $task->workflow_id . '.'; $key .= $task->status_id; $this->subset($key, $result, $task); } } public function subset($key , &$result, $task) { $node = Arr::get($result, $key, $this->map); $node['test'] += $task->ready_to_test; $node['total'] += 1; $node['overdue'] += !$task->on_time; $node['spent_time'] += $task->spent_time; $node['estimated_time'] += $task->estimated_time; $node['over_spent_time'] += $task->estimated_time < $task->spent_time ? $task->spent_time - $task->estimated_time : 0; $node['under_spent_time'] += $task->estimated_time > $task->spent_time ? $task->estimated_time - $task->spent_time : 0; Arr::set($result, $key, $node); } }