- A task now has one optional assignee (tasks.assignee_user_id), whose calendar row it is on and whose days its estimate books, and members (task_members) who see it among their own jobs and in the weekly sheet but get no bookings. A migration moves existing assignees over: the one with most booked time becomes the assignee, the others members. - Adding tasks from a workflow template asks for each step's assignee and, by default, schedules tasks with a due date to end on it, booking the estimate backwards from the due date (not before today); what does not fit runs past the due date and is reported as late. - The task calendar leaves quick sale tasks out of the unscheduled list, shows a bar's job details in a popover on hover and focus, and shows the day load on its own grid line. - The template table stacks its cells on narrow screens to reflow at 320 px. Co-Authored-By: Claude Opus 5.5 <[email protected]>
168 lines
6.9 KiB
PHP
168 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
|
|
|
|
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
|
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
|
use App\Modules\Jobs\Application\Contracts\TaskCalendarQueries;
|
|
use App\Modules\Jobs\Application\DTOs\CalendarAssignment;
|
|
use App\Modules\Jobs\Application\DTOs\CalendarTask;
|
|
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;
|
|
|
|
class EloquentTaskCalendarQueries implements TaskCalendarQueries
|
|
{
|
|
public function __construct(
|
|
private CatalogQueries $catalog,
|
|
private CustomerQueries $customers,
|
|
private UserAccountQueries $users,
|
|
) {}
|
|
|
|
public function scheduledTasks(string $from, string $to, ?int $serviceUnitId = null, ?int $onlyAssigneeUserId = null): array
|
|
{
|
|
$query = $this->tasks($serviceUnitId, $onlyAssigneeUserId)
|
|
->whereNotNull('tasks.planned_from')
|
|
->where('tasks.planned_from', '<=', $to)
|
|
->where('tasks.planned_to', '>=', $from)
|
|
->orderBy('tasks.planned_from')
|
|
->orderBy('tasks.id');
|
|
|
|
return $this->calendarTasks($query);
|
|
}
|
|
|
|
public function unscheduledOpenTasks(?int $serviceUnitId = null, ?int $onlyAssigneeUserId = null, int $limit = 100): array
|
|
{
|
|
$query = $this->tasks($serviceUnitId, $onlyAssigneeUserId)
|
|
->whereNull('tasks.planned_from')
|
|
->whereNull('customer_jobs.quick_sale_year')
|
|
->where('tasks.status', '!=', TaskStatus::Done->value)
|
|
->where('customer_jobs.status', '!=', JobStatus::Archived->value)
|
|
->orderByRaw('tasks.due_on IS NULL')
|
|
->orderBy('tasks.due_on')
|
|
->orderBy('tasks.id')
|
|
->limit($limit);
|
|
|
|
return $this->calendarTasks($query);
|
|
}
|
|
|
|
public function openMinutesByUser(array $userIds, string $from, string $to): array
|
|
{
|
|
if ($userIds === []) {
|
|
return [];
|
|
}
|
|
|
|
$minutes = [];
|
|
|
|
DB::table('task_bookings')
|
|
->join('tasks', 'tasks.id', '=', 'task_bookings.task_id')
|
|
->whereIn('task_bookings.user_id', $userIds)
|
|
->where('tasks.status', '!=', TaskStatus::Done->value)
|
|
->whereBetween('task_bookings.day', [$from, $to])
|
|
->groupBy('task_bookings.user_id', 'task_bookings.day')
|
|
->selectRaw('task_bookings.user_id as user_id, task_bookings.day as day, SUM(task_bookings.minutes) as minutes')
|
|
->get()
|
|
->each(function ($row) use (&$minutes): void {
|
|
$minutes[(int) $row->user_id][substr((string) $row->day, 0, 10)] = (int) $row->minutes;
|
|
});
|
|
|
|
return $minutes;
|
|
}
|
|
|
|
private function tasks(?int $serviceUnitId, ?int $onlyAssigneeUserId): Builder
|
|
{
|
|
$query = DB::table('tasks')->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id');
|
|
|
|
if ($serviceUnitId !== null) {
|
|
$query->whereIn('customer_jobs.id', DB::table('customer_job_service_unit')->select('customer_job_id')->where('service_unit_id', $serviceUnitId));
|
|
}
|
|
|
|
if ($onlyAssigneeUserId !== null) {
|
|
JobInvolvement::restrict($query, $onlyAssigneeUserId);
|
|
$query->where('tasks.assignee_user_id', $onlyAssigneeUserId);
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* @return list<CalendarTask>
|
|
*/
|
|
private function calendarTasks(Builder $query): array
|
|
{
|
|
$rows = $query->get([
|
|
'tasks.id', 'tasks.customer_job_id', 'tasks.title', 'tasks.description', 'tasks.status', 'tasks.due_on', 'tasks.estimated_hours',
|
|
'tasks.planned_from', 'tasks.planned_to', 'tasks.assignee_user_id', 'tasks.work_type_id',
|
|
'customer_jobs.number', 'customer_jobs.name', 'customer_jobs.customer_id', 'customer_jobs.status as job_status',
|
|
'customer_jobs.due_on as job_due_on', 'customer_jobs.responsible_user_id',
|
|
]);
|
|
$taskIds = $rows->pluck('id')->map(fn ($id): int => (int) $id)->all();
|
|
|
|
if ($taskIds === []) {
|
|
return [];
|
|
}
|
|
|
|
$bookings = DB::table('task_bookings')
|
|
->whereIn('task_id', $taskIds)
|
|
->groupBy('task_id', 'user_id')
|
|
->selectRaw('task_id, user_id, MIN(day) as first_day, MAX(day) as last_day, SUM(minutes) as minutes')
|
|
->get()
|
|
->keyBy(fn ($row): string => $row->task_id.'-'.$row->user_id);
|
|
|
|
$customerNames = $this->customers->namesByIds(self::ids($rows->pluck('customer_id')->all()));
|
|
$userNames = $this->users->namesByIds(self::ids($rows->pluck('responsible_user_id')->all()));
|
|
$workTypeNames = [];
|
|
|
|
foreach (self::ids($rows->pluck('work_type_id')->all()) as $workTypeId) {
|
|
$workTypeNames[$workTypeId] = $this->catalog->findWorkType($workTypeId)->name ?? null;
|
|
}
|
|
|
|
return $rows->map(fn ($row): CalendarTask => new CalendarTask(
|
|
(int) $row->id,
|
|
(int) $row->customer_job_id,
|
|
(string) $row->number,
|
|
(string) $row->name,
|
|
(string) $row->title,
|
|
TaskStatus::from((string) $row->status),
|
|
self::date($row->due_on),
|
|
$row->estimated_hours === null ? null : (string) (float) $row->estimated_hours,
|
|
self::date($row->planned_from),
|
|
self::date($row->planned_to),
|
|
$row->assignee_user_id === null ? null : self::assignment((int) $row->assignee_user_id, $bookings->get($row->id.'-'.$row->assignee_user_id)),
|
|
customerName: $customerNames[(int) $row->customer_id] ?? null,
|
|
jobStatus: JobStatus::tryFrom((string) $row->job_status),
|
|
jobDueOn: self::date($row->job_due_on),
|
|
responsibleName: $row->responsible_user_id === null ? null : ($userNames[(int) $row->responsible_user_id] ?? null),
|
|
workTypeName: $workTypeNames[(int) $row->work_type_id] ?? null,
|
|
description: $row->description === null ? null : (string) $row->description,
|
|
))->values()->all();
|
|
}
|
|
|
|
/**
|
|
* @param array<int, mixed> $values
|
|
* @return list<int>
|
|
*/
|
|
private static function ids(array $values): array
|
|
{
|
|
return array_values(array_unique(array_map('intval', array_filter($values, fn ($value): bool => $value !== null))));
|
|
}
|
|
|
|
private static function assignment(int $userId, mixed $booking): CalendarAssignment
|
|
{
|
|
return new CalendarAssignment(
|
|
$userId,
|
|
self::date($booking?->first_day),
|
|
self::date($booking?->last_day),
|
|
(int) ($booking->minutes ?? 0),
|
|
);
|
|
}
|
|
|
|
private static function date(mixed $value): ?string
|
|
{
|
|
return $value === null ? null : substr((string) $value, 0, 10);
|
|
}
|
|
}
|