Files
Toiminnanohjaus/app/Modules/Jobs/Application/Services/TaskCalendarLayout.php
petriandClaude Opus 5.5 18598cc08e 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]>
2026-11-24 19:01:11 +02:00

124 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Application\Services;
use App\Modules\Jobs\Application\DTOs\CalendarBar;
use App\Modules\Jobs\Application\DTOs\CalendarRow;
use App\Modules\Jobs\Application\DTOs\CalendarTask;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
/**
* Lays scheduled tasks out on the task calendar's rows: one row per
* 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
{
/**
* @param list<string> $days The shown days, Y-m-d, consecutive.
* @param list<array{id: ?int, name: string}> $people Rows in order; id null is the "no assignee" row.
* @param list<CalendarTask> $tasks
* @param array<int, array<string, int>> $minutesByUser user id => Y-m-d => booked minutes.
* @return list<CalendarRow>
*/
public static function build(array $days, array $people, array $tasks, array $minutesByUser): array
{
$rows = [];
foreach ($people as $person) {
$bars = [];
foreach ($tasks as $task) {
$bar = self::bar($task, $person['id'], $days);
if ($bar !== null) {
$bars[] = $bar;
}
}
[$bars, $lanes] = self::stack($bars);
$minutes = [];
foreach ($days as $day) {
$minutes[$day] = $person['id'] === null ? 0 : ($minutesByUser[$person['id']][$day] ?? 0);
}
$rows[] = new CalendarRow($person['id'], $person['name'], $minutes, $bars, $lanes);
}
return $rows;
}
/**
* @param list<string> $days
*/
private static function bar(CalendarTask $task, ?int $userId, array $days): ?CalendarBar
{
if ($task->plannedFrom === null || $task->plannedTo === null || $days === []) {
return null;
}
if ($userId === null) {
if ($task->assignment !== null) {
return null;
}
$from = $task->plannedFrom;
$to = $task->plannedTo;
$minutes = 0;
} else {
$assignment = $task->assignmentOf($userId);
if ($assignment === null) {
return null;
}
$from = $task->plannedFrom;
$to = max($from, $assignment->to ?? $task->plannedTo);
$minutes = $assignment->minutes;
}
$first = $days[0];
$last = $days[count($days) - 1];
if ($to < $first || $from > $last) {
return null;
}
$start = $from < $first ? 0 : (int) array_search($from, $days, true);
$end = $to > $last ? count($days) - 1 : (int) array_search($to, $days, true);
$late = $task->status !== TaskStatus::Done && $task->dueOn !== null && $to > $task->dueOn;
return new CalendarBar($task, $userId, $from, $to, $minutes, $start, $end - $start + 1, 0, $late);
}
/**
* Puts each bar on the first lane where it does not overlap another.
*
* @param list<CalendarBar> $bars
* @return array{0: list<CalendarBar>, 1: int}
*/
private static function stack(array $bars): array
{
usort($bars, fn (CalendarBar $a, CalendarBar $b): int => [$a->start, -$a->span, $a->task->taskId] <=> [$b->start, -$b->span, $b->task->taskId]);
$laneEnds = [];
$stacked = [];
foreach ($bars as $bar) {
$lane = 0;
while (isset($laneEnds[$lane]) && $laneEnds[$lane] >= $bar->start) {
$lane++;
}
$laneEnds[$lane] = $bar->start + $bar->span - 1;
$stacked[] = new CalendarBar($bar->task, $bar->userId, $bar->from, $bar->to, $bar->minutes, $bar->start, $bar->span, $lane, $bar->late);
}
return [$stacked, max(1, count($laneEnds))];
}
}