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]>
249 lines
6.3 KiB
PHP
249 lines
6.3 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
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;
|
||
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
|
||
{
|
||
/**
|
||
* @param list<int> $assigneeUserIds
|
||
*/
|
||
private function __construct(
|
||
private ?int $id,
|
||
private int $jobId,
|
||
private int $workTypeId,
|
||
private TaskTitle $title,
|
||
private ?string $description,
|
||
private TaskStatus $status,
|
||
private ?DateTimeImmutable $dueOn,
|
||
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,
|
||
JobStatus $jobStatus,
|
||
int $workTypeId,
|
||
TaskTitle $title,
|
||
?string $description,
|
||
TaskStatus $status,
|
||
?DateTimeImmutable $dueOn,
|
||
?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, ...self::plan($plannedFrom, $plannedTo));
|
||
}
|
||
|
||
/**
|
||
* @param list<int> $assigneeUserIds
|
||
*/
|
||
public static function reconstitute(
|
||
int $id,
|
||
int $jobId,
|
||
int $workTypeId,
|
||
TaskTitle $title,
|
||
?string $description,
|
||
TaskStatus $status,
|
||
?DateTimeImmutable $dueOn,
|
||
?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, $plannedFrom, $plannedTo);
|
||
}
|
||
|
||
/**
|
||
* @param list<int> $assigneeUserIds
|
||
*/
|
||
public function changeDetails(
|
||
int $workTypeId,
|
||
TaskTitle $title,
|
||
?string $description,
|
||
TaskStatus $status,
|
||
?DateTimeImmutable $dueOn,
|
||
?EstimatedHours $estimatedHours,
|
||
array $assigneeUserIds,
|
||
): void {
|
||
$this->workTypeId = $workTypeId;
|
||
$this->title = $title;
|
||
$this->description = self::text($description);
|
||
$this->status = $status;
|
||
$this->dueOn = $dueOn;
|
||
$this->estimatedHours = $estimatedHours;
|
||
$this->assigneeUserIds = self::normaliseIds($assigneeUserIds);
|
||
}
|
||
|
||
public function changeStatus(TaskStatus $status): void
|
||
{
|
||
$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;
|
||
}
|
||
|
||
public function jobId(): int
|
||
{
|
||
return $this->jobId;
|
||
}
|
||
|
||
public function workTypeId(): int
|
||
{
|
||
return $this->workTypeId;
|
||
}
|
||
|
||
public function title(): TaskTitle
|
||
{
|
||
return $this->title;
|
||
}
|
||
|
||
public function description(): ?string
|
||
{
|
||
return $this->description;
|
||
}
|
||
|
||
public function status(): TaskStatus
|
||
{
|
||
return $this->status;
|
||
}
|
||
|
||
public function dueOn(): ?DateTimeImmutable
|
||
{
|
||
return $this->dueOn;
|
||
}
|
||
|
||
public function estimatedHours(): ?EstimatedHours
|
||
{
|
||
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>
|
||
*/
|
||
public function assigneeUserIds(): array
|
||
{
|
||
return $this->assigneeUserIds;
|
||
}
|
||
|
||
public function createdBy(): int
|
||
{
|
||
return $this->createdBy;
|
||
}
|
||
|
||
/**
|
||
* @param list<int> $ids
|
||
* @return list<int>
|
||
*/
|
||
private static function normaliseIds(array $ids): array
|
||
{
|
||
$ids = array_values(array_unique($ids));
|
||
sort($ids);
|
||
|
||
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);
|
||
|
||
return $value === '' ? null : $value;
|
||
}
|
||
}
|