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
@@ -22,7 +22,8 @@ interface TaskCalendarQueries
/**
* Tasks that are not done and have no planned period, on jobs that are
* not archived, by due date.
* not archived, by due date. Tasks of the quick sale jobs are left out:
* they hold quick sale hours and are not scheduled.
*
* @return list<CalendarTask>
*/
@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace App\Modules\Jobs\Application\DTOs;
/**
* One assignee's part of a scheduled task: the first and last booked day
* The assignee's part of a scheduled task: the first and last booked day
* (null when the task books no time, e.g. without an estimate) and the
* booked minutes.
*/
@@ -4,15 +4,17 @@ declare(strict_types=1);
namespace App\Modules\Jobs\Application\DTOs;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
/**
* A task as the task calendar shows it. Dates are Y-m-d.
* A task as the task calendar shows it, with the basic details of its job
* for the bar's info popover. Dates are Y-m-d.
*/
final readonly class CalendarTask
{
/**
* @param list<CalendarAssignment> $assignments One per assignee.
* @param ?CalendarAssignment $assignment The assignee's part; null without an assignee.
*/
public function __construct(
public int $taskId,
@@ -25,17 +27,17 @@ final readonly class CalendarTask
public ?string $estimatedHours,
public ?string $plannedFrom,
public ?string $plannedTo,
public array $assignments,
public ?CalendarAssignment $assignment,
public ?string $customerName = null,
public ?JobStatus $jobStatus = null,
public ?string $jobDueOn = null,
public ?string $responsibleName = null,
public ?string $workTypeName = null,
public ?string $description = null,
) {}
public function assignmentOf(int $userId): ?CalendarAssignment
{
foreach ($this->assignments as $assignment) {
if ($assignment->userId === $userId) {
return $assignment;
}
}
return null;
return $this->assignment?->userId === $userId ? $this->assignment : null;
}
}
@@ -11,7 +11,7 @@ namespace App\Modules\Jobs\Application\DTOs;
final readonly class TaskData
{
/**
* @param list<int> $assigneeUserIds
* @param list<int> $memberUserIds Members see the task among their own; only the assignee gets calendar bookings.
*/
public function __construct(
public string $title,
@@ -20,7 +20,8 @@ final readonly class TaskData
public ?string $description = null,
public ?string $dueOn = null,
public ?string $estimatedHours = null,
public array $assigneeUserIds = [],
public ?int $assigneeUserId = null,
public array $memberUserIds = [],
public ?string $plannedFrom = null,
public ?string $plannedTo = null,
) {}
@@ -12,7 +12,7 @@ use App\Modules\Jobs\Domain\Enums\TaskStatus;
final readonly class TaskSummary
{
/**
* @param list<NamedId> $assignees
* @param list<NamedId> $members
*/
public function __construct(
public int $id,
@@ -23,7 +23,8 @@ final readonly class TaskSummary
public ?string $dueOn,
public ?string $estimatedHours,
public NamedId $workType,
public array $assignees,
public ?NamedId $assignee,
public array $members,
public ?string $plannedFrom = null,
public ?string $plannedTo = null,
) {}
@@ -5,13 +5,14 @@ declare(strict_types=1);
namespace App\Modules\Jobs\Application\DTOs;
/**
* Due date (Y-m-d) and estimated hours ("1,5") entered for the task of one
* workflow template step. Both are optional.
* Due date (Y-m-d), estimated hours ("1,5") and assignee entered for the
* task of one workflow template step. All are optional.
*/
final readonly class TemplateTaskSchedule
{
public function __construct(
public ?string $dueOn = null,
public ?string $estimatedHours = null,
public ?int $assigneeUserId = null,
) {}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\DTOs;
/**
* What adding tasks from a workflow template did: how many tasks were
* added, how many of them were scheduled to their due dates, and how many
* of those end after the due date because their assignee's days were full.
*/
final readonly class TemplateTasksResult
{
public function __construct(
public int $added,
public int $scheduled = 0,
public int $late = 0,
) {}
}
@@ -11,8 +11,8 @@ use App\Modules\Jobs\Domain\Enums\TaskStatus;
/**
* Lays scheduled tasks out on the task calendar's rows: one row per
* person (a task with several assignees is on each of their rows) and a
* row for tasks without an assignee. Overlapping bars get their own lanes.
* person (a task is on its assignee's row; members do not see it there)
* and a row for tasks without an assignee. Overlapping bars get their own lanes.
*/
final class TaskCalendarLayout
{
@@ -61,7 +61,7 @@ final class TaskCalendarLayout
}
if ($userId === null) {
if ($task->assignments !== []) {
if ($task->assignment !== null) {
return null;
}
@@ -13,7 +13,7 @@ use DateInterval;
use DateTimeImmutable;
/**
* Books a saved task's estimate from each assignee's days with the
* Books a saved task's estimate from its assignee's days with the
* TaskScheduler, using the holidays in use and the daily maximum
* (setting `jobs.daily_capacity_hours`, 8 h by default). When the days
* are full, the task's planned end moves later.
@@ -41,8 +41,8 @@ final class TaskPlanner
}
/**
* Rebooks the task's assignees and, if their days were full, moves the
* task's planned end later. The task must be saved (have an id);
* Rebooks the task's assignee (members get no bookings) and, if their
* days were full, moves the task's planned end later. The task must be saved (have an id);
* save it again afterwards.
*
* @return bool Whether the planned end moved later.
@@ -59,33 +59,27 @@ final class TaskPlanner
return false;
}
$this->bookings->deleteForTask($taskId, $task->assigneeUserIds());
$userId = $task->assigneeUserId();
$this->bookings->deleteForTask($taskId, $userId === null ? [] : [$userId]);
$minutes = (int) round(($task->estimatedHours()->value ?? 0) * 60);
$lookahead = $to->add(new DateInterval('P'.self::LOOKAHEAD_DAYS.'D'))->format('Y-m-d');
$holidays = $this->holidays->activeDatesBetween($from->format('Y-m-d'), $lookahead);
$capacity = $this->capacityMinutes();
$end = $to;
foreach ($task->assigneeUserIds() as $userId) {
$schedule = TaskScheduler::schedule(
$from,
$to,
$minutes,
$this->bookings->otherOpenMinutes($userId, $from->format('Y-m-d'), $lookahead, $taskId),
$capacity,
$holidays,
);
$this->bookings->replaceFor($taskId, $userId, $schedule->minutesByDay);
if ($schedule->end > $end) {
$end = $schedule->end;
}
if ($userId === null) {
return false;
}
if ($end > $to) {
$task->schedule($from, $end);
$lookahead = $to->add(new DateInterval('P'.self::LOOKAHEAD_DAYS.'D'))->format('Y-m-d');
$schedule = TaskScheduler::schedule(
$from,
$to,
self::minutesOf($task),
$this->bookings->otherOpenMinutes($userId, $from->format('Y-m-d'), $lookahead, $taskId),
$this->capacityMinutes(),
$this->holidays->activeDatesBetween($from->format('Y-m-d'), $lookahead),
);
$this->bookings->replaceFor($taskId, $userId, $schedule->minutesByDay);
if ($schedule->end > $to) {
$task->schedule($from, $schedule->end);
return true;
}
@@ -93,14 +87,69 @@ final class TaskPlanner
return false;
}
/**
* Schedules a saved task to end on its due date: the estimate is booked
* from the assignee's free time on the due date and the working days
* before it, not before the earliest day (today); what does not fit
* goes after the due date. Without an assignee nothing is booked and the
* task starts as many working days before as the estimate needs. Save
* the task afterwards.
*
* @return bool Whether the task ends after its due date.
*/
public function planBackwards(Task $task, DateTimeImmutable $earliest): bool
{
$taskId = (int) $task->id();
$due = $task->dueOn();
if ($due === null) {
return false;
}
$userId = $task->assigneeUserId();
$minutes = self::minutesOf($task);
$start = min($due, $earliest)->sub(new DateInterval('P'.self::LOOKAHEAD_DAYS.'D'))->format('Y-m-d');
$lookahead = max($due, $earliest)->add(new DateInterval('P'.self::LOOKAHEAD_DAYS.'D'))->format('Y-m-d');
$holidays = $this->holidays->activeDatesBetween($start, $lookahead);
$this->bookings->deleteForTask($taskId);
if ($userId === null) {
$task->schedule(TaskScheduler::defaultStart($due, $minutes, $this->capacityMinutes(), $holidays, $earliest), $due);
return false;
}
$schedule = TaskScheduler::scheduleBackwards(
$due,
$minutes,
$this->bookings->otherOpenMinutes($userId, $start, $lookahead, $taskId),
$this->capacityMinutes(),
$holidays,
$earliest,
);
$this->bookings->replaceFor($taskId, $userId, $schedule->minutesByDay);
$firstDay = array_key_first($schedule->minutesByDay);
$task->schedule($firstDay === null ? $due : new DateTimeImmutable($firstDay), $schedule->end);
return $schedule->extended;
}
/**
* The end of a task placed on a day without an end (TaskScheduler::defaultEnd).
*/
public function defaultEnd(DateTimeImmutable $from, Task $task): DateTimeImmutable
{
$minutes = (int) round(($task->estimatedHours()->value ?? 0) * 60);
$minutes = self::minutesOf($task);
$lookahead = $from->add(new DateInterval('P'.self::LOOKAHEAD_DAYS.'D'))->format('Y-m-d');
return TaskScheduler::defaultEnd($from, $minutes, $this->capacityMinutes(), $this->holidays->activeDatesBetween($from->format('Y-m-d'), $lookahead));
}
private static function minutesOf(Task $task): int
{
return (int) round(($task->estimatedHours()->value ?? 0) * 60);
}
}
@@ -12,7 +12,8 @@ use App\Modules\Users\Application\Contracts\UserAccountQueries;
/**
* Checks that a task's work type belongs to the job's service units (or
* the shared work types) and is in use, and that assignees are active.
* the shared work types) and is in use, and that its assignee and members
* are active.
* What the task already has stays allowed after being taken out of use.
*/
final class TaskReferenceChecker
@@ -23,11 +24,11 @@ final class TaskReferenceChecker
) {}
/**
* @param list<int> $assigneeUserIds
* @param list<int> $memberUserIds
*
* @throws InvalidTaskReference
*/
public function check(CustomerJob $job, int $workTypeId, array $assigneeUserIds, ?Task $current = null): void
public function check(CustomerJob $job, int $workTypeId, ?int $assigneeUserId, array $memberUserIds = [], ?Task $current = null): void
{
if ($workTypeId !== $current?->workTypeId() && ! in_array($workTypeId, $this->workTypeIdsFor($job), true)) {
throw new InvalidTaskReference('work_type');
@@ -35,9 +36,16 @@ final class TaskReferenceChecker
$activeIds = array_map(fn ($option): int => $option->id, $this->users->activeUserOptions());
foreach ($assigneeUserIds as $userId) {
if (! in_array($userId, $activeIds, true) && ! in_array($userId, $current?->assigneeUserIds() ?? [], true)) {
throw new InvalidTaskReference('assignees');
$currentIds = $current === null ? [] : [$current->assigneeUserId(), ...$current->memberUserIds()];
$allowed = fn (int $userId): bool => in_array($userId, $activeIds, true) || in_array($userId, $currentIds, true);
if ($assigneeUserId !== null && ! $allowed($assigneeUserId)) {
throw new InvalidTaskReference('assignee');
}
foreach ($memberUserIds as $userId) {
if (! $allowed($userId)) {
throw new InvalidTaskReference('members');
}
}
}
@@ -42,7 +42,7 @@ final class AddTask
return $this->transactions->run(function () use ($jobId, $data, $title, $actorUserId): int {
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$this->references->check($job, $data->workTypeId, $data->assigneeUserIds);
$this->references->check($job, $data->workTypeId, $data->assigneeUserId, $data->memberUserIds);
$task = Task::create(
$jobId,
@@ -53,7 +53,8 @@ final class AddTask
TaskStatus::from($data->status),
TaskInput::date($data->dueOn),
TaskInput::hours($data->estimatedHours),
$data->assigneeUserIds,
$data->assigneeUserId,
$data->memberUserIds,
$actorUserId,
TaskInput::date($data->plannedFrom),
TaskInput::date($data->plannedTo),
@@ -7,6 +7,8 @@ namespace App\Modules\Jobs\Application\UseCases;
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateDetails;
use App\Modules\Jobs\Application\DTOs\TemplateTaskSchedule;
use App\Modules\Jobs\Application\DTOs\TemplateTasksResult;
use App\Modules\Jobs\Application\Services\TaskPlanner;
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
@@ -19,11 +21,14 @@ use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Jobs\Domain\Exceptions\WorkflowTemplateNotAvailable;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Makes a task of every step of a workflow template, in the template's
* order: waiting, with the step's work type and the due date and estimate
* entered for it, assigned to the user who adds them. The tasks are
* order: waiting, with the step's work type and the due date, estimate and
* assignee entered for it (without an entry, the adder is the assignee).
* When asked, a task with a due date is scheduled to end on it, booked
* backwards from the due date (TaskPlanner::planBackwards). The tasks are
* copies, so later changes to the template do not touch them.
*/
final class AddTasksFromWorkflowTemplate
@@ -33,32 +38,39 @@ final class AddTasksFromWorkflowTemplate
private TaskRepository $tasks,
private WorkflowTemplateQueries $templates,
private TaskReferenceChecker $references,
private TaskPlanner $planner,
private Clock $clock,
private TransactionManager $transactions,
) {}
/**
* @param list<TemplateTaskSchedule> $schedules In step order; a missing one leaves the task without a due date and estimate.
* @return int How many tasks were added.
* @param list<TemplateTaskSchedule> $schedules In step order; a missing one leaves the task without a due date and estimate, assigned to the adder.
* @param bool $scheduleToDueDates Whether tasks with a due date are scheduled to end on it.
*
* @throws CustomerJobNotFound
* @throws InvalidEstimatedHours
* @throws TasksNotAllowedYet
* @throws WorkflowTemplateNotAvailable
* @throws InvalidTaskReference when a step's work type is not available for the job
* @throws InvalidTaskReference when a step's work type is not available for the job, or the assignee is not active
*/
public function execute(int $jobId, int $templateId, int $actorUserId, array $schedules = []): int
public function execute(int $jobId, int $templateId, int $actorUserId, array $schedules = [], bool $scheduleToDueDates = false): TemplateTasksResult
{
$dueDates = array_map(fn (TemplateTaskSchedule $schedule) => TaskInput::date($schedule->dueOn), $schedules);
$estimates = array_map(fn (TemplateTaskSchedule $schedule) => TaskInput::hours($schedule->estimatedHours), $schedules);
$assignees = array_map(fn (TemplateTaskSchedule $schedule): ?int => $schedule->assigneeUserId, $schedules);
return $this->transactions->run(function () use ($jobId, $templateId, $actorUserId, $dueDates, $estimates): int {
return $this->transactions->run(function () use ($jobId, $templateId, $actorUserId, $dueDates, $estimates, $assignees, $scheduleToDueDates): TemplateTasksResult {
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$template = $this->availableTemplate($templateId, $job->details()->serviceUnitIds);
$today = $this->clock->today();
$scheduled = 0;
$late = 0;
foreach ($template->steps as $index => $step) {
$this->references->check($job, $step->effectiveWorkTypeId, [$actorUserId]);
$assigneeUserId = array_key_exists($index, $assignees) ? $assignees[$index] : $actorUserId;
$this->references->check($job, $step->effectiveWorkTypeId, $assigneeUserId);
$this->tasks->save(Task::create(
$task = $this->tasks->save(Task::create(
$jobId,
$job->details()->status,
$step->effectiveWorkTypeId,
@@ -67,12 +79,19 @@ final class AddTasksFromWorkflowTemplate
TaskStatus::Waiting,
$dueDates[$index] ?? null,
$estimates[$index] ?? null,
[$actorUserId],
$assigneeUserId,
[],
$actorUserId,
));
if ($scheduleToDueDates && $task->dueOn() !== null) {
$late += $this->planner->planBackwards($task, $today) ? 1 : 0;
$scheduled++;
$this->tasks->save($task);
}
}
return count($template->steps);
return new TemplateTasksResult(count($template->steps), $scheduled, $late);
});
}
@@ -72,6 +72,7 @@ final class ProvideQuickSaleTask
TaskStatus::InProgress,
null,
null,
null,
[],
$actorUserId,
));
@@ -17,8 +17,8 @@ use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Moves a task in the task calendar: a new planned period, and possibly
* from one person's row to another's (the person on the row it left is
* replaced as an assignee; null is the "no assignee" row). Without an
* from one person's row to another's (the person on the new row becomes
* the assignee; null is the "no assignee" row; members stay). Without an
* end, the task lasts as many working days as its estimate needs.
*/
final class ScheduleTask
@@ -46,19 +46,11 @@ final class ScheduleTask
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
$assignees = $task->assigneeUserIds();
if ($fromUserId !== $toUserId) {
$assignees = array_values(array_filter($assignees, fn (int $userId): bool => $userId !== $fromUserId));
if ($toUserId !== null) {
$assignees[] = $toUserId;
}
$this->references->check($job, $task->workTypeId(), $toUserId, [], $task);
$task->assignTo($toUserId);
}
$this->references->check($job, $task->workTypeId(), $assignees, $task);
$task->changeAssignees($assignees);
$task->schedule($from, $to ?? $this->planner->defaultEnd($from, $task));
$task = $this->tasks->save($task);
@@ -42,7 +42,7 @@ final class UpdateTask
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
$this->references->check($job, $data->workTypeId, $data->assigneeUserIds, $task);
$this->references->check($job, $data->workTypeId, $data->assigneeUserId, $data->memberUserIds, $task);
$before = self::planningInput($task);
@@ -53,7 +53,8 @@ final class UpdateTask
TaskStatus::from($data->status),
TaskInput::date($data->dueOn),
TaskInput::hours($data->estimatedHours),
$data->assigneeUserIds,
$data->assigneeUserId,
$data->memberUserIds,
);
$task->schedule(TaskInput::date($data->plannedFrom), TaskInput::date($data->plannedTo));
@@ -68,7 +69,7 @@ final class UpdateTask
}
/**
* @return array{0: ?string, 1: ?string, 2: ?float, 3: list<int>}
* @return array{0: ?string, 1: ?string, 2: ?float, 3: ?int}
*/
private static function planningInput(Task $task): array
{
@@ -76,7 +77,7 @@ final class UpdateTask
$task->plannedFrom()?->format('Y-m-d'),
$task->plannedTo()?->format('Y-m-d'),
$task->estimatedHours()?->value,
$task->assigneeUserIds(),
$task->assigneeUserId(),
];
}
}