|
@@ -0,0 +1,63 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\Project;
|
|
|
+
|
|
|
+use App\Http\Resources\API\ProjectGanttResource;
|
|
|
+use App\Http\Resources\API\ProjectGroupViewTaskResource;
|
|
|
+use App\Models\Enums\ProjectStatus;
|
|
|
+use App\Models\Project;
|
|
|
+use App\Models\Requirement;
|
|
|
+use App\Models\Task;
|
|
|
+use App\Models\User;
|
|
|
+use Illuminate\Support\Collection;
|
|
|
+
|
|
|
+class ProjectGanttService
|
|
|
+{
|
|
|
+ public function gantt(Project $project, string $group): array
|
|
|
+ {
|
|
|
+ $groupTasks = $this->getGroupTask($project, $group);
|
|
|
+
|
|
|
+ $groups = $groupTasks->keys()->filter();
|
|
|
+
|
|
|
+ $groupNamesKeyBy = $this->getGroupNamesKeyBy($groups, $group);
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach(["", ...$groups] as $groupKey) {
|
|
|
+ if (! isset($groupTasks[$groupKey])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $items[] = [
|
|
|
+ 'group' => $group,
|
|
|
+ 'group_label' => $groupKey == "" ? ['id' => "", "name" => ""] : $groupNamesKeyBy[$groupKey],
|
|
|
+ 'tasks' => ProjectGanttResource::collection($groupTasks[$groupKey]),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $items;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getGroupNamesKeyBy(Collection $groups, string $group): array
|
|
|
+ {
|
|
|
+ $groupsFormat = $groups->map(fn($group) => ['id' => $group, 'name' => $group]);
|
|
|
+ $groupNames = match ($group) {
|
|
|
+ "requirement_id" => Requirement::query()->whereIn("id", $groups)->selectRaw("id,title as name")->get(),
|
|
|
+ "task_type" => $groupsFormat,
|
|
|
+ "assign" => User::query()->whereIn("id", $groups)->get(['id', 'name']),
|
|
|
+ };
|
|
|
+
|
|
|
+ return $groupNames->keyBy("id")->toArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getGroupTask(
|
|
|
+ Project $project,
|
|
|
+ string $group,
|
|
|
+ ): \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array
|
|
|
+ {
|
|
|
+ return Task::query()
|
|
|
+ ->with(['assignTo', 'finishedBy'])
|
|
|
+ ->where("project_id", $project->id)
|
|
|
+ ->get()
|
|
|
+ ->groupBy($group);
|
|
|
+ }
|
|
|
+}
|