where('customer_job_id', $jobId) ->orderByRaw('status = ?', [TaskStatus::Done->value]) ->orderByRaw('due_on IS NULL') ->orderBy('due_on') ->orderBy('id') ->get(); return $this->summaries($models); } public function findTask(int $jobId, int $taskId): ?TaskSummary { $model = TaskModel::query()->where('customer_job_id', $jobId)->find($taskId); return $model === null ? null : $this->summaries(collect([$model]))[0]; } public function bookableTask(int $taskId): ?BookableTask { return $this->bookableTasks([$taskId])[0] ?? null; } public function bookableTasks(array $taskIds): array { if ($taskIds === []) { return []; } return $this->bookableRows(DB::table('tasks')->whereIn('tasks.id', $taskIds)); } public function assignedOpenTaskIds(int $userId): array { return DB::table('tasks') ->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id') ->where(fn ($query) => $query ->where('tasks.assignee_user_id', $userId) ->orWhereIn('tasks.id', DB::table('task_members')->select('task_id')->where('user_id', $userId))) ->where('tasks.status', '!=', TaskStatus::Done->value) ->whereNotIn('customer_jobs.status', self::lockedStatuses()) ->orderBy('tasks.id') ->pluck('tasks.id') ->map(fn ($id): int => (int) $id) ->all(); } public function searchBookableTasks(string $term, ?int $onlyMemberUserId = null, int $limit = 8): array { $term = trim($term); if ($term === '') { return []; } $pattern = '%'.addcslashes($term, '%_\\').'%'; $query = DB::table('tasks') ->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id') ->whereNotIn('customer_jobs.status', self::lockedStatuses()) ->where(fn ($query) => $query ->where('customer_jobs.number', 'like', $pattern) ->orWhere('customer_jobs.name', 'like', $pattern) ->orWhere('tasks.title', 'like', $pattern)) ->limit($limit); if ($onlyMemberUserId !== null) { JobInvolvement::restrict($query, $onlyMemberUserId); } return $this->bookableRows(DB::table('tasks')->whereIn('tasks.id', $query->pluck('tasks.id')->all())); } /** * @return list */ private function bookableRows(Builder $tasks): array { $rows = $tasks ->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id') ->orderBy('customer_jobs.number') ->orderBy('tasks.id') ->get(['tasks.id', 'tasks.customer_job_id', 'tasks.title', 'tasks.work_type_id', 'tasks.status', 'customer_jobs.number', 'customer_jobs.name', 'customer_jobs.status as job_status']); $workTypeNames = []; foreach ($rows->pluck('work_type_id')->unique() as $workTypeId) { $workTypeNames[(int) $workTypeId] = $this->catalog->findWorkType((int) $workTypeId)->name ?? ''; } return $rows->map(fn ($row): BookableTask => new BookableTask( (int) $row->id, (int) $row->customer_job_id, (string) $row->number, (string) $row->name, (string) $row->title, (int) $row->work_type_id, $workTypeNames[(int) $row->work_type_id] ?? '', JobStatus::from((string) $row->job_status), TaskStatus::from((string) $row->status), ))->values()->all(); } /** * @return list */ private static function lockedStatuses(): array { return array_values(array_map(fn (JobStatus $status): string => $status->value, array_filter(JobStatus::cases(), fn (JobStatus $status): bool => $status->locksTime()))); } /** * @param Collection $models * @return list */ private function summaries(Collection $models): array { $members = DB::table('task_members') ->whereIn('task_id', $models->pluck('id')->all()) ->get() ->groupBy('task_id'); $userNames = $this->users->namesByIds($members->flatten()->pluck('user_id') ->merge($models->pluck('assignee_user_id')->filter()) ->map(fn ($id): int => (int) $id)->unique()->values()->all()); $workTypeNames = []; foreach ($models->pluck('work_type_id')->unique() as $workTypeId) { $workTypeNames[$workTypeId] = $this->catalog->findWorkType((int) $workTypeId)->name ?? ''; } return $models->map(fn (TaskModel $model): TaskSummary => new TaskSummary( id: $model->id, jobId: $model->customer_job_id, title: $model->title, description: $model->description, status: $model->status, dueOn: $model->due_on?->format('Y-m-d'), estimatedHours: $model->estimated_hours === null ? null : rtrim(rtrim($model->estimated_hours, '0'), '.'), workType: new NamedId($model->work_type_id, $workTypeNames[$model->work_type_id] ?? ''), assignee: $model->assignee_user_id === null ? null : new NamedId($model->assignee_user_id, $userNames[$model->assignee_user_id] ?? ''), members: ($members->get($model->id) ?? collect()) ->map(fn ($row): NamedId => new NamedId((int) $row->user_id, $userNames[(int) $row->user_id] ?? '')) ->sortBy('name') ->values() ->all(), plannedFrom: $model->planned_from?->format('Y-m-d'), plannedTo: $model->planned_to?->format('Y-m-d'), ))->values()->all(); } }