Give tasks one assignee and members, and schedule template tasks to due dates

- 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]>
This commit is contained in:
2026-11-24 19:01:11 +02:00
co-authored by Claude Opus 5.5
parent b4f2f1cfaa
commit 18598cc08e
45 changed files with 1133 additions and 248 deletions
@@ -2,17 +2,26 @@
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)
@@ -29,6 +38,7 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
{
$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')
@@ -72,7 +82,7 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
if ($onlyAssigneeUserId !== null) {
JobInvolvement::restrict($query, $onlyAssigneeUserId);
$query->whereIn('tasks.id', DB::table('task_assignees')->select('task_id')->where('user_id', $onlyAssigneeUserId));
$query->where('tasks.assignee_user_id', $onlyAssigneeUserId);
}
return $query;
@@ -84,8 +94,10 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
private function calendarTasks(Builder $query): array
{
$rows = $query->get([
'tasks.id', 'tasks.customer_job_id', 'tasks.title', 'tasks.status', 'tasks.due_on', 'tasks.estimated_hours',
'tasks.planned_from', 'tasks.planned_to', 'customer_jobs.number', 'customer_jobs.name',
'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();
@@ -93,7 +105,6 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
return [];
}
$assignees = DB::table('task_assignees')->whereIn('task_id', $taskIds)->orderBy('user_id')->get()->groupBy('task_id');
$bookings = DB::table('task_bookings')
->whereIn('task_id', $taskIds)
->groupBy('task_id', 'user_id')
@@ -101,6 +112,14 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
->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,
@@ -112,19 +131,35 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
$row->estimated_hours === null ? null : (string) (float) $row->estimated_hours,
self::date($row->planned_from),
self::date($row->planned_to),
($assignees->get($row->id) ?? collect())->map(function ($assignee) use ($bookings, $row): CalendarAssignment {
$booking = $bookings->get($row->id.'-'.$assignee->user_id);
return new CalendarAssignment(
(int) $assignee->user_id,
self::date($booking?->first_day),
self::date($booking?->last_day),
(int) ($booking->minutes ?? 0),
);
})->values()->all(),
$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);