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]>
This commit is contained in:
2026-11-24 18:13:11 +02:00
co-authored by Claude Opus 5.5
parent dfe4941f02
commit b4f2f1cfaa
134 changed files with 6930 additions and 45 deletions
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Contracts;
/**
* The minutes each assignee's days are booked for tasks (task calendar).
*/
interface TaskBookingRepository
{
/**
* Replaces the assignee's bookings of the task.
*
* @param array<string, int> $minutesByDay Y-m-d => minutes.
*/
public function replaceFor(int $taskId, int $userId, array $minutesByDay): void;
/**
* Removes the task's bookings, except those of the given assignees.
*
* @param list<int> $keepUserIds
*/
public function deleteForTask(int $taskId, array $keepUserIds = []): void;
/**
* Minutes the user's other tasks that are not done book per day.
*
* @return array<string, int> Y-m-d => minutes.
*/
public function otherOpenMinutes(int $userId, string $from, string $to, int $exceptTaskId): array;
}
+73 -2
View File
@@ -6,6 +6,7 @@ namespace App\Modules\Jobs\Domain\Entities;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Jobs\Domain\ValueObjects\EstimatedHours;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
@@ -14,6 +15,7 @@ use DateTimeImmutable;
/**
* A task (tehtävä) of a customer job: one step of the work, done by its
* assignees under one work type. Time entries are recorded on tasks.
* Its planned schedule (planned from–to) places it in the task calendar.
*/
final class Task
{
@@ -31,14 +33,18 @@ final class Task
private ?EstimatedHours $estimatedHours,
private array $assigneeUserIds,
private int $createdBy,
private ?DateTimeImmutable $plannedFrom = null,
private ?DateTimeImmutable $plannedTo = null,
) {
$this->assigneeUserIds = self::normaliseIds($assigneeUserIds);
self::checkPlan($plannedFrom, $plannedTo);
}
/**
* @param list<int> $assigneeUserIds
*
* @throws TasksNotAllowedYet
* @throws PlanEndsBeforeStart
*/
public static function create(
int $jobId,
@@ -51,12 +57,14 @@ final class Task
?EstimatedHours $estimatedHours,
array $assigneeUserIds,
int $createdBy,
?DateTimeImmutable $plannedFrom = null,
?DateTimeImmutable $plannedTo = null,
): self {
if (! $jobStatus->allowsTasks()) {
throw new TasksNotAllowedYet;
}
return new self(null, $jobId, $workTypeId, $title, self::text($description), $status, $dueOn, $estimatedHours, $assigneeUserIds, $createdBy);
return new self(null, $jobId, $workTypeId, $title, self::text($description), $status, $dueOn, $estimatedHours, $assigneeUserIds, $createdBy, ...self::plan($plannedFrom, $plannedTo));
}
/**
@@ -73,8 +81,10 @@ final class Task
?EstimatedHours $estimatedHours,
array $assigneeUserIds,
int $createdBy,
?DateTimeImmutable $plannedFrom = null,
?DateTimeImmutable $plannedTo = null,
): self {
return new self($id, $jobId, $workTypeId, $title, $description, $status, $dueOn, $estimatedHours, $assigneeUserIds, $createdBy);
return new self($id, $jobId, $workTypeId, $title, $description, $status, $dueOn, $estimatedHours, $assigneeUserIds, $createdBy, $plannedFrom, $plannedTo);
}
/**
@@ -103,6 +113,25 @@ final class Task
$this->status = $status;
}
/**
* Sets the planned schedule. A start without an end lasts one day;
* null as the start removes the schedule.
*
* @throws PlanEndsBeforeStart
*/
public function schedule(?DateTimeImmutable $from, ?DateTimeImmutable $to): void
{
[$this->plannedFrom, $this->plannedTo] = self::plan($from, $to);
}
/**
* @param list<int> $assigneeUserIds
*/
public function changeAssignees(array $assigneeUserIds): void
{
$this->assigneeUserIds = self::normaliseIds($assigneeUserIds);
}
public function id(): ?int
{
return $this->id;
@@ -143,6 +172,21 @@ final class Task
return $this->estimatedHours;
}
public function plannedFrom(): ?DateTimeImmutable
{
return $this->plannedFrom;
}
public function plannedTo(): ?DateTimeImmutable
{
return $this->plannedTo;
}
public function isScheduled(): bool
{
return $this->plannedFrom !== null;
}
/**
* @return list<int>
*/
@@ -168,6 +212,33 @@ final class Task
return $ids;
}
/**
* @return array{0: ?DateTimeImmutable, 1: ?DateTimeImmutable}
*
* @throws PlanEndsBeforeStart
*/
private static function plan(?DateTimeImmutable $from, ?DateTimeImmutable $to): array
{
if ($from === null) {
return [null, null];
}
$to ??= $from;
self::checkPlan($from, $to);
return [$from, $to];
}
/**
* @throws PlanEndsBeforeStart
*/
private static function checkPlan(?DateTimeImmutable $from, ?DateTimeImmutable $to): void
{
if ($from !== null && $to !== null && $to < $from) {
throw new PlanEndsBeforeStart;
}
}
private static function text(?string $value): ?string
{
$value = $value === null ? '' : trim($value);
+2 -1
View File
@@ -5,7 +5,8 @@ declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Enums;
/**
* Status of a task (spec 8.2). Per-service-unit columns come in phase 3.
* Status of a task (spec 8.2). The statuses are fixed: tasks are arranged
* per person in the task calendar instead of per-unit Kanban columns.
*/
enum TaskStatus: string
{
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* The workflow template is missing, out of use, or not for the job's service units.
*/
final class WorkflowTemplateNotAvailable extends DomainException
{
public function __construct(public readonly int $templateId)
{
parent::__construct("Workflow template {$templateId} is not available for this job.");
}
}
@@ -0,0 +1,178 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Services;
use App\Modules\Jobs\Domain\ValueObjects\TaskSchedule;
use DateInterval;
use DateTimeImmutable;
/**
* Books a task's estimated time from an assignee's days (spec 18.3).
*
* The estimate is split evenly over the working days of the planned
* period (not Saturdays, Sundays or holidays); a period of only such days
* splits it over all its days. No day gets more than the assignee's free
* capacity (the daily maximum minus what other open tasks already book):
* what does not fit moves to the next day of the period, and past its
* end to the next working days, which makes the task end later. Other
* tasks' bookings never move.
*/
final class TaskScheduler
{
/** Bookings are made in quarter hours, like time entries. */
private const int STEP_MINUTES = 15;
/** How far past the planned end the overflow may go. */
private const int MAX_EXTRA_DAYS = 366;
/**
* @param array<string, int> $otherMinutes Y-m-d => minutes booked by the assignee's other open tasks.
* @param list<string> $holidays Y-m-d dates of the holidays in use.
*/
public static function schedule(
DateTimeImmutable $from,
DateTimeImmutable $to,
int $minutes,
array $otherMinutes,
int $capacityMinutes,
array $holidays,
): TaskSchedule {
if ($minutes <= 0) {
return new TaskSchedule([], $to, false);
}
$holidaySet = array_flip($holidays);
$days = self::days($from, $to);
$targets = array_values(array_filter($days, fn (string $day): bool => self::isWorkingDay($day, $holidaySet)));
if ($targets === []) {
$targets = $days;
}
$shares = self::split($minutes, count($targets));
$booked = [];
$carry = 0;
foreach ($targets as $index => $day) {
$wanted = $shares[$index] + $carry;
$booking = min($wanted, self::free($day, $otherMinutes, $capacityMinutes));
$carry = $wanted - $booking;
if ($booking > 0) {
$booked[$day] = $booking;
}
}
$day = $to;
for ($extra = 0; $carry > 0 && $extra < self::MAX_EXTRA_DAYS; $extra++) {
$day = $day->add(new DateInterval('P1D'));
$date = $day->format('Y-m-d');
if (! self::isWorkingDay($date, $holidaySet)) {
continue;
}
$booking = min($carry, self::free($date, $otherMinutes, $capacityMinutes));
$carry -= $booking;
if ($booking > 0) {
$booked[$date] = $booking;
}
}
if ($carry > 0) {
$date = $day->format('Y-m-d');
$booked[$date] = ($booked[$date] ?? 0) + $carry;
}
ksort($booked);
$lastDay = array_key_last($booked);
$end = $lastDay !== null && $lastDay > $to->format('Y-m-d') ? new DateTimeImmutable($lastDay) : $to;
return new TaskSchedule($booked, $end, $end > $to);
}
/**
* The end of a task placed on a day without an end: as many working
* days as the estimate needs at the daily maximum; without an estimate, the day itself.
*
* @param list<string> $holidays
*/
public static function defaultEnd(DateTimeImmutable $from, int $minutes, int $capacityMinutes, array $holidays): DateTimeImmutable
{
if ($minutes <= 0 || $capacityMinutes <= 0) {
return $from;
}
$needed = (int) ceil($minutes / $capacityMinutes);
$holidaySet = array_flip($holidays);
$day = $from;
$counted = self::isWorkingDay($from->format('Y-m-d'), $holidaySet) ? 1 : 0;
for ($extra = 0; $counted < $needed && $extra < self::MAX_EXTRA_DAYS; $extra++) {
$day = $day->add(new DateInterval('P1D'));
if (self::isWorkingDay($day->format('Y-m-d'), $holidaySet)) {
$counted++;
}
}
return $day;
}
/**
* @param array<string, int> $holidaySet
*/
public static function isWorkingDay(string $date, array $holidaySet): bool
{
$day = new DateTimeImmutable($date);
return (int) $day->format('N') <= 5 && ! isset($holidaySet[$date]);
}
/**
* @return list<string>
*/
private static function days(DateTimeImmutable $from, DateTimeImmutable $to): array
{
$days = [];
for ($day = $from; $day <= $to; $day = $day->add(new DateInterval('P1D'))) {
$days[] = $day->format('Y-m-d');
}
return $days;
}
/**
* Splits the minutes into equal quarter-hour shares; the first days get
* the remainder. Odd minutes go to the first day.
*
* @return list<int>
*/
private static function split(int $minutes, int $parts): array
{
$steps = intdiv($minutes, self::STEP_MINUTES);
$odd = $minutes % self::STEP_MINUTES;
$shares = [];
for ($index = 0; $index < $parts; $index++) {
$shares[] = (intdiv($steps, $parts) + ($index < $steps % $parts ? 1 : 0)) * self::STEP_MINUTES;
}
$shares[0] += $odd;
return $shares;
}
/**
* @param array<string, int> $otherMinutes
*/
private static function free(string $day, array $otherMinutes, int $capacityMinutes): int
{
return max(0, $capacityMinutes - ($otherMinutes[$day] ?? 0));
}
}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
use DateTimeImmutable;
/**
* The days one assignee spends on a task: minutes booked per day, and
* the day the work ends. The end is later than planned when the
* assignee's days were full and the rest moved to the next working days.
*/
final readonly class TaskSchedule
{
/**
* @param array<string, int> $minutesByDay Y-m-d => minutes, in date order.
*/
public function __construct(
public array $minutesByDay,
public DateTimeImmutable $end,
public bool $extended,
) {}
}