Files
Toiminnanohjaus/app/Modules/Jobs/Application/Services/TaskCalendarLayout.php
T
petriandClaude Opus 5.5 b4f2f1cfaa Add the task calendar, holidays, workflow templates and customer removal
Task calendar (/jobs/calendar) replaces per-unit task Kanban columns
(K34): a two-week Gantt of tasks per person. Bars move, resize and
change person by dragging or through a keyboard dialog. Each task's
estimate is booked evenly over working days into task_bookings, and
what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves
to the next working day. Tasks get planned_from/planned_to on the form.

Holidays (/settings/holidays): Finnish public holidays calculated per
year, which the admin can take out of use, plus company days off.

Also includes the earlier uncommitted work: workflow templates with
adding tasks from a template, and removing customers no module refers to.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 18:13: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 with several assignees is on each of their rows) 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->assignments !== []) {
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))];
}
}