Everyone sees their open tasks on an agenda, their open jobs, this week's hours and quick links. Admin and management also get a lazily loaded management panel: things to do, the month so far against the previous month, jobs and late work, and everyone's workload. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
138 lines
5.1 KiB
PHP
138 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
|
|
|
|
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
|
use App\Modules\Jobs\Application\Contracts\JobOverviewQueries;
|
|
use App\Modules\Jobs\Application\DTOs\CustomerJobSummary;
|
|
use App\Modules\Jobs\Application\DTOs\OpenTask;
|
|
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
|
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
|
use App\Modules\Jobs\Infrastructure\Persistence\JobInvolvement;
|
|
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
|
use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Assignee names come from the Users module, never from its table.
|
|
*/
|
|
class EloquentJobOverviewQueries implements JobOverviewQueries
|
|
{
|
|
public function __construct(
|
|
private UserAccountQueries $users,
|
|
private CustomerJobQueries $jobs,
|
|
) {}
|
|
|
|
public function openTasksOf(int $userId): array
|
|
{
|
|
$query = $this->openTasks()
|
|
->where(fn (Builder $query) => $query
|
|
->where('tasks.assignee_user_id', $userId)
|
|
->orWhereIn('tasks.id', DB::table('task_members')->select('task_id')->where('user_id', $userId)))
|
|
->orderByRaw('tasks.due_on IS NULL')
|
|
->orderBy('tasks.due_on')
|
|
->orderByRaw('tasks.planned_from IS NULL')
|
|
->orderBy('tasks.planned_from')
|
|
->orderBy('tasks.id');
|
|
|
|
return $this->toOpenTasks($query);
|
|
}
|
|
|
|
public function openJobsOf(int $userId): array
|
|
{
|
|
$query = DB::table('customer_jobs')
|
|
->whereIn('status', array_map(fn (JobStatus $status): string => $status->value, JobStatus::openStatuses()))
|
|
->whereNull('quick_sale_year');
|
|
JobInvolvement::restrict($query, $userId);
|
|
|
|
$ids = $query->orderByRaw('due_on IS NULL')->orderBy('due_on')->orderByDesc('id')
|
|
->pluck('id')->map(fn ($id): int => (int) $id)->all();
|
|
$summaries = $this->jobs->summariesByIds($ids);
|
|
|
|
return array_values(array_filter(array_map(fn (int $id): ?CustomerJobSummary => $summaries[$id] ?? null, $ids)));
|
|
}
|
|
|
|
public function jobCountsByStatus(): array
|
|
{
|
|
$counts = DB::table('customer_jobs')
|
|
->where('status', '!=', JobStatus::Archived->value)
|
|
->groupBy('status')
|
|
->selectRaw('status, COUNT(*) as job_count')
|
|
->pluck('job_count', 'status');
|
|
|
|
$result = [];
|
|
foreach (JobStatus::kanbanStatuses() as $status) {
|
|
$result[$status->value] = (int) ($counts[$status->value] ?? 0);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function overdueJobCount(string $today): int
|
|
{
|
|
return DB::table('customer_jobs')
|
|
->whereIn('status', array_map(fn (JobStatus $status): string => $status->value, JobStatus::openStatuses()))
|
|
->whereNull('quick_sale_year')
|
|
->whereNotNull('due_on')
|
|
->where('due_on', '<', $today)
|
|
->count();
|
|
}
|
|
|
|
public function overdueTaskCount(string $today): int
|
|
{
|
|
return $this->overdueTasks($today)->count();
|
|
}
|
|
|
|
public function oldestOverdueTasks(string $today, int $limit): array
|
|
{
|
|
return $this->toOpenTasks($this->overdueTasks($today)->orderBy('tasks.due_on')->orderBy('tasks.id')->limit($limit));
|
|
}
|
|
|
|
private function overdueTasks(string $today): Builder
|
|
{
|
|
return $this->openTasks()->whereNotNull('tasks.due_on')->where('tasks.due_on', '<', $today);
|
|
}
|
|
|
|
private function openTasks(): Builder
|
|
{
|
|
return DB::table('tasks')
|
|
->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id')
|
|
->where('tasks.status', '!=', TaskStatus::Done->value)
|
|
->where('customer_jobs.status', '!=', JobStatus::Archived->value)
|
|
->whereNull('customer_jobs.quick_sale_year');
|
|
}
|
|
|
|
/**
|
|
* @return list<OpenTask>
|
|
*/
|
|
private function toOpenTasks(Builder $query): array
|
|
{
|
|
$rows = $query->get([
|
|
'tasks.id', 'tasks.customer_job_id', 'tasks.title', 'tasks.status', 'tasks.due_on', 'tasks.planned_from', 'tasks.planned_to', 'tasks.assignee_user_id',
|
|
'customer_jobs.number', 'customer_jobs.name',
|
|
]);
|
|
|
|
$assigneeIds = $rows->pluck('assignee_user_id')->filter()->map(fn ($id): int => (int) $id)->unique()->values()->all();
|
|
$names = $assigneeIds === [] ? [] : $this->users->namesByIds($assigneeIds);
|
|
|
|
return $rows->map(fn (object $row): OpenTask => new OpenTask(
|
|
(int) $row->id,
|
|
(int) $row->customer_job_id,
|
|
(string) $row->number,
|
|
(string) $row->name,
|
|
(string) $row->title,
|
|
TaskStatus::from((string) $row->status),
|
|
self::day($row->due_on),
|
|
self::day($row->planned_from),
|
|
self::day($row->planned_to),
|
|
$row->assignee_user_id === null ? null : (int) $row->assignee_user_id,
|
|
$row->assignee_user_id === null ? null : ($names[(int) $row->assignee_user_id] ?? null),
|
|
))->values()->all();
|
|
}
|
|
|
|
private static function day(mixed $value): ?string
|
|
{
|
|
return $value === null ? null : substr((string) $value, 0, 10);
|
|
}
|
|
}
|