Files
Toiminnanohjaus/app/Modules/Jobs/Domain/Entities/Task.php
T
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

265 lines
7.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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 under one work
* type. Its assignee (tekijä) does it: the planned schedule (planned
* from–to) places it on the assignee's row of the task calendar and books
* the estimate from their days. Members (jäsenet) see the task among their
* own but get no bookings. Time entries are recorded on tasks.
*/
final class Task
{
/**
* @param list<int> $memberUserIds
*/
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 ?int $assigneeUserId,
private array $memberUserIds,
private int $createdBy,
private ?DateTimeImmutable $plannedFrom = null,
private ?DateTimeImmutable $plannedTo = null,
) {
$this->memberUserIds = self::members($memberUserIds, $assigneeUserId);
self::checkPlan($plannedFrom, $plannedTo);
}
/**
* @param list<int> $memberUserIds
*
* @throws TasksNotAllowedYet
* @throws PlanEndsBeforeStart
*/
public static function create(
int $jobId,
JobStatus $jobStatus,
int $workTypeId,
TaskTitle $title,
?string $description,
TaskStatus $status,
?DateTimeImmutable $dueOn,
?EstimatedHours $estimatedHours,
?int $assigneeUserId,
array $memberUserIds,
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, $assigneeUserId, $memberUserIds, $createdBy, ...self::plan($plannedFrom, $plannedTo));
}
/**
* @param list<int> $memberUserIds
*/
public static function reconstitute(
int $id,
int $jobId,
int $workTypeId,
TaskTitle $title,
?string $description,
TaskStatus $status,
?DateTimeImmutable $dueOn,
?EstimatedHours $estimatedHours,
?int $assigneeUserId,
array $memberUserIds,
int $createdBy,
?DateTimeImmutable $plannedFrom = null,
?DateTimeImmutable $plannedTo = null,
): self {
return new self($id, $jobId, $workTypeId, $title, $description, $status, $dueOn, $estimatedHours, $assigneeUserId, $memberUserIds, $createdBy, $plannedFrom, $plannedTo);
}
/**
* @param list<int> $memberUserIds
*/
public function changeDetails(
int $workTypeId,
TaskTitle $title,
?string $description,
TaskStatus $status,
?DateTimeImmutable $dueOn,
?EstimatedHours $estimatedHours,
?int $assigneeUserId,
array $memberUserIds,
): void {
$this->workTypeId = $workTypeId;
$this->title = $title;
$this->description = self::text($description);
$this->status = $status;
$this->dueOn = $dueOn;
$this->estimatedHours = $estimatedHours;
$this->assigneeUserId = $assigneeUserId;
$this->memberUserIds = self::members($memberUserIds, $assigneeUserId);
}
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);
}
/**
* Makes the user the assignee (null: no assignee). A member who becomes
* the assignee is no longer a member; the earlier assignee is dropped.
*/
public function assignTo(?int $userId): void
{
$this->assigneeUserId = $userId;
$this->memberUserIds = self::members($this->memberUserIds, $userId);
}
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;
}
public function assigneeUserId(): ?int
{
return $this->assigneeUserId;
}
/**
* @return list<int>
*/
public function memberUserIds(): array
{
return $this->memberUserIds;
}
public function createdBy(): int
{
return $this->createdBy;
}
/**
* The members sorted, without duplicates and without the assignee.
*
* @param list<int> $ids
* @return list<int>
*/
private static function members(array $ids, ?int $assigneeUserId): array
{
$ids = array_values(array_unique(array_filter($ids, fn (int $id): bool => $id !== $assigneeUserId)));
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;
}
}