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,48 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\Contracts;
use App\Modules\Machines\Application\DTOs\DayLoad;
use App\Modules\Machines\Application\DTOs\MachineSummary;
use App\Modules\Machines\Application\DTOs\QueueRow;
interface MachineQueries
{
/**
* @return list<MachineSummary> By name.
*/
public function machines(bool $includeInactive = false): array;
public function findMachine(int $id): ?MachineSummary;
/**
* Active machines serving any of the service units.
*
* @param list<int> $serviceUnitIds
* @return list<MachineSummary>
*/
public function machinesFor(array $serviceUnitIds): array;
/**
* The task's open row on a machine's work list, without task details.
*/
public function rowForTask(int $taskId): ?QueueRow;
/**
* A machine's work list for the day: quick sale orders first, then on
* today's list the late rows of earlier days, then the day's rows in
* order. Rows on days after today are listed for viewing only.
*
* @return list<QueueRow>
*/
public function rowsOn(int $machineId, string $day, string $today): array;
/**
* The machine's planned rows and estimated hours on the days.
*
* @return list<DayLoad>
*/
public function load(int $machineId, string $from, int $days): array;
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\Contracts;
use App\Modules\Machines\Application\DTOs\MachineTaskInfo;
/**
* Machines' view of job tasks, read from and changed through the Jobs
* module's application contracts.
*/
interface MachineTasks
{
public function info(int $taskId): ?MachineTaskInfo;
/**
* @param list<int> $taskIds
* @return array<int, MachineTaskInfo> By task id.
*/
public function infos(array $taskIds): array;
/**
* Changes a task's status ("in_progress", "in_review" or "done").
*/
public function changeStatus(int $taskId, string $status): void;
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\DTOs;
use App\Modules\Machines\Domain\Enums\CounterUnit;
/**
* A machine counter with its latest reading (in thousandths of the unit),
* read at "Y-m-d H:i" in the company's time zone.
*/
final readonly class CounterSummary
{
public function __construct(
public int $id,
public string $name,
public CounterUnit $unit,
public ?int $readingMilli = null,
public ?string $readAt = null,
) {}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\DTOs;
/**
* A machine's planned work on a day: the number of rows and the tasks'
* estimated hours together (in quarters of an hour).
*/
final readonly class DayLoad
{
public function __construct(
public string $day,
public int $rows,
public int $estimatedQuarters,
) {}
}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\DTOs;
/**
* A machine's details as entered. Counters without an id are new.
*/
final readonly class MachineData
{
/**
* @param list<int> $serviceUnitIds
* @param list<array{id: ?int, name: string, unit: string}> $counters
*/
public function __construct(
public string $name,
public string $type,
public ?string $description,
public array $serviceUnitIds,
public ?int $responsibleUserId,
public array $counters = [],
) {}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\DTOs;
use App\Modules\Machines\Domain\Enums\MachineType;
final readonly class MachineSummary
{
/**
* @param list<int> $serviceUnitIds
* @param list<string> $serviceUnitNames
* @param list<CounterSummary> $counters
*/
public function __construct(
public int $id,
public string $name,
public MachineType $type,
public ?string $description,
public array $serviceUnitIds,
public array $serviceUnitNames,
public ?int $responsibleUserId,
public ?string $responsibleName,
public array $counters,
public bool $isActive,
) {}
}
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\DTOs;
/**
* A job's task as a machine's work list shows it, read from the Jobs and
* Catalog modules. $status and $priority are Jobs' enum keys; $priorityRank
* orders priorities (higher first). $allowedServiceUnitIds are the service
* units using stock whose machines may make the task: the task's work
* type's unit, or for a shared work type the job's units.
*/
final readonly class MachineTaskInfo
{
/**
* @param list<int> $allowedServiceUnitIds
*/
public function __construct(
public int $taskId,
public int $jobId,
public string $jobNumber,
public string $jobName,
public string $customerName,
public string $title,
public ?string $description,
public ?string $instructions,
public string $status,
public string $priority,
public int $priorityRank,
public ?string $dueOn,
public ?string $plannedFrom,
public ?string $plannedTo,
public ?string $estimatedHours,
public array $allowedServiceUnitIds,
public bool $isDone,
) {}
/**
* Whether making the task on the day is outside its schedule or after
* its due date (spec 19.4: shown as a note, not prevented).
*/
public function isOutsidePlan(string $day): bool
{
return ($this->plannedFrom !== null && $day < $this->plannedFrom)
|| ($this->plannedTo !== null && $day > $this->plannedTo)
|| ($this->dueOn !== null && $day > $this->dueOn);
}
}
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\DTOs;
/**
* A row of a machine's work list: a task (with its details) or a step of
* a quick sale order line. $plannedOn is Y-m-d; a late row was planned on
* an earlier day and is still open.
*/
final readonly class QueueRow
{
public function __construct(
public int $entryId,
public int $machineId,
public string $plannedOn,
public int $position,
public bool $isLate,
public ?int $taskId,
public ?int $orderLineId,
public ?int $stepPosition,
public ?MachineTaskInfo $task = null,
) {}
public function isOrder(): bool
{
return $this->orderLineId !== null;
}
}
@@ -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;
}
}
@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Application\UseCases;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Machines\Application\DTOs\MachineData;
use App\Modules\Machines\Domain\Contracts\MachineRepository;
use App\Modules\Machines\Domain\Entities\Machine;
use App\Modules\Machines\Domain\Enums\CounterUnit;
use App\Modules\Machines\Domain\Enums\MachineType;
use App\Modules\Machines\Domain\Exceptions\InvalidCounter;
use App\Modules\Machines\Domain\Exceptions\InvalidMachineName;
use App\Modules\Machines\Domain\Exceptions\MachineNameTaken;
use App\Modules\Machines\Domain\Exceptions\MachineNeedsServiceUnit;
use App\Modules\Machines\Domain\Exceptions\MachineNotFound;
use App\Modules\Machines\Domain\ValueObjects\MachineCounter;
use App\Modules\Machines\Domain\ValueObjects\MachineName;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Adds or changes a machine (management, spec 19.4). Its service units are
* chosen among those that use stock; a unit it already has may stay after
* the setting is turned off.
*/
final class SaveMachine
{
public function __construct(
private MachineRepository $machines,
private CatalogQueries $catalog,
private TransactionManager $transactions,
) {}
/**
* @throws InvalidMachineName
* @throws InvalidCounter
* @throws MachineNameTaken
* @throws MachineNeedsServiceUnit
*/
public function add(MachineData $data): int
{
[$name, $type, $counters] = $this->parse($data);
$unitIds = $this->allowedUnits($data->serviceUnitIds, []);
return $this->transactions->run(function () use ($data, $name, $type, $counters, $unitIds): int {
if ($this->machines->isNameTaken($name->value)) {
throw new MachineNameTaken;
}
$machine = Machine::create($name, $type, $data->description, $unitIds, $data->responsibleUserId, $counters);
return (int) $this->machines->save($machine)->id();
});
}
/**
* @throws MachineNotFound
* @throws InvalidMachineName
* @throws InvalidCounter
* @throws MachineNameTaken
* @throws MachineNeedsServiceUnit
*/
public function update(int $machineId, MachineData $data): void
{
[$name, $type, $counters] = $this->parse($data);
$this->transactions->run(function () use ($machineId, $data, $name, $type, $counters): void {
$machine = $this->machines->findById($machineId) ?? throw new MachineNotFound;
if ($this->machines->isNameTaken($name->value, $machineId)) {
throw new MachineNameTaken;
}
$machine->change($name, $type, $data->description, $this->allowedUnits($data->serviceUnitIds, $machine->serviceUnitIds()), $data->responsibleUserId, $counters);
$this->machines->save($machine);
});
}
/**
* @throws MachineNotFound
*/
public function setActive(int $machineId, bool $active): void
{
$this->transactions->run(function () use ($machineId, $active): void {
$machine = $this->machines->findById($machineId) ?? throw new MachineNotFound;
$active ? $machine->activate() : $machine->deactivate();
$this->machines->save($machine);
});
}
/**
* @return array{0: MachineName, 1: MachineType, 2: list<MachineCounter>}
*
* @throws InvalidMachineName
* @throws InvalidCounter
*/
private function parse(MachineData $data): array
{
$counters = [];
foreach ($data->counters as $counter) {
$unit = CounterUnit::tryFrom($counter['unit']) ?? throw new InvalidCounter;
$counters[] = new MachineCounter($counter['id'], $counter['name'], $unit);
}
return [new MachineName($data->name), MachineType::tryFrom($data->type) ?? MachineType::Other, $counters];
}
/**
* @param list<int> $requested
* @param list<int> $current
* @return list<int>
*/
private function allowedUnits(array $requested, array $current): array
{
$stockUnitIds = $this->catalog->stockServiceUnitIds();
return array_values(array_filter(
array_unique($requested),
fn (int $id): bool => in_array($id, $stockUnitIds, true) || in_array($id, $current, true),
));
}
}