320 lines
11 KiB
PHP
320 lines
11 KiB
PHP
<?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');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Puts a step of a quick sale order line on the machine's list for the
|
|
* day (spec 19.7); customer waiting rows are listed first.
|
|
*/
|
|
public function enqueueOrderStep(int $orderLineId, int $stepPosition, int $machineId, DateTimeImmutable $day): void
|
|
{
|
|
$this->transactions->run(function () use ($orderLineId, $stepPosition, $machineId, $day): void {
|
|
$open = $this->queue->openEntryForOrderLine($orderLineId);
|
|
|
|
if ($open !== null) {
|
|
$this->queue->delete($open);
|
|
}
|
|
|
|
$this->queue->save(QueueEntry::forOrderLine($machineId, $orderLineId, $stepPosition, $day, 0));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Closes an order line's open row as made.
|
|
*/
|
|
public function completeOrderLine(int $orderLineId, ?int $actorUserId): void
|
|
{
|
|
$this->transactions->run(function () use ($orderLineId, $actorUserId): void {
|
|
$entry = $this->queue->openEntryForOrderLine($orderLineId);
|
|
|
|
if ($entry !== null) {
|
|
$entry->complete($actorUserId, $this->clock->now());
|
|
$this->queue->save($entry);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Takes an order line's open row off its list (a cancelled order).
|
|
*/
|
|
public function removeOrderLine(int $orderLineId): void
|
|
{
|
|
$this->transactions->run(function () use ($orderLineId): void {
|
|
$entry = $this->queue->openEntryForOrderLine($orderLineId);
|
|
|
|
if ($entry !== null) {
|
|
$this->queue->delete($entry);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Moves an order line's open row to another machine of the same type.
|
|
*
|
|
* @throws QueueEntryNotFound
|
|
* @throws MachineNotFound
|
|
* @throws MachineInactive
|
|
* @throws MachineNotForTask when the machine is of another type
|
|
*/
|
|
public function moveOrderLine(int $entryId, int $machineId): void
|
|
{
|
|
$this->transactions->run(function () use ($entryId, $machineId): void {
|
|
$entry = $this->queue->findById($entryId) ?? throw new QueueEntryNotFound;
|
|
|
|
if (! $entry->isOrderLine()) {
|
|
throw new QueueEntryNotFound;
|
|
}
|
|
|
|
$current = $this->machines->findById($entry->machineId()) ?? throw new MachineNotFound;
|
|
$target = $this->machines->findById($machineId) ?? throw new MachineNotFound;
|
|
$target->ensureActive();
|
|
|
|
if ($target->type() !== $current->type()) {
|
|
throw new MachineNotForTask;
|
|
}
|
|
|
|
$entry->plan($machineId, $entry->plannedOn(), 0);
|
|
$this->queue->save($entry);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|