Add machines with day-by-day work lists for job tasks

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:43:55 +02:00
co-authored by Claude Sonnet 5.5
parent 0de0e332b2
commit aac579257a
59 changed files with 3803 additions and 1 deletions
@@ -0,0 +1,243 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\UseCases;
use App\Modules\Machines\Application\Contracts\MachineTasks;
use App\Modules\Machines\Domain\Contracts\MachineRepository;
use App\Modules\Machines\Domain\Contracts\QueueRepository;
use App\Modules\Machines\Domain\Entities\QueueEntry;
use App\Modules\Machines\Domain\Exceptions\InvalidPlannedDay;
use App\Modules\Machines\Domain\Exceptions\MachineInactive;
use App\Modules\Machines\Domain\Exceptions\MachineNotForTask;
use App\Modules\Machines\Domain\Exceptions\MachineNotFound;
use App\Modules\Machines\Domain\Exceptions\MachineTaskNotFound;
use App\Modules\Machines\Domain\Exceptions\QueueEntryCompleted;
use App\Modules\Machines\Domain\Exceptions\QueueEntryNotFound;
use App\Modules\Machines\Domain\Services\QueueOrder;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
use DateTimeImmutable;
/**
* A machine's day-by-day work list (spec 19.4): a job's task is planned on
* a machine of its service unit for a day (by default the start of its
* schedule, else today), moved to another day or machine, taken off, or
* moved up and down among the day's rows. The task stays a task of its
* job with its assignee, schedule and hours. A finished row leaves the
* list, and finishing a task's row marks the task done.
*/
final class MachineWorkList
{
public function __construct(
private MachineRepository $machines,
private QueueRepository $queue,
private MachineTasks $tasks,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @param string|null $day Y-m-d; null: the start of the task's schedule if not past, else today.
*
* @throws MachineTaskNotFound
* @throws MachineNotFound
* @throws MachineInactive
* @throws MachineNotForTask
* @throws InvalidPlannedDay
*/
public function planTask(int $taskId, int $machineId, ?string $day): void
{
$task = $this->tasks->info($taskId) ?? throw new MachineTaskNotFound;
if ($task->isDone) {
throw new MachineTaskNotFound;
}
$today = $this->clock->today()->format('Y-m-d');
$plannedOn = $this->day($day ?? ($task->plannedFrom !== null && $task->plannedFrom > $today ? $task->plannedFrom : $today));
$this->transactions->run(function () use ($task, $taskId, $machineId, $plannedOn): void {
$machine = $this->machines->findById($machineId) ?? throw new MachineNotFound;
$machine->ensureActive();
if (! $machine->servesAnyOf($task->allowedServiceUnitIds)) {
throw new MachineNotForTask;
}
$entry = $this->queue->openEntryForTask($taskId);
if ($entry !== null) {
$this->takeOut($entry);
}
$rows = array_values(array_filter(
$this->dayRows($machineId, $plannedOn),
fn (array $row): bool => $entry === null || $row['entry']->id() !== $entry->id(),
));
$index = QueueOrder::insertionIndex(array_map(fn (array $row): array => $row['order'], $rows), $task->priorityRank, $task->dueOn);
$entry ??= QueueEntry::forTask($machineId, $taskId, $plannedOn, 0);
$entry->plan($machineId, $plannedOn, 0);
$entries = array_map(fn (array $row): QueueEntry => $row['entry'], $rows);
array_splice($entries, $index, 0, [$entry]);
$this->renumber($entries);
});
}
/**
* Takes the task off its machine's list.
*/
public function removeTask(int $taskId): void
{
$this->transactions->run(function () use ($taskId): void {
$entry = $this->queue->openEntryForTask($taskId);
if ($entry !== null) {
$this->takeOut($entry);
$this->queue->delete($entry);
}
});
}
/**
* Moves a row one place up or down among its day's rows.
*
* @return bool False when it already was first (up) or last.
*
* @throws QueueEntryNotFound
* @throws QueueEntryCompleted
*/
public function move(int $entryId, bool $up): bool
{
return $this->transactions->run(function () use ($entryId, $up): bool {
$entry = $this->queue->findById($entryId) ?? throw new QueueEntryNotFound;
$entry->ensureOpen();
$entries = $this->queue->openEntriesOn($entry->machineId(), $entry->plannedOn());
$index = $this->indexOf($entries, $entryId);
$target = $up ? $index - 1 : $index + 1;
if (! isset($entries[$target])) {
return false;
}
[$entries[$index], $entries[$target]] = [$entries[$target], $entries[$index]];
$this->renumber($entries);
return true;
});
}
/**
* Finishes a task's row from the machine: the row leaves the list and
* the task is marked done.
*
* @throws QueueEntryNotFound
* @throws QueueEntryCompleted
*/
public function finish(int $entryId, int $actorUserId): void
{
$taskId = $this->transactions->run(function () use ($entryId, $actorUserId): ?int {
$entry = $this->queue->findById($entryId) ?? throw new QueueEntryNotFound;
$entry->complete($actorUserId, $this->clock->now());
$this->queue->save($entry);
return $entry->taskId();
});
if ($taskId !== null) {
$this->tasks->changeStatus($taskId, 'done');
}
}
/**
* Closes the open row of a task that was marked done elsewhere.
*/
public function closeTask(int $taskId): void
{
$this->transactions->run(function () use ($taskId): void {
$entry = $this->queue->openEntryForTask($taskId);
if ($entry !== null) {
$entry->complete(null, $this->clock->now());
$this->queue->save($entry);
}
});
}
/**
* Removes the entry from its day's order and closes the gap.
*/
private function takeOut(QueueEntry $entry): void
{
$remaining = array_values(array_filter(
$this->queue->openEntriesOn($entry->machineId(), $entry->plannedOn()),
fn (QueueEntry $other): bool => $other->id() !== $entry->id(),
));
$this->renumber($remaining);
}
/**
* The day's open rows with what ordering needs.
*
* @return list<array{entry: QueueEntry, order: array{isOrder: bool, rank: int, dueOn: ?string}}>
*/
private function dayRows(int $machineId, DateTimeImmutable $day): array
{
$entries = $this->queue->openEntriesOn($machineId, $day);
$taskIds = array_values(array_filter(array_map(fn (QueueEntry $entry): ?int => $entry->taskId(), $entries)));
$infos = $this->tasks->infos($taskIds);
return array_map(function (QueueEntry $entry) use ($infos): array {
$info = $entry->taskId() === null ? null : ($infos[$entry->taskId()] ?? null);
return [
'entry' => $entry,
'order' => ['isOrder' => $entry->isOrderLine(), 'rank' => $info->priorityRank ?? 0, 'dueOn' => $info?->dueOn],
];
}, $entries);
}
/**
* @param list<QueueEntry> $entries
*/
private function renumber(array $entries): void
{
foreach ($entries as $index => $entry) {
$entry->moveTo($index + 1);
$this->queue->save($entry);
}
}
/**
* @param list<QueueEntry> $entries
*/
private function indexOf(array $entries, int $entryId): int
{
foreach ($entries as $index => $entry) {
if ($entry->id() === $entryId) {
return $index;
}
}
return -1;
}
/**
* @throws InvalidPlannedDay
*/
private function day(string $value): DateTimeImmutable
{
$day = DateTimeImmutable::createFromFormat('!Y-m-d', $value);
if ($day === false || $day->format('Y-m-d') !== $value) {
throw new InvalidPlannedDay;
}
return $day;
}
}