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:
@@ -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),
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user