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:
@@ -6,14 +6,21 @@ namespace App\Modules\Jobs\Application\UseCases;
|
|||||||
|
|
||||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||||
|
use App\Modules\Jobs\Domain\Events\TaskCompleted;
|
||||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||||
|
use App\Modules\Shared\Application\Contracts\EventDispatcher;
|
||||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes a task's status. A task newly marked done is published as
|
||||||
|
* completed, so that e.g. a machine's work list closes its row.
|
||||||
|
*/
|
||||||
final class ChangeTaskStatus
|
final class ChangeTaskStatus
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private TaskRepository $tasks,
|
private TaskRepository $tasks,
|
||||||
private TransactionManager $transactions,
|
private TransactionManager $transactions,
|
||||||
|
private EventDispatcher $events,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -23,8 +30,14 @@ final class ChangeTaskStatus
|
|||||||
{
|
{
|
||||||
$this->transactions->run(function () use ($jobId, $taskId, $status): void {
|
$this->transactions->run(function () use ($jobId, $taskId, $status): void {
|
||||||
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
||||||
|
$wasDone = $task->status() === TaskStatus::Done;
|
||||||
|
|
||||||
$task->changeStatus($status);
|
$task->changeStatus($status);
|
||||||
$this->tasks->save($task);
|
$this->tasks->save($task);
|
||||||
|
|
||||||
|
if (! $wasDone && $status === TaskStatus::Done) {
|
||||||
|
$this->events->dispatch(new TaskCompleted($taskId, $jobId));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
|||||||
use App\Modules\Jobs\Domain\Entities\Task;
|
use App\Modules\Jobs\Domain\Entities\Task;
|
||||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||||
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
|
use App\Modules\Jobs\Domain\Events\PeopleAddedToTask;
|
||||||
|
use App\Modules\Jobs\Domain\Events\TaskCompleted;
|
||||||
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
||||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||||
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
|
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
|
||||||
@@ -49,6 +50,7 @@ final class UpdateTask
|
|||||||
|
|
||||||
$before = self::planningInput($task);
|
$before = self::planningInput($task);
|
||||||
$peopleBefore = PeopleAddedToTask::peopleOf($task);
|
$peopleBefore = PeopleAddedToTask::peopleOf($task);
|
||||||
|
$wasDone = $task->status() === TaskStatus::Done;
|
||||||
|
|
||||||
$task->changeDetails(
|
$task->changeDetails(
|
||||||
$data->workTypeId,
|
$data->workTypeId,
|
||||||
@@ -75,6 +77,10 @@ final class UpdateTask
|
|||||||
if ($added !== null) {
|
if ($added !== null) {
|
||||||
$this->events->dispatch($added);
|
$this->events->dispatch($added);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (! $wasDone && $task->status() === TaskStatus::Done) {
|
||||||
|
$this->events->dispatch(new TaskCompleted($taskId, $jobId));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Jobs\Domain\Events;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A task was marked done (valmis). A machine's work list closes the task's
|
||||||
|
* row on it (spec 19.4).
|
||||||
|
*/
|
||||||
|
final readonly class TaskCompleted
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $taskId,
|
||||||
|
public int $jobId,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -476,6 +476,7 @@ new class extends Component
|
|||||||
|
|
||||||
<livewire:jobs::task-images :job-id="$job->id" :task-id="$task->id" :task-title="$task->title" :wire:key="'images-'.$task->id" />
|
<livewire:jobs::task-images :job-id="$job->id" :task-id="$task->id" :task-title="$task->title" :wire:key="'images-'.$task->id" />
|
||||||
<livewire:inventory::task-materials :job-id="$job->id" :task-id="$task->id" :task-title="$task->title" :wire:key="'materials-'.$task->id" />
|
<livewire:inventory::task-materials :job-id="$job->id" :task-id="$task->id" :task-title="$task->title" :wire:key="'materials-'.$task->id" />
|
||||||
|
<livewire:machines::task-machine :job-id="$job->id" :task-id="$task->id" :task-title="$task->title" :wire:key="'machine-'.$task->id" />
|
||||||
|
|
||||||
<div class="flex flex-wrap items-end gap-3">
|
<div class="flex flex-wrap items-end gap-3">
|
||||||
@if ($canChangeStatus)
|
@if ($canChangeStatus)
|
||||||
|
|||||||
@@ -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),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Contracts;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Entities\Machine;
|
||||||
|
|
||||||
|
interface MachineRepository
|
||||||
|
{
|
||||||
|
public function findById(int $id): ?Machine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the machine with its service units and counters; counters left
|
||||||
|
* out are removed when nothing refers to them.
|
||||||
|
*/
|
||||||
|
public function save(Machine $machine): Machine;
|
||||||
|
|
||||||
|
public function isNameTaken(string $name, ?int $exceptId = null): bool;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Contracts;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Entities\QueueEntry;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
|
||||||
|
interface QueueRepository
|
||||||
|
{
|
||||||
|
public function findById(int $id): ?QueueEntry;
|
||||||
|
|
||||||
|
public function openEntryForTask(int $taskId): ?QueueEntry;
|
||||||
|
|
||||||
|
public function openEntryForOrderLine(int $orderLineId): ?QueueEntry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The machine's open rows planned on the day, in position order.
|
||||||
|
*
|
||||||
|
* @return list<QueueEntry>
|
||||||
|
*/
|
||||||
|
public function openEntriesOn(int $machineId, DateTimeImmutable $day): array;
|
||||||
|
|
||||||
|
public function save(QueueEntry $entry): QueueEntry;
|
||||||
|
|
||||||
|
public function delete(QueueEntry $entry): void;
|
||||||
|
}
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Entities;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Enums\MachineType;
|
||||||
|
use App\Modules\Machines\Domain\Exceptions\MachineInactive;
|
||||||
|
use App\Modules\Machines\Domain\Exceptions\MachineNeedsServiceUnit;
|
||||||
|
use App\Modules\Machines\Domain\ValueObjects\MachineCounter;
|
||||||
|
use App\Modules\Machines\Domain\ValueObjects\MachineName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A machine (kone, spec 19.4): a printing press, a sticker cutter or other,
|
||||||
|
* in one or more service units, with a responsible person who gets the
|
||||||
|
* maintenance notifications, and counters. Machines are never deleted: one
|
||||||
|
* out of use is not offered, but its history stays.
|
||||||
|
*/
|
||||||
|
final class Machine
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param list<int> $serviceUnitIds
|
||||||
|
* @param list<MachineCounter> $counters
|
||||||
|
*/
|
||||||
|
private function __construct(
|
||||||
|
private ?int $id,
|
||||||
|
private MachineName $name,
|
||||||
|
private MachineType $type,
|
||||||
|
private ?string $description,
|
||||||
|
private array $serviceUnitIds,
|
||||||
|
private ?int $responsibleUserId,
|
||||||
|
private array $counters,
|
||||||
|
private bool $active,
|
||||||
|
) {
|
||||||
|
if ($serviceUnitIds === []) {
|
||||||
|
throw new MachineNeedsServiceUnit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<int> $serviceUnitIds
|
||||||
|
* @param list<MachineCounter> $counters
|
||||||
|
*
|
||||||
|
* @throws MachineNeedsServiceUnit
|
||||||
|
*/
|
||||||
|
public static function create(MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters): self
|
||||||
|
{
|
||||||
|
return new self(null, $name, $type, self::text($description), array_values(array_unique($serviceUnitIds)), $responsibleUserId, $counters, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<int> $serviceUnitIds
|
||||||
|
* @param list<MachineCounter> $counters
|
||||||
|
*/
|
||||||
|
public static function reconstitute(int $id, MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters, bool $active): self
|
||||||
|
{
|
||||||
|
return new self($id, $name, $type, $description, $serviceUnitIds, $responsibleUserId, $counters, $active);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<int> $serviceUnitIds
|
||||||
|
* @param list<MachineCounter> $counters
|
||||||
|
*
|
||||||
|
* @throws MachineNeedsServiceUnit
|
||||||
|
*/
|
||||||
|
public function change(MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters): void
|
||||||
|
{
|
||||||
|
if ($serviceUnitIds === []) {
|
||||||
|
throw new MachineNeedsServiceUnit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->name = $name;
|
||||||
|
$this->type = $type;
|
||||||
|
$this->description = self::text($description);
|
||||||
|
$this->serviceUnitIds = array_values(array_unique($serviceUnitIds));
|
||||||
|
$this->responsibleUserId = $responsibleUserId;
|
||||||
|
$this->counters = $counters;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the machine serves any of the service units.
|
||||||
|
*
|
||||||
|
* @param list<int> $serviceUnitIds
|
||||||
|
*/
|
||||||
|
public function servesAnyOf(array $serviceUnitIds): bool
|
||||||
|
{
|
||||||
|
return array_intersect($this->serviceUnitIds, $serviceUnitIds) !== [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws MachineInactive
|
||||||
|
*/
|
||||||
|
public function ensureActive(): void
|
||||||
|
{
|
||||||
|
if (! $this->active) {
|
||||||
|
throw new MachineInactive;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function activate(): void
|
||||||
|
{
|
||||||
|
$this->active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deactivate(): void
|
||||||
|
{
|
||||||
|
$this->active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function id(): ?int
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function name(): MachineName
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function type(): MachineType
|
||||||
|
{
|
||||||
|
return $this->type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function description(): ?string
|
||||||
|
{
|
||||||
|
return $this->description;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<int>
|
||||||
|
*/
|
||||||
|
public function serviceUnitIds(): array
|
||||||
|
{
|
||||||
|
return $this->serviceUnitIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function responsibleUserId(): ?int
|
||||||
|
{
|
||||||
|
return $this->responsibleUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<MachineCounter>
|
||||||
|
*/
|
||||||
|
public function counters(): array
|
||||||
|
{
|
||||||
|
return $this->counters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isActive(): bool
|
||||||
|
{
|
||||||
|
return $this->active;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function text(?string $value): ?string
|
||||||
|
{
|
||||||
|
$value = $value === null ? '' : trim($value);
|
||||||
|
|
||||||
|
return $value === '' ? null : mb_substr($value, 0, 5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Entities;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Exceptions\QueueEntryCompleted;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A row of a machine's work list (spec 19.4): a job's task, or a step of a
|
||||||
|
* quick sale order line, to be made on the machine on its planned day, in
|
||||||
|
* its position among that day's rows. A row not made on its day stays open
|
||||||
|
* and shows as late on the following days. Completed rows leave the list.
|
||||||
|
*/
|
||||||
|
final class QueueEntry
|
||||||
|
{
|
||||||
|
private function __construct(
|
||||||
|
private ?int $id,
|
||||||
|
private int $machineId,
|
||||||
|
private DateTimeImmutable $plannedOn,
|
||||||
|
private int $position,
|
||||||
|
private ?int $taskId,
|
||||||
|
private ?int $orderLineId,
|
||||||
|
private ?int $stepPosition,
|
||||||
|
private ?DateTimeImmutable $completedAt,
|
||||||
|
private ?int $completedBy,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public static function forTask(int $machineId, int $taskId, DateTimeImmutable $plannedOn, int $position): self
|
||||||
|
{
|
||||||
|
return new self(null, $machineId, $plannedOn, $position, $taskId, null, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function forOrderLine(int $machineId, int $orderLineId, int $stepPosition, DateTimeImmutable $plannedOn, int $position): self
|
||||||
|
{
|
||||||
|
return new self(null, $machineId, $plannedOn, $position, null, $orderLineId, $stepPosition, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function reconstitute(int $id, int $machineId, DateTimeImmutable $plannedOn, int $position, ?int $taskId, ?int $orderLineId, ?int $stepPosition, ?DateTimeImmutable $completedAt, ?int $completedBy): self
|
||||||
|
{
|
||||||
|
return new self($id, $machineId, $plannedOn, $position, $taskId, $orderLineId, $stepPosition, $completedAt, $completedBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws QueueEntryCompleted
|
||||||
|
*/
|
||||||
|
public function plan(int $machineId, DateTimeImmutable $plannedOn, int $position): void
|
||||||
|
{
|
||||||
|
$this->ensureOpen();
|
||||||
|
$this->machineId = $machineId;
|
||||||
|
$this->plannedOn = $plannedOn;
|
||||||
|
$this->position = $position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function moveTo(int $position): void
|
||||||
|
{
|
||||||
|
$this->position = $position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int|null $userId Null when the task was marked done elsewhere.
|
||||||
|
*
|
||||||
|
* @throws QueueEntryCompleted
|
||||||
|
*/
|
||||||
|
public function complete(?int $userId, DateTimeImmutable $at): void
|
||||||
|
{
|
||||||
|
$this->ensureOpen();
|
||||||
|
$this->completedBy = $userId;
|
||||||
|
$this->completedAt = $at;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws QueueEntryCompleted
|
||||||
|
*/
|
||||||
|
public function ensureOpen(): void
|
||||||
|
{
|
||||||
|
if ($this->completedAt !== null) {
|
||||||
|
throw new QueueEntryCompleted;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isOrderLine(): bool
|
||||||
|
{
|
||||||
|
return $this->orderLineId !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function id(): ?int
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function machineId(): int
|
||||||
|
{
|
||||||
|
return $this->machineId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function plannedOn(): DateTimeImmutable
|
||||||
|
{
|
||||||
|
return $this->plannedOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function position(): int
|
||||||
|
{
|
||||||
|
return $this->position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function taskId(): ?int
|
||||||
|
{
|
||||||
|
return $this->taskId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function orderLineId(): ?int
|
||||||
|
{
|
||||||
|
return $this->orderLineId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stepPosition(): ?int
|
||||||
|
{
|
||||||
|
return $this->stepPosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function completedAt(): ?DateTimeImmutable
|
||||||
|
{
|
||||||
|
return $this->completedAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function completedBy(): ?int
|
||||||
|
{
|
||||||
|
return $this->completedBy;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The unit of a machine counter (spec 19.5): prints, sheets, printed area,
|
||||||
|
* ink used, cut length or running hours.
|
||||||
|
*/
|
||||||
|
enum CounterUnit: string
|
||||||
|
{
|
||||||
|
case Piece = 'piece';
|
||||||
|
case Sheet = 'sheet';
|
||||||
|
case SquareMetre = 'square_metre';
|
||||||
|
case Millilitre = 'millilitre';
|
||||||
|
case Metre = 'metre';
|
||||||
|
case Hour = 'hour';
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The kind of machine (spec 19.4): printing presses that use ink or toner
|
||||||
|
* (e.g. wide-format printers), sticker cutters, and others. Labels are in
|
||||||
|
* lang/*\/machines.php under types.
|
||||||
|
*/
|
||||||
|
enum MachineType: string
|
||||||
|
{
|
||||||
|
case Printer = 'printer';
|
||||||
|
case Cutter = 'cutter';
|
||||||
|
case Other = 'other';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The counters suggested for a new machine of this type (spec 19.5), as
|
||||||
|
* translation keys of their names with their units.
|
||||||
|
*
|
||||||
|
* @return list<array{name: string, unit: CounterUnit}>
|
||||||
|
*/
|
||||||
|
public function suggestedCounters(): array
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::Printer => [
|
||||||
|
['name' => 'prints', 'unit' => CounterUnit::Piece],
|
||||||
|
['name' => 'printed_area', 'unit' => CounterUnit::SquareMetre],
|
||||||
|
],
|
||||||
|
self::Cutter => [
|
||||||
|
['name' => 'cut_length', 'unit' => CounterUnit::Metre],
|
||||||
|
],
|
||||||
|
self::Other => [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class InvalidCounter extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('A counter needs a name of 1-100 characters and a unit.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class InvalidMachineName extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('A machine name must be 1-150 characters.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class InvalidPlannedDay extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('The planned day must be a valid date.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class MachineInactive extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('The machine is no longer in use.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class MachineNameTaken extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('The machine name is already in use.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class MachineNeedsServiceUnit extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('A machine needs at least one service unit that uses stock.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class MachineNotForTask extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('The machine does not belong to the service unit of the task.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class MachineNotFound extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('The machine was not found.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class MachineTaskNotFound extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('The task was not found.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class QueueEntryCompleted extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('The work list row has already been completed.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Exceptions;
|
||||||
|
|
||||||
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||||
|
|
||||||
|
final class QueueEntryNotFound extends DomainException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct('The work list row was not found.');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\Services;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Where a task goes in a day's work list by default (spec 19.4): after
|
||||||
|
* quick sale orders, then by priority (higher first) and due date (earlier
|
||||||
|
* first, none last). Rows moved by hand keep their places; a new task
|
||||||
|
* goes before the first task it outranks.
|
||||||
|
*/
|
||||||
|
final class QueueOrder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param list<array{isOrder: bool, rank: int, dueOn: ?string}> $rows The day's open rows in order.
|
||||||
|
* @return int The index to insert the new task at.
|
||||||
|
*/
|
||||||
|
public static function insertionIndex(array $rows, int $rank, ?string $dueOn): int
|
||||||
|
{
|
||||||
|
foreach ($rows as $index => $row) {
|
||||||
|
if ($row['isOrder']) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self::before($rank, $dueOn, $row['rank'], $row['dueOn'])) {
|
||||||
|
return $index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count($rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function before(int $rank, ?string $dueOn, int $otherRank, ?string $otherDueOn): bool
|
||||||
|
{
|
||||||
|
if ($rank !== $otherRank) {
|
||||||
|
return $rank > $otherRank;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($dueOn === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $otherDueOn === null || $dueOn < $otherDueOn;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\ValueObjects;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Enums\CounterUnit;
|
||||||
|
use App\Modules\Machines\Domain\Exceptions\InvalidCounter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A counter of a machine (laskuri, spec 19.5) by which maintenance needs
|
||||||
|
* are measured, e.g. prints or printed area. $id is null for a new one.
|
||||||
|
*/
|
||||||
|
final readonly class MachineCounter
|
||||||
|
{
|
||||||
|
public const int MAX_LENGTH = 100;
|
||||||
|
|
||||||
|
public string $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws InvalidCounter
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
public ?int $id,
|
||||||
|
string $name,
|
||||||
|
public CounterUnit $unit,
|
||||||
|
) {
|
||||||
|
$name = trim(preg_replace('/\s+/u', ' ', $name) ?? $name);
|
||||||
|
|
||||||
|
if ($name === '' || mb_strlen($name) > self::MAX_LENGTH) {
|
||||||
|
throw new InvalidCounter;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Domain\ValueObjects;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Exceptions\InvalidMachineName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A machine's name, e.g. "Suurkuvatulostin 1".
|
||||||
|
*/
|
||||||
|
final readonly class MachineName
|
||||||
|
{
|
||||||
|
public const int MAX_LENGTH = 150;
|
||||||
|
|
||||||
|
public string $value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws InvalidMachineName
|
||||||
|
*/
|
||||||
|
public function __construct(string $value)
|
||||||
|
{
|
||||||
|
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
|
||||||
|
|
||||||
|
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
|
||||||
|
throw new InvalidMachineName;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Adapters;
|
||||||
|
|
||||||
|
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||||
|
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
|
||||||
|
use App\Modules\Jobs\Application\Contracts\TaskQueries;
|
||||||
|
use App\Modules\Jobs\Application\DTOs\CustomerJobDetails;
|
||||||
|
use App\Modules\Jobs\Application\UseCases\ChangeTaskStatus;
|
||||||
|
use App\Modules\Jobs\Domain\Enums\Priority;
|
||||||
|
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineTasks;
|
||||||
|
use App\Modules\Machines\Application\DTOs\MachineTaskInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads tasks, their jobs and the service units of their work types from
|
||||||
|
* the Jobs and Catalog modules, and changes task statuses through Jobs.
|
||||||
|
*/
|
||||||
|
class JobsForMachines implements MachineTasks
|
||||||
|
{
|
||||||
|
/** @var array<int, CustomerJobDetails|null> */
|
||||||
|
private array $jobCache = [];
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private TaskQueries $tasks,
|
||||||
|
private CustomerJobQueries $jobs,
|
||||||
|
private CatalogQueries $catalog,
|
||||||
|
private ChangeTaskStatus $changeTaskStatus,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function info(int $taskId): ?MachineTaskInfo
|
||||||
|
{
|
||||||
|
return $this->infos([$taskId])[$taskId] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function infos(array $taskIds): array
|
||||||
|
{
|
||||||
|
if ($taskIds === []) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$stockUnitIds = $this->catalog->stockServiceUnitIds();
|
||||||
|
$infos = [];
|
||||||
|
|
||||||
|
foreach ($this->tasks->bookableTasks($taskIds) as $bookable) {
|
||||||
|
$task = $this->tasks->findTask($bookable->jobId, $bookable->taskId);
|
||||||
|
$job = $this->job($bookable->jobId);
|
||||||
|
|
||||||
|
if ($task === null || $job === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$infos[$task->id] = new MachineTaskInfo(
|
||||||
|
taskId: $task->id,
|
||||||
|
jobId: $job->id,
|
||||||
|
jobNumber: $job->number,
|
||||||
|
jobName: $job->name,
|
||||||
|
customerName: $job->customer->name,
|
||||||
|
title: $task->title,
|
||||||
|
description: $task->description,
|
||||||
|
instructions: $job->instructions,
|
||||||
|
status: $task->status->value,
|
||||||
|
priority: $job->priority->value,
|
||||||
|
priorityRank: self::rank($job->priority),
|
||||||
|
dueOn: $task->dueOn ?? $job->dueOn,
|
||||||
|
plannedFrom: $task->plannedFrom,
|
||||||
|
plannedTo: $task->plannedTo,
|
||||||
|
estimatedHours: $task->estimatedHours,
|
||||||
|
allowedServiceUnitIds: $this->allowedUnits($bookable->workTypeId, $job, $stockUnitIds),
|
||||||
|
isDone: $task->status === TaskStatus::Done,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $infos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function changeStatus(int $taskId, string $status): void
|
||||||
|
{
|
||||||
|
$task = $this->tasks->bookableTask($taskId);
|
||||||
|
|
||||||
|
if ($task !== null) {
|
||||||
|
$this->changeTaskStatus->execute($task->jobId, $taskId, TaskStatus::from($status));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<int> $stockUnitIds
|
||||||
|
* @return list<int>
|
||||||
|
*/
|
||||||
|
private function allowedUnits(int $workTypeId, CustomerJobDetails $job, array $stockUnitIds): array
|
||||||
|
{
|
||||||
|
$workType = $this->catalog->findWorkType($workTypeId);
|
||||||
|
$unit = $workType === null ? null : $this->catalog->findServiceUnit($workType->serviceUnitId);
|
||||||
|
|
||||||
|
if ($unit === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$unitIds = $unit->isShared ? array_map(fn ($jobUnit): int => $jobUnit->id, $job->serviceUnits) : [$unit->id];
|
||||||
|
|
||||||
|
return array_values(array_intersect($unitIds, $stockUnitIds));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function job(int $jobId): ?CustomerJobDetails
|
||||||
|
{
|
||||||
|
if (! array_key_exists($jobId, $this->jobCache)) {
|
||||||
|
$this->jobCache[$jobId] = $this->jobs->findDetails($jobId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->jobCache[$jobId];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function rank(Priority $priority): int
|
||||||
|
{
|
||||||
|
return match ($priority) {
|
||||||
|
Priority::Immediate => 4,
|
||||||
|
Priority::Urgent => 3,
|
||||||
|
Priority::First => 2,
|
||||||
|
Priority::Normal => 1,
|
||||||
|
Priority::Waiting => 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Persistence\Eloquent;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Enums\CounterUnit;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persistence model, used only inside the Machines Infrastructure layer.
|
||||||
|
*
|
||||||
|
* @property int $id
|
||||||
|
* @property int $machine_id
|
||||||
|
* @property string $name
|
||||||
|
* @property CounterUnit $unit
|
||||||
|
* @property int $sort_order
|
||||||
|
*/
|
||||||
|
#[Table('machine_counters')]
|
||||||
|
#[Fillable(['machine_id', 'name', 'unit', 'sort_order'])]
|
||||||
|
class MachineCounterModel extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return HasOne<MachineCounterReadingModel, $this>
|
||||||
|
*/
|
||||||
|
public function latestReading(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(MachineCounterReadingModel::class, 'machine_counter_id')->latestOfMany('read_at');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'machine_id' => 'integer',
|
||||||
|
'unit' => CounterUnit::class,
|
||||||
|
'sort_order' => 'integer',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Persistence\Eloquent;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persistence model, used only inside the Machines Infrastructure layer.
|
||||||
|
*
|
||||||
|
* @property int $id
|
||||||
|
* @property int $machine_counter_id
|
||||||
|
* @property int $reading_milli
|
||||||
|
* @property int $user_id
|
||||||
|
* @property Carbon $read_at
|
||||||
|
*/
|
||||||
|
#[Table('machine_counter_readings')]
|
||||||
|
#[Fillable(['machine_counter_id', 'reading_milli', 'user_id', 'read_at'])]
|
||||||
|
class MachineCounterReadingModel extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'machine_counter_id' => 'integer',
|
||||||
|
'reading_milli' => 'integer',
|
||||||
|
'user_id' => 'integer',
|
||||||
|
'read_at' => 'immutable_datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Persistence\Eloquent;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Enums\MachineType;
|
||||||
|
use Database\Factories\MachineFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persistence model, used only inside the Machines Infrastructure layer.
|
||||||
|
*
|
||||||
|
* @property int $id
|
||||||
|
* @property string $name
|
||||||
|
* @property MachineType $type
|
||||||
|
* @property string|null $description
|
||||||
|
* @property int|null $responsible_user_id
|
||||||
|
* @property bool $is_active
|
||||||
|
*/
|
||||||
|
#[Table('machines')]
|
||||||
|
#[Fillable(['name', 'type', 'description', 'responsible_user_id', 'is_active'])]
|
||||||
|
#[UseFactory(MachineFactory::class)]
|
||||||
|
class MachineModel extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<MachineFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return HasMany<MachineCounterModel, $this>
|
||||||
|
*/
|
||||||
|
public function counters(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(MachineCounterModel::class, 'machine_id')->orderBy('sort_order');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'type' => MachineType::class,
|
||||||
|
'responsible_user_id' => 'integer',
|
||||||
|
'is_active' => 'boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Persistence\Eloquent;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persistence model, used only inside the Machines Infrastructure layer.
|
||||||
|
*
|
||||||
|
* @property int $id
|
||||||
|
* @property int $machine_id
|
||||||
|
* @property string $planned_on Y-m-d, kept as text so that it compares as a date in every database.
|
||||||
|
* @property int $position
|
||||||
|
* @property int|null $task_id
|
||||||
|
* @property int|null $quick_sale_order_line_id
|
||||||
|
* @property int|null $production_step_position
|
||||||
|
* @property Carbon|null $completed_at
|
||||||
|
* @property int|null $completed_by
|
||||||
|
*/
|
||||||
|
#[Table('machine_queue_entries')]
|
||||||
|
#[Fillable(['machine_id', 'planned_on', 'position', 'task_id', 'quick_sale_order_line_id', 'production_step_position', 'completed_at', 'completed_by'])]
|
||||||
|
class MachineQueueEntryModel extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'machine_id' => 'integer',
|
||||||
|
'position' => 'integer',
|
||||||
|
'task_id' => 'integer',
|
||||||
|
'quick_sale_order_line_id' => 'integer',
|
||||||
|
'production_step_position' => 'integer',
|
||||||
|
'completed_at' => 'immutable_datetime',
|
||||||
|
'completed_by' => 'integer',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
+176
@@ -0,0 +1,176 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
|
||||||
|
|
||||||
|
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineTasks;
|
||||||
|
use App\Modules\Machines\Application\DTOs\CounterSummary;
|
||||||
|
use App\Modules\Machines\Application\DTOs\DayLoad;
|
||||||
|
use App\Modules\Machines\Application\DTOs\MachineSummary;
|
||||||
|
use App\Modules\Machines\Application\DTOs\QueueRow;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
|
||||||
|
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class EloquentMachineQueries implements MachineQueries
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private CatalogQueries $catalog,
|
||||||
|
private UserAccountQueries $users,
|
||||||
|
private MachineTasks $tasks,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function machines(bool $includeInactive = false): array
|
||||||
|
{
|
||||||
|
return $this->summaries(MachineModel::query()
|
||||||
|
->with('counters.latestReading')
|
||||||
|
->when(! $includeInactive, fn ($query) => $query->where('is_active', true))
|
||||||
|
->orderBy('name')
|
||||||
|
->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findMachine(int $id): ?MachineSummary
|
||||||
|
{
|
||||||
|
$model = MachineModel::query()->with('counters.latestReading')->find($id);
|
||||||
|
|
||||||
|
return $model === null ? null : $this->summaries(collect([$model]))[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function machinesFor(array $serviceUnitIds): array
|
||||||
|
{
|
||||||
|
if ($serviceUnitIds === []) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->summaries(MachineModel::query()
|
||||||
|
->with('counters.latestReading')
|
||||||
|
->where('is_active', true)
|
||||||
|
->whereIn('id', DB::table('machine_service_units')->select('machine_id')->whereIn('service_unit_id', $serviceUnitIds))
|
||||||
|
->orderBy('name')
|
||||||
|
->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rowForTask(int $taskId): ?QueueRow
|
||||||
|
{
|
||||||
|
$model = MachineQueueEntryModel::query()->where('task_id', $taskId)->whereNull('completed_at')->first();
|
||||||
|
|
||||||
|
return $model === null ? null : $this->toRow($model, null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rowsOn(int $machineId, string $day, string $today): array
|
||||||
|
{
|
||||||
|
$models = MachineQueueEntryModel::query()
|
||||||
|
->where('machine_id', $machineId)
|
||||||
|
->whereNull('completed_at')
|
||||||
|
->when(
|
||||||
|
$day === $today,
|
||||||
|
fn ($query) => $query->where('planned_on', '<=', $day),
|
||||||
|
fn ($query) => $query->where('planned_on', $day),
|
||||||
|
)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$infos = $this->tasks->infos($models->pluck('task_id')->filter()->unique()->values()->all());
|
||||||
|
|
||||||
|
return $models
|
||||||
|
->sortBy(fn (MachineQueueEntryModel $model): string => sprintf(
|
||||||
|
'%d-%d-%s-%05d-%010d',
|
||||||
|
$model->quick_sale_order_line_id === null ? 1 : 0,
|
||||||
|
substr($model->planned_on, 0, 10) < $day ? 0 : 1,
|
||||||
|
substr($model->planned_on, 0, 10),
|
||||||
|
$model->position,
|
||||||
|
$model->id,
|
||||||
|
))
|
||||||
|
->map(fn (MachineQueueEntryModel $model): QueueRow => $this->toRow(
|
||||||
|
$model,
|
||||||
|
$model->task_id === null ? null : ($infos[$model->task_id] ?? null),
|
||||||
|
substr($model->planned_on, 0, 10) < $day,
|
||||||
|
))
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function load(int $machineId, string $from, int $days): array
|
||||||
|
{
|
||||||
|
$to = CarbonImmutable::parse($from)->addDays($days - 1)->format('Y-m-d');
|
||||||
|
$models = MachineQueueEntryModel::query()
|
||||||
|
->where('machine_id', $machineId)
|
||||||
|
->whereNull('completed_at')
|
||||||
|
->whereBetween('planned_on', [$from, $to])
|
||||||
|
->get();
|
||||||
|
$infos = $this->tasks->infos($models->pluck('task_id')->filter()->unique()->values()->all());
|
||||||
|
$loads = [];
|
||||||
|
|
||||||
|
for ($i = 0; $i < $days; $i++) {
|
||||||
|
$day = CarbonImmutable::parse($from)->addDays($i)->format('Y-m-d');
|
||||||
|
$rows = $models->filter(fn (MachineQueueEntryModel $model): bool => substr($model->planned_on, 0, 10) === $day);
|
||||||
|
$quarters = $rows->sum(function (MachineQueueEntryModel $model) use ($infos): int {
|
||||||
|
$hours = $model->task_id === null ? null : ($infos[$model->task_id]->estimatedHours ?? null);
|
||||||
|
|
||||||
|
return $hours === null ? 0 : (int) round((float) $hours * 4);
|
||||||
|
});
|
||||||
|
|
||||||
|
$loads[] = new DayLoad($day, $rows->count(), $quarters);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $loads;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function toRow(MachineQueueEntryModel $model, mixed $task, bool $isLate): QueueRow
|
||||||
|
{
|
||||||
|
return new QueueRow(
|
||||||
|
entryId: $model->id,
|
||||||
|
machineId: $model->machine_id,
|
||||||
|
plannedOn: substr($model->planned_on, 0, 10),
|
||||||
|
position: $model->position,
|
||||||
|
isLate: $isLate,
|
||||||
|
taskId: $model->task_id,
|
||||||
|
orderLineId: $model->quick_sale_order_line_id,
|
||||||
|
stepPosition: $model->production_step_position,
|
||||||
|
task: $task,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection<int, MachineModel> $models
|
||||||
|
* @return list<MachineSummary>
|
||||||
|
*/
|
||||||
|
private function summaries(Collection $models): array
|
||||||
|
{
|
||||||
|
$unitIdsByMachine = DB::table('machine_service_units')
|
||||||
|
->whereIn('machine_id', $models->pluck('id'))
|
||||||
|
->get()
|
||||||
|
->groupBy('machine_id')
|
||||||
|
->map(fn ($rows) => $rows->pluck('service_unit_id')->map(fn ($id): int => (int) $id)->values()->all());
|
||||||
|
$unitNames = $this->catalog->serviceUnitNames($unitIdsByMachine->flatten()->unique()->values()->all());
|
||||||
|
$userNames = $this->users->namesByIds($models->pluck('responsible_user_id')->filter()->unique()->values()->all());
|
||||||
|
|
||||||
|
return $models->map(function (MachineModel $model) use ($unitIdsByMachine, $unitNames, $userNames): MachineSummary {
|
||||||
|
$unitIds = $unitIdsByMachine->get($model->id, []);
|
||||||
|
|
||||||
|
return new MachineSummary(
|
||||||
|
id: $model->id,
|
||||||
|
name: $model->name,
|
||||||
|
type: $model->type,
|
||||||
|
description: $model->description,
|
||||||
|
serviceUnitIds: $unitIds,
|
||||||
|
serviceUnitNames: array_values(array_intersect_key($unitNames, array_flip($unitIds))),
|
||||||
|
responsibleUserId: $model->responsible_user_id,
|
||||||
|
responsibleName: $model->responsible_user_id === null ? null : ($userNames[$model->responsible_user_id] ?? ''),
|
||||||
|
counters: $model->counters->map(fn (MachineCounterModel $counter): CounterSummary => new CounterSummary(
|
||||||
|
$counter->id,
|
||||||
|
$counter->name,
|
||||||
|
$counter->unit,
|
||||||
|
$counter->latestReading?->reading_milli,
|
||||||
|
$counter->latestReading === null ? null : Carbon::instance($counter->latestReading->read_at)->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i'),
|
||||||
|
))->values()->all(),
|
||||||
|
isActive: $model->is_active,
|
||||||
|
);
|
||||||
|
})->values()->all();
|
||||||
|
}
|
||||||
|
}
|
||||||
+77
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Contracts\MachineRepository;
|
||||||
|
use App\Modules\Machines\Domain\Entities\Machine;
|
||||||
|
use App\Modules\Machines\Domain\ValueObjects\MachineCounter;
|
||||||
|
use App\Modules\Machines\Domain\ValueObjects\MachineName;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class EloquentMachineRepository implements MachineRepository
|
||||||
|
{
|
||||||
|
public function findById(int $id): ?Machine
|
||||||
|
{
|
||||||
|
$model = MachineModel::query()->with('counters')->find($id);
|
||||||
|
|
||||||
|
return $model === null ? null : $this->toEntity($model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(Machine $machine): Machine
|
||||||
|
{
|
||||||
|
$model = $machine->id() === null ? new MachineModel : MachineModel::query()->findOrFail($machine->id());
|
||||||
|
|
||||||
|
$model->fill([
|
||||||
|
'name' => $machine->name()->value,
|
||||||
|
'type' => $machine->type(),
|
||||||
|
'description' => $machine->description(),
|
||||||
|
'responsible_user_id' => $machine->responsibleUserId(),
|
||||||
|
'is_active' => $machine->isActive(),
|
||||||
|
])->save();
|
||||||
|
|
||||||
|
DB::table('machine_service_units')->where('machine_id', $model->id)->delete();
|
||||||
|
DB::table('machine_service_units')->insert(array_map(
|
||||||
|
fn (int $unitId): array => ['machine_id' => $model->id, 'service_unit_id' => $unitId],
|
||||||
|
$machine->serviceUnitIds(),
|
||||||
|
));
|
||||||
|
|
||||||
|
$keptIds = [];
|
||||||
|
|
||||||
|
foreach ($machine->counters() as $index => $counter) {
|
||||||
|
$counterModel = $counter->id === null
|
||||||
|
? new MachineCounterModel(['machine_id' => $model->id])
|
||||||
|
: MachineCounterModel::query()->where('machine_id', $model->id)->findOrFail($counter->id);
|
||||||
|
|
||||||
|
$counterModel->fill(['name' => $counter->name, 'unit' => $counter->unit, 'sort_order' => $index + 1])->save();
|
||||||
|
$keptIds[] = $counterModel->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
MachineCounterModel::query()->where('machine_id', $model->id)->whereNotIn('id', $keptIds)->delete();
|
||||||
|
|
||||||
|
return $this->toEntity($model->load('counters'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isNameTaken(string $name, ?int $exceptId = null): bool
|
||||||
|
{
|
||||||
|
return MachineModel::query()
|
||||||
|
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
|
||||||
|
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
|
||||||
|
->exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function toEntity(MachineModel $model): Machine
|
||||||
|
{
|
||||||
|
return Machine::reconstitute(
|
||||||
|
$model->id,
|
||||||
|
new MachineName($model->name),
|
||||||
|
$model->type,
|
||||||
|
$model->description,
|
||||||
|
DB::table('machine_service_units')->where('machine_id', $model->id)->orderBy('service_unit_id')->pluck('service_unit_id')->map(fn ($id): int => (int) $id)->values()->all(),
|
||||||
|
$model->responsible_user_id,
|
||||||
|
$model->counters->map(fn (MachineCounterModel $counter): MachineCounter => new MachineCounter($counter->id, $counter->name, $counter->unit))->values()->all(),
|
||||||
|
$model->is_active,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+86
@@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Persistence\Repositories;
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Contracts\QueueRepository;
|
||||||
|
use App\Modules\Machines\Domain\Entities\QueueEntry;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
|
||||||
|
class EloquentQueueRepository implements QueueRepository
|
||||||
|
{
|
||||||
|
public function findById(int $id): ?QueueEntry
|
||||||
|
{
|
||||||
|
$model = MachineQueueEntryModel::query()->find($id);
|
||||||
|
|
||||||
|
return $model === null ? null : $this->toEntity($model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function openEntryForTask(int $taskId): ?QueueEntry
|
||||||
|
{
|
||||||
|
$model = MachineQueueEntryModel::query()->where('task_id', $taskId)->whereNull('completed_at')->first();
|
||||||
|
|
||||||
|
return $model === null ? null : $this->toEntity($model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function openEntryForOrderLine(int $orderLineId): ?QueueEntry
|
||||||
|
{
|
||||||
|
$model = MachineQueueEntryModel::query()->where('quick_sale_order_line_id', $orderLineId)->whereNull('completed_at')->first();
|
||||||
|
|
||||||
|
return $model === null ? null : $this->toEntity($model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function openEntriesOn(int $machineId, DateTimeImmutable $day): array
|
||||||
|
{
|
||||||
|
return MachineQueueEntryModel::query()
|
||||||
|
->where('machine_id', $machineId)
|
||||||
|
->where('planned_on', $day->format('Y-m-d'))
|
||||||
|
->whereNull('completed_at')
|
||||||
|
->orderBy('position')
|
||||||
|
->orderBy('id')
|
||||||
|
->get()
|
||||||
|
->map(fn (MachineQueueEntryModel $model): QueueEntry => $this->toEntity($model))
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(QueueEntry $entry): QueueEntry
|
||||||
|
{
|
||||||
|
$model = $entry->id() === null ? new MachineQueueEntryModel : MachineQueueEntryModel::query()->findOrFail($entry->id());
|
||||||
|
|
||||||
|
$model->fill([
|
||||||
|
'machine_id' => $entry->machineId(),
|
||||||
|
'planned_on' => $entry->plannedOn()->format('Y-m-d'),
|
||||||
|
'position' => $entry->position(),
|
||||||
|
'task_id' => $entry->taskId(),
|
||||||
|
'quick_sale_order_line_id' => $entry->orderLineId(),
|
||||||
|
'production_step_position' => $entry->stepPosition(),
|
||||||
|
'completed_at' => $entry->completedAt()?->format('Y-m-d H:i:s'),
|
||||||
|
'completed_by' => $entry->completedBy(),
|
||||||
|
])->save();
|
||||||
|
|
||||||
|
return $this->toEntity($model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(QueueEntry $entry): void
|
||||||
|
{
|
||||||
|
if ($entry->id() !== null) {
|
||||||
|
MachineQueueEntryModel::query()->whereKey($entry->id())->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function toEntity(MachineQueueEntryModel $model): QueueEntry
|
||||||
|
{
|
||||||
|
return QueueEntry::reconstitute(
|
||||||
|
$model->id,
|
||||||
|
$model->machine_id,
|
||||||
|
new DateTimeImmutable(substr($model->planned_on, 0, 10)),
|
||||||
|
$model->position,
|
||||||
|
$model->task_id,
|
||||||
|
$model->quick_sale_order_line_id,
|
||||||
|
$model->production_step_position,
|
||||||
|
$model->completed_at === null ? null : DateTimeImmutable::createFromInterface($model->completed_at),
|
||||||
|
$model->completed_by,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Infrastructure\Providers;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||||
|
use App\Modules\Jobs\Domain\Events\TaskCompleted;
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineTasks;
|
||||||
|
use App\Modules\Machines\Application\UseCases\MachineWorkList;
|
||||||
|
use App\Modules\Machines\Domain\Contracts\MachineRepository;
|
||||||
|
use App\Modules\Machines\Domain\Contracts\QueueRepository;
|
||||||
|
use App\Modules\Machines\Infrastructure\Adapters\JobsForMachines;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentMachineQueries;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentMachineRepository;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Repositories\EloquentQueueRepository;
|
||||||
|
use App\Modules\Users\Domain\Enums\Role;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Illuminate\Support\Facades\Gate;
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wires the Machines module (machines, their work lists, maintenance and
|
||||||
|
* the machine console, spec 19.4–19.6) into Laravel: bindings, routes, the
|
||||||
|
* `machines::` Livewire namespace and the `machines.*` abilities.
|
||||||
|
*/
|
||||||
|
class MachinesServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array<class-string, class-string>
|
||||||
|
*/
|
||||||
|
public array $bindings = [
|
||||||
|
MachineRepository::class => EloquentMachineRepository::class,
|
||||||
|
QueueRepository::class => EloquentQueueRepository::class,
|
||||||
|
MachineQueries::class => EloquentMachineQueries::class,
|
||||||
|
MachineTasks::class => JobsForMachines::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
public function boot(): void
|
||||||
|
{
|
||||||
|
$presentationPath = dirname(__DIR__, 2).'/Presentation';
|
||||||
|
|
||||||
|
Livewire::addNamespace('machines', viewPath: $presentationPath.'/Livewire');
|
||||||
|
|
||||||
|
if (! $this->app->routesAreCached()) {
|
||||||
|
$this->loadRoutesFrom($presentationPath.'/Routes/web.php');
|
||||||
|
}
|
||||||
|
|
||||||
|
// A task marked done elsewhere leaves its machine's work list.
|
||||||
|
Event::listen(TaskCompleted::class, fn (TaskCompleted $event) => app(MachineWorkList::class)->closeTask($event->taskId));
|
||||||
|
|
||||||
|
$staff = fn (User $user): bool => $user->role !== Role::Subcontractor;
|
||||||
|
|
||||||
|
// Moving tasks to machines, the console and maintenance records: everyone but subcontractors (spec 19.9).
|
||||||
|
Gate::define('machines.use', $staff);
|
||||||
|
Gate::define('machines.manage', fn (User $user): bool => in_array($user->role, [Role::Admin, Role::Management], true));
|
||||||
|
Gate::define('machines.menu', fn (User $user): bool => $staff($user) && app(CatalogQueries::class)->stockServiceUnitIds() !== []);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Modules\Machines\Presentation\Livewire\Concerns;
|
||||||
|
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formatting shared by the machine pages and the machine console.
|
||||||
|
*/
|
||||||
|
trait FormatsMachines
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* "24.11.2026" for "2026-11-24"; "24.11.2026 klo 14.05" for "2026-11-24 14:05".
|
||||||
|
*/
|
||||||
|
public function dateLabel(string $value): string
|
||||||
|
{
|
||||||
|
return strlen($value) > 10
|
||||||
|
? CarbonImmutable::createFromFormat('Y-m-d H:i', $value)->format('j.n.Y \k\l\o H.i')
|
||||||
|
: CarbonImmutable::createFromFormat('!Y-m-d', $value)->format('j.n.Y');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* "ti 24.11." for "2026-11-24".
|
||||||
|
*/
|
||||||
|
public function shortDay(string $value): string
|
||||||
|
{
|
||||||
|
$date = CarbonImmutable::createFromFormat('!Y-m-d', $value)->locale(app()->getLocale());
|
||||||
|
|
||||||
|
return $date->isoFormat('dd').' '.$date->format('j.n.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function quarters(int $quarters): string
|
||||||
|
{
|
||||||
|
return rtrim(rtrim(number_format($quarters / 4, 2, ',', ''), '0'), ',');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A counter reading in thousandths, e.g. "20 000" or "12,5".
|
||||||
|
*/
|
||||||
|
public function reading(int $milli): string
|
||||||
|
{
|
||||||
|
$whole = intdiv(abs($milli), 1000);
|
||||||
|
$fraction = rtrim(str_pad((string) (abs($milli) % 1000), 3, '0', STR_PAD_LEFT), '0');
|
||||||
|
|
||||||
|
return ($milli < 0 ? '-' : '').number_format($whole, 0, ',', "\u{00A0}").($fraction === '' ? '' : ','.$fraction);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,323 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||||||
|
use App\Modules\Machines\Application\DTOs\MachineData;
|
||||||
|
use App\Modules\Machines\Application\UseCases\SaveMachine;
|
||||||
|
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\Users\Application\Contracts\UserAccountQueries;
|
||||||
|
use App\Modules\Users\Application\DTOs\UserOption;
|
||||||
|
use Livewire\Attributes\Computed;
|
||||||
|
use Livewire\Attributes\Locked;
|
||||||
|
use Livewire\Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add page (no {machine}) and edit page (with {machine}) of a machine
|
||||||
|
* (management, spec 19.4–19.5): name, type, description, service units
|
||||||
|
* among those that use stock, the responsible person and the counters.
|
||||||
|
*/
|
||||||
|
new class extends Component
|
||||||
|
{
|
||||||
|
#[Locked]
|
||||||
|
public ?int $machineId = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Units the stored machine has, offered even after the setting is off.
|
||||||
|
*
|
||||||
|
* @var list<int>
|
||||||
|
*/
|
||||||
|
#[Locked]
|
||||||
|
public array $storedUnitIds = [];
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public bool $isActive = true;
|
||||||
|
|
||||||
|
public string $name = '';
|
||||||
|
|
||||||
|
public string $type = 'printer';
|
||||||
|
|
||||||
|
public string $description = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
public array $serviceUnitIds = [];
|
||||||
|
|
||||||
|
public string $responsibleUserId = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var list<array{key: int, id: ?int, name: string, unit: string}>
|
||||||
|
*/
|
||||||
|
public array $counters = [];
|
||||||
|
|
||||||
|
public int $nextKey = 1;
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public string $message = '';
|
||||||
|
|
||||||
|
public function mount(MachineQueries $machines, ?int $machine = null): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.manage');
|
||||||
|
|
||||||
|
if ($machine === null) {
|
||||||
|
$stockUnits = app(CatalogQueries::class)->stockServiceUnitIds();
|
||||||
|
$this->serviceUnitIds = count($stockUnits) === 1 ? [(string) $stockUnits[0]] : [];
|
||||||
|
$this->suggestCounters(announce: false);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$summary = $machines->findMachine($machine) ?? abort(404);
|
||||||
|
|
||||||
|
$this->machineId = $summary->id;
|
||||||
|
$this->isActive = $summary->isActive;
|
||||||
|
$this->name = $summary->name;
|
||||||
|
$this->type = $summary->type->value;
|
||||||
|
$this->description = (string) $summary->description;
|
||||||
|
$this->serviceUnitIds = array_map('strval', $summary->serviceUnitIds);
|
||||||
|
$this->storedUnitIds = $summary->serviceUnitIds;
|
||||||
|
$this->responsibleUserId = (string) ($summary->responsibleUserId ?? '');
|
||||||
|
|
||||||
|
foreach ($summary->counters as $counter) {
|
||||||
|
$this->counters[] = ['key' => $this->nextKey++, 'id' => $counter->id, 'name' => $counter->name, 'unit' => $counter->unit->value];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<array{value: int, label: string}>
|
||||||
|
*/
|
||||||
|
#[Computed]
|
||||||
|
public function unitOptions(): array
|
||||||
|
{
|
||||||
|
$ids = array_values(array_unique(array_merge(app(CatalogQueries::class)->stockServiceUnitIds(), $this->storedUnitIds)));
|
||||||
|
$options = [];
|
||||||
|
|
||||||
|
foreach (app(CatalogQueries::class)->serviceUnitNames($ids) as $id => $name) {
|
||||||
|
$options[] = ['value' => $id, 'label' => $name];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<UserOption>
|
||||||
|
*/
|
||||||
|
#[Computed]
|
||||||
|
public function users(): array
|
||||||
|
{
|
||||||
|
return app(UserAccountQueries::class)->activeUserOptions();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addCounter(): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.manage');
|
||||||
|
|
||||||
|
$key = $this->nextKey++;
|
||||||
|
$this->counters[] = ['key' => $key, 'id' => null, 'name' => '', 'unit' => CounterUnit::Piece->value];
|
||||||
|
$this->message = __('machines.form.counter_added', ['number' => count($this->counters)]);
|
||||||
|
$this->js("document.getElementById('counter-{$key}-name')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeCounter(int $index): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.manage');
|
||||||
|
|
||||||
|
if (! isset($this->counters[$index])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
array_splice($this->counters, $index, 1);
|
||||||
|
$this->resetErrorBag();
|
||||||
|
$this->message = __('machines.form.counter_removed', ['number' => $index + 1]);
|
||||||
|
$this->js("document.getElementById('add-counter')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the usual counters of the machine type that are not there yet.
|
||||||
|
*/
|
||||||
|
public function suggestCounters(bool $announce = true): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.manage');
|
||||||
|
|
||||||
|
$added = 0;
|
||||||
|
$names = array_map(fn (array $counter): string => mb_strtolower($counter['name']), $this->counters);
|
||||||
|
|
||||||
|
foreach ((MachineType::tryFrom($this->type) ?? MachineType::Other)->suggestedCounters() as $suggestion) {
|
||||||
|
$name = __('machines.counter_names.'.$suggestion['name']);
|
||||||
|
|
||||||
|
if (! in_array(mb_strtolower($name), $names, true)) {
|
||||||
|
$this->counters[] = ['key' => $this->nextKey++, 'id' => null, 'name' => $name, 'unit' => $suggestion['unit']->value];
|
||||||
|
$added++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($announce) {
|
||||||
|
$this->message = trans_choice('machines.form.counters_suggested', $added, ['count' => $added]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(SaveMachine $saveMachine): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.manage');
|
||||||
|
$this->resetErrorBag();
|
||||||
|
|
||||||
|
$this->validate([
|
||||||
|
'type' => ['required', 'in:'.implode(',', array_column(MachineType::cases(), 'value'))],
|
||||||
|
'description' => ['nullable', 'string', 'max:5000'],
|
||||||
|
'serviceUnitIds' => ['array'],
|
||||||
|
'serviceUnitIds.*' => ['integer'],
|
||||||
|
'responsibleUserId' => ['nullable', 'integer'],
|
||||||
|
'counters.*.unit' => ['required', 'in:'.implode(',', array_column(CounterUnit::cases(), 'value'))],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$data = new MachineData(
|
||||||
|
$this->name,
|
||||||
|
$this->type,
|
||||||
|
$this->description,
|
||||||
|
array_map('intval', $this->serviceUnitIds),
|
||||||
|
$this->responsibleUserId === '' ? null : (int) $this->responsibleUserId,
|
||||||
|
array_map(fn (array $counter): array => ['id' => $counter['id'], 'name' => $counter['name'], 'unit' => $counter['unit']], $this->counters),
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if ($this->machineId === null) {
|
||||||
|
$this->machineId = $saveMachine->add($data);
|
||||||
|
$status = __('machines.form.created', ['name' => trim($this->name)]);
|
||||||
|
} else {
|
||||||
|
$saveMachine->update($this->machineId, $data);
|
||||||
|
$status = __('machines.form.updated', ['name' => trim($this->name)]);
|
||||||
|
}
|
||||||
|
} catch (InvalidMachineName) {
|
||||||
|
$this->addError('name', __('machines.errors.name'));
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (MachineNameTaken) {
|
||||||
|
$this->addError('name', __('machines.errors.name_taken'));
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (MachineNeedsServiceUnit) {
|
||||||
|
$this->addError('serviceUnitIds', __('machines.errors.service_units'));
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (InvalidCounter) {
|
||||||
|
$this->addError('counters', __('machines.errors.counter'));
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (MachineNotFound) {
|
||||||
|
abort(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
session()->flash('status', $status);
|
||||||
|
$this->redirectRoute('machines.show', $this->machineId, navigate: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toggleActive(SaveMachine $saveMachine): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.manage');
|
||||||
|
|
||||||
|
if ($this->machineId === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$saveMachine->setActive($this->machineId, ! $this->isActive);
|
||||||
|
$this->isActive = ! $this->isActive;
|
||||||
|
$this->message = __($this->isActive ? 'machines.form.activated' : 'machines.form.deactivated');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function heading(): string
|
||||||
|
{
|
||||||
|
return __($this->machineId === null ? 'machines.form.create_title' : 'machines.form.edit_title');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(): mixed
|
||||||
|
{
|
||||||
|
return $this->view()->title($this->heading());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
|
||||||
|
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
|
||||||
|
|
||||||
|
<p role="status" aria-live="polite" class="text-sm font-medium">{{ $message }}</p>
|
||||||
|
|
||||||
|
<form wire:submit="save" class="space-y-6" novalidate>
|
||||||
|
<x-input id="machine-name" wire:model="name" :label="__('machines.form.name')" autocomplete="off" maxlength="150" required />
|
||||||
|
|
||||||
|
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||||
|
<x-select.native id="machine-type" wire:model="type" :label="__('machines.form.type')" :aria-label="__('machines.form.type')">
|
||||||
|
@foreach (MachineType::cases() as $option)
|
||||||
|
<option value="{{ $option->value }}">{{ __('machines.types.'.$option->value) }}</option>
|
||||||
|
@endforeach
|
||||||
|
</x-select.native>
|
||||||
|
|
||||||
|
<x-textarea id="machine-description" wire:model="description" :label="__('machines.form.description')" resize-auto />
|
||||||
|
|
||||||
|
@if ($this->unitOptions === [])
|
||||||
|
<p class="text-sm font-medium">{{ __('machines.form.no_stock_units') }}</p>
|
||||||
|
@else
|
||||||
|
<x-checkbox-group :legend="__('machines.form.service_units')" model="serviceUnitIds" :options="$this->unitOptions" :hint="__('machines.form.service_units_hint')" />
|
||||||
|
@endif
|
||||||
|
|
||||||
|
<x-select.native id="machine-responsible" wire:model="responsibleUserId" :label="__('machines.form.responsible')" :aria-label="__('machines.form.responsible')" :hint="__('machines.form.responsible_hint')">
|
||||||
|
<option value="">{{ __('machines.form.no_responsible') }}</option>
|
||||||
|
@foreach ($this->users as $user)
|
||||||
|
<option value="{{ $user->id }}">{{ $user->name }}</option>
|
||||||
|
@endforeach
|
||||||
|
</x-select.native>
|
||||||
|
|
||||||
|
<fieldset class="space-y-3">
|
||||||
|
<legend class="text-lg font-semibold">{{ __('machines.form.counters') }}</legend>
|
||||||
|
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('machines.form.counters_hint') }}</p>
|
||||||
|
@error('counters')
|
||||||
|
<p role="alert" class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
|
||||||
|
@enderror
|
||||||
|
|
||||||
|
@foreach ($counters as $index => $counter)
|
||||||
|
<div wire:key="counter-{{ $counter['key'] }}" class="flex flex-wrap items-end gap-2">
|
||||||
|
<div class="min-w-48 flex-1">
|
||||||
|
<x-input
|
||||||
|
id="counter-{{ $counter['key'] }}-name"
|
||||||
|
wire:model="counters.{{ $index }}.name"
|
||||||
|
:label="__('machines.form.counter_name')"
|
||||||
|
autocomplete="off"
|
||||||
|
maxlength="100"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="w-32">
|
||||||
|
<x-select.native
|
||||||
|
id="counter-{{ $counter['key'] }}-unit"
|
||||||
|
wire:model="counters.{{ $index }}.unit"
|
||||||
|
:label="__('machines.form.counter_unit')"
|
||||||
|
:aria-label="__('machines.form.counter_unit').': '.__('machines.form.counter_named', ['number' => $index + 1])"
|
||||||
|
>
|
||||||
|
@foreach (CounterUnit::cases() as $unit)
|
||||||
|
<option value="{{ $unit->value }}">{{ __('machines.counter_units.'.$unit->value) }}</option>
|
||||||
|
@endforeach
|
||||||
|
</x-select.native>
|
||||||
|
</div>
|
||||||
|
<x-button wire:click="removeCounter({{ $index }})" subtle color="black" sm icon="trash" :aria-label="__('machines.form.remove_counter_named', ['number' => $index + 1])" />
|
||||||
|
</div>
|
||||||
|
@endforeach
|
||||||
|
|
||||||
|
<div class="flex flex-wrap gap-2">
|
||||||
|
<x-button id="add-counter" wire:click="addCounter" subtle color="black" sm icon="plus" :text="__('machines.form.add_counter')" />
|
||||||
|
<x-button wire:click="suggestCounters" subtle color="black" sm icon="sparkles" :text="__('machines.form.suggest_counters')" />
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
|
<div class="flex flex-wrap gap-3">
|
||||||
|
<x-button submit :text="__('machines.form.save')" />
|
||||||
|
<x-button :href="$machineId === null ? route('machines.index') : route('machines.show', $machineId)" wire:navigate subtle color="black" :text="__('machines.form.cancel')" />
|
||||||
|
@if ($machineId !== null)
|
||||||
|
<x-button wire:click="toggleActive" subtle color="black" :icon="$isActive ? 'archive-box-x-mark' : 'archive-box-arrow-down'" :text="$isActive ? __('machines.form.deactivate') : __('machines.form.activate')" />
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||||||
|
use App\Modules\Machines\Application\DTOs\MachineSummary;
|
||||||
|
use Illuminate\Support\Facades\Gate;
|
||||||
|
use Livewire\Attributes\Computed;
|
||||||
|
use Livewire\Attributes\Url;
|
||||||
|
use Livewire\Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Machines grouped by service unit (spec 19.4), filtered by unit. Each
|
||||||
|
* machine links to its page with the work list.
|
||||||
|
*/
|
||||||
|
new class extends Component
|
||||||
|
{
|
||||||
|
#[Url(as: 'yksikko', except: '')]
|
||||||
|
public string $unit = '';
|
||||||
|
|
||||||
|
public bool $includeInactive = false;
|
||||||
|
|
||||||
|
public function mount(): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.use');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string> Service units that use stock or have machines.
|
||||||
|
*/
|
||||||
|
#[Computed]
|
||||||
|
public function units(): array
|
||||||
|
{
|
||||||
|
$ids = array_unique(array_merge(
|
||||||
|
app(CatalogQueries::class)->stockServiceUnitIds(),
|
||||||
|
...array_map(fn (MachineSummary $machine): array => $machine->serviceUnitIds, $this->machines),
|
||||||
|
));
|
||||||
|
|
||||||
|
return app(CatalogQueries::class)->serviceUnitNames(array_values($ids));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<MachineSummary>
|
||||||
|
*/
|
||||||
|
#[Computed]
|
||||||
|
public function machines(): array
|
||||||
|
{
|
||||||
|
return app(MachineQueries::class)->machines($this->includeInactive && Gate::allows('machines.manage'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Machines by unit name; a shared machine is under each of its units.
|
||||||
|
*
|
||||||
|
* @return array<string, list<MachineSummary>>
|
||||||
|
*/
|
||||||
|
public function grouped(): array
|
||||||
|
{
|
||||||
|
$groups = [];
|
||||||
|
|
||||||
|
foreach ($this->units as $unitId => $unitName) {
|
||||||
|
if ($this->unit !== '' && (int) $this->unit !== $unitId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$machines = array_values(array_filter($this->machines, fn (MachineSummary $machine): bool => in_array($unitId, $machine->serviceUnitIds, true)));
|
||||||
|
|
||||||
|
if ($machines !== []) {
|
||||||
|
$groups[$unitName] = $machines;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $groups;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updated(): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.use');
|
||||||
|
unset($this->machines, $this->units);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(): mixed
|
||||||
|
{
|
||||||
|
return $this->view()->title(__('machines.index.title'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
|
||||||
|
<div class="space-y-2">
|
||||||
|
<h1 class="text-2xl font-semibold">{{ __('machines.index.title') }}</h1>
|
||||||
|
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('machines.index.intro') }}</p>
|
||||||
|
@can('machines.manage')
|
||||||
|
<p><x-button :href="route('machines.create')" wire:navigate icon="plus" :text="__('machines.index.new')" /></p>
|
||||||
|
@endcan
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex flex-wrap items-end gap-4">
|
||||||
|
<div class="w-full max-w-xs">
|
||||||
|
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||||
|
<x-select.native id="machine-unit" wire:model.live="unit" :label="__('machines.index.unit')" :aria-label="__('machines.index.unit')">
|
||||||
|
<option value="">{{ __('machines.index.all_units') }}</option>
|
||||||
|
@foreach ($this->units as $unitId => $unitName)
|
||||||
|
<option value="{{ $unitId }}">{{ $unitName }}</option>
|
||||||
|
@endforeach
|
||||||
|
</x-select.native>
|
||||||
|
</div>
|
||||||
|
@can('machines.manage')
|
||||||
|
<x-checkbox id="include-inactive-machines" wire:model.live="includeInactive" :label="__('machines.index.include_inactive')" />
|
||||||
|
@endcan
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@forelse ($this->grouped() as $unitName => $machines)
|
||||||
|
<section wire:key="unit-{{ md5($unitName) }}" aria-label="{{ $unitName }}" class="space-y-2">
|
||||||
|
<h2 class="text-lg font-semibold">{{ $unitName }}</h2>
|
||||||
|
<ul class="divide-y divide-zinc-200 rounded-lg border border-zinc-300 dark:divide-zinc-700 dark:border-zinc-600">
|
||||||
|
@foreach ($machines as $machine)
|
||||||
|
<li wire:key="machine-{{ md5($unitName) }}-{{ $machine->id }}" class="flex flex-wrap items-center justify-between gap-3 p-3">
|
||||||
|
<div class="min-w-0 space-y-1">
|
||||||
|
<p class="font-medium break-words">
|
||||||
|
<x-link :href="route('machines.show', $machine->id)" navigate :aria-label="__('machines.index.open_named', ['name' => $machine->name])">{{ $machine->name }}</x-link>
|
||||||
|
</p>
|
||||||
|
<p class="text-sm text-zinc-700 dark:text-zinc-300">
|
||||||
|
{{ __('machines.types.'.$machine->type->value) }}
|
||||||
|
@if ($machine->responsibleName !== null)
|
||||||
|
· {{ __('machines.index.responsible', ['name' => $machine->responsibleName]) }}
|
||||||
|
@endif
|
||||||
|
@unless ($machine->isActive)
|
||||||
|
· {{ __('machines.index.inactive') }}
|
||||||
|
@endunless
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
@empty
|
||||||
|
<p class="text-sm">{{ __('machines.index.none') }}</p>
|
||||||
|
@endforelse
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,386 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||||||
|
use App\Modules\Machines\Application\DTOs\DayLoad;
|
||||||
|
use App\Modules\Machines\Application\DTOs\MachineSummary;
|
||||||
|
use App\Modules\Machines\Application\DTOs\QueueRow;
|
||||||
|
use App\Modules\Machines\Application\UseCases\MachineWorkList;
|
||||||
|
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\Presentation\Livewire\Concerns\FormatsMachines;
|
||||||
|
use App\Modules\Users\Application\Contracts\Clock;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
use Livewire\Attributes\Computed;
|
||||||
|
use Livewire\Attributes\Locked;
|
||||||
|
use Livewire\Attributes\Url;
|
||||||
|
use Livewire\Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A machine's page (spec 19.4): its details and the planning view of its
|
||||||
|
* day-by-day work list. The day is chosen with Previous / Today / Next or
|
||||||
|
* a date, and the coming days show their rows and estimated hours. Rows
|
||||||
|
* are moved up and down with buttons, and a task's row can be moved to
|
||||||
|
* another day or machine, or taken off.
|
||||||
|
*/
|
||||||
|
new class extends Component
|
||||||
|
{
|
||||||
|
use FormatsMachines;
|
||||||
|
|
||||||
|
public const int LOAD_DAYS = 7;
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public int $machineId;
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
#[Url(as: 'paiva', except: '')]
|
||||||
|
public string $day = '';
|
||||||
|
|
||||||
|
public string $dayInput = '';
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public ?int $editingEntryId = null;
|
||||||
|
|
||||||
|
public string $editMachineId = '';
|
||||||
|
|
||||||
|
public string $editDay = '';
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public string $message = '';
|
||||||
|
|
||||||
|
public function mount(MachineQueries $machines, Clock $clock, int $machine): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.use');
|
||||||
|
|
||||||
|
$summary = $machines->findMachine($machine) ?? abort(404);
|
||||||
|
$this->machineId = $summary->id;
|
||||||
|
$this->day = $this->validDay($this->day) ?? $clock->today()->format('Y-m-d');
|
||||||
|
$this->dayInput = $this->day;
|
||||||
|
$this->message = (string) session('status', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Computed]
|
||||||
|
public function machine(): MachineSummary
|
||||||
|
{
|
||||||
|
return app(MachineQueries::class)->findMachine($this->machineId) ?? abort(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Computed]
|
||||||
|
public function today(): string
|
||||||
|
{
|
||||||
|
return app(Clock::class)->today()->format('Y-m-d');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<QueueRow>
|
||||||
|
*/
|
||||||
|
#[Computed]
|
||||||
|
public function rows(): array
|
||||||
|
{
|
||||||
|
return app(MachineQueries::class)->rowsOn($this->machineId, $this->day, $this->today);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<DayLoad>
|
||||||
|
*/
|
||||||
|
#[Computed]
|
||||||
|
public function load(): array
|
||||||
|
{
|
||||||
|
return app(MachineQueries::class)->load($this->machineId, $this->today, self::LOAD_DAYS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Machines a row's task could move to.
|
||||||
|
*
|
||||||
|
* @return list<MachineSummary>
|
||||||
|
*/
|
||||||
|
public function machinesFor(QueueRow $row): array
|
||||||
|
{
|
||||||
|
return $row->task === null ? [] : app(MachineQueries::class)->machinesFor($row->task->allowedServiceUnitIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function showDay(string $which): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.use');
|
||||||
|
|
||||||
|
$current = CarbonImmutable::createFromFormat('!Y-m-d', $this->day);
|
||||||
|
$this->day = match ($which) {
|
||||||
|
'previous' => $current->subDay()->format('Y-m-d'),
|
||||||
|
'next' => $current->addDay()->format('Y-m-d'),
|
||||||
|
'chosen' => $this->validDay($this->dayInput) ?? $this->day,
|
||||||
|
default => $this->today,
|
||||||
|
};
|
||||||
|
$this->dayInput = $this->day;
|
||||||
|
$this->editingEntryId = null;
|
||||||
|
$this->message = '';
|
||||||
|
unset($this->rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function showDate(string $date): void
|
||||||
|
{
|
||||||
|
$this->dayInput = $date;
|
||||||
|
$this->showDay('chosen');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function move(int $entryId, string $direction, MachineWorkList $workList): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.use');
|
||||||
|
|
||||||
|
$row = $this->row($entryId);
|
||||||
|
$up = $direction === 'up';
|
||||||
|
|
||||||
|
try {
|
||||||
|
$moved = $workList->move($entryId, $up);
|
||||||
|
} catch (QueueEntryNotFound|QueueEntryCompleted) {
|
||||||
|
$this->message = __('machines.errors.not_found');
|
||||||
|
unset($this->rows);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$title = $this->rowTitle($row);
|
||||||
|
$this->message = __('machines.show.'.($moved ? ($up ? 'moved_up' : 'moved_down') : ($up ? 'first' : 'last')), ['title' => $title]);
|
||||||
|
unset($this->rows);
|
||||||
|
$this->js("document.getElementById('row-{$entryId}-{$direction}')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function startEdit(int $entryId): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.use');
|
||||||
|
|
||||||
|
$row = $this->row($entryId);
|
||||||
|
$this->editingEntryId = $entryId;
|
||||||
|
$this->editMachineId = (string) $row->machineId;
|
||||||
|
$this->editDay = $row->isLate ? $this->today : $row->plannedOn;
|
||||||
|
$this->resetErrorBag();
|
||||||
|
$this->js("setTimeout(() => document.getElementById('edit-day')?.focus())");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cancelEdit(): void
|
||||||
|
{
|
||||||
|
$entryId = $this->editingEntryId;
|
||||||
|
$this->editingEntryId = null;
|
||||||
|
$this->js("document.getElementById('row-{$entryId}-edit')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reschedule(MachineWorkList $workList): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.use');
|
||||||
|
|
||||||
|
$row = $this->row((int) $this->editingEntryId);
|
||||||
|
abort_if($row->taskId === null, 404);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$workList->planTask($row->taskId, (int) $this->editMachineId, $this->editDay);
|
||||||
|
} catch (InvalidPlannedDay) {
|
||||||
|
$this->addError('editDay', __('machines.errors.day'));
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (MachineNotFound|MachineInactive|MachineNotForTask) {
|
||||||
|
$this->addError('editMachineId', __('machines.errors.machine'));
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (MachineTaskNotFound) {
|
||||||
|
$this->message = __('machines.errors.task_done');
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$machine = app(MachineQueries::class)->findMachine((int) $this->editMachineId);
|
||||||
|
$this->message = __('machines.task.moved', ['machine' => $machine->name ?? '', 'date' => $this->dateLabel($this->editDay)]);
|
||||||
|
$this->editingEntryId = null;
|
||||||
|
unset($this->rows, $this->load);
|
||||||
|
$this->js("document.getElementById('work-list-status')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function remove(int $entryId, MachineWorkList $workList): void
|
||||||
|
{
|
||||||
|
$this->authorize('machines.use');
|
||||||
|
|
||||||
|
$row = $this->row($entryId);
|
||||||
|
abort_if($row->taskId === null, 404);
|
||||||
|
|
||||||
|
$workList->removeTask($row->taskId);
|
||||||
|
$this->message = __('machines.task.removed');
|
||||||
|
unset($this->rows, $this->load);
|
||||||
|
$this->js("document.getElementById('work-list-status')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rowTitle(QueueRow $row): string
|
||||||
|
{
|
||||||
|
return $row->task === null ? __('machines.show.order') : $row->task->title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(): mixed
|
||||||
|
{
|
||||||
|
return $this->view()->title($this->machine->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function row(int $entryId): QueueRow
|
||||||
|
{
|
||||||
|
return collect($this->rows)->firstWhere('entryId', $entryId) ?? abort(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validDay(string $value): ?string
|
||||||
|
{
|
||||||
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) !== 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$date = CarbonImmutable::createFromFormat('!Y-m-d', $value);
|
||||||
|
|
||||||
|
return $date !== null && $date->format('Y-m-d') === $value ? $value : null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
|
||||||
|
@php($machine = $this->machine)
|
||||||
|
|
||||||
|
<div class="space-y-2">
|
||||||
|
<p class="text-sm"><x-link :href="route('machines.index')" navigate>{{ __('machines.show.back') }}</x-link></p>
|
||||||
|
<h1 class="text-2xl font-semibold break-words">{{ $machine->name }}</h1>
|
||||||
|
<p class="text-sm">
|
||||||
|
{{ __('machines.types.'.$machine->type->value) }}
|
||||||
|
· {{ __('machines.show.units', ['units' => implode(', ', $machine->serviceUnitNames)]) }}
|
||||||
|
@if ($machine->responsibleName !== null)
|
||||||
|
· {{ __('machines.index.responsible', ['name' => $machine->responsibleName]) }}
|
||||||
|
@endif
|
||||||
|
@unless ($machine->isActive)
|
||||||
|
· {{ __('machines.index.inactive') }}
|
||||||
|
@endunless
|
||||||
|
</p>
|
||||||
|
@if ($machine->description !== null)
|
||||||
|
<p class="text-sm whitespace-pre-line">{{ $machine->description }}</p>
|
||||||
|
@endif
|
||||||
|
@can('machines.manage')
|
||||||
|
<p><x-button :href="route('machines.edit', $machine->id)" wire:navigate sm icon="pencil" :text="__('machines.show.edit')" /></p>
|
||||||
|
@endcan
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p id="work-list-status" tabindex="-1" role="status" aria-live="polite" class="text-sm font-medium focus:outline-none">{{ $message }}</p>
|
||||||
|
|
||||||
|
<section aria-labelledby="work-list-heading" class="space-y-4">
|
||||||
|
<h2 id="work-list-heading" class="text-xl font-semibold">{{ __('machines.show.work_list') }}</h2>
|
||||||
|
|
||||||
|
<nav aria-label="{{ __('machines.show.day_navigation') }}" class="flex flex-wrap items-end gap-2">
|
||||||
|
<x-button wire:click="showDay('previous')" subtle color="black" sm icon="chevron-left" :text="__('machines.show.previous_day')" />
|
||||||
|
<x-button wire:click="showDay('today')" subtle color="black" sm :text="__('machines.show.today')" />
|
||||||
|
<x-button wire:click="showDay('next')" subtle color="black" sm icon="chevron-right" position="right" :text="__('machines.show.next_day')" />
|
||||||
|
<form wire:submit="showDay('chosen')" class="flex flex-wrap items-end gap-2">
|
||||||
|
<x-input id="work-list-day" wire:model="dayInput" type="date" :label="__('machines.show.day_field')" />
|
||||||
|
<x-button submit subtle color="black" sm :text="__('machines.show.show_day')" />
|
||||||
|
</form>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<div class="space-y-1">
|
||||||
|
<h3 class="font-semibold">{{ __('machines.show.load') }}</h3>
|
||||||
|
<ul class="flex flex-wrap gap-2 text-sm">
|
||||||
|
@foreach ($this->load as $dayLoad)
|
||||||
|
<li wire:key="load-{{ $dayLoad->day }}">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
wire:click="showDate('{{ $dayLoad->day }}')"
|
||||||
|
class="rounded-md border border-zinc-300 px-2 py-1 hover:bg-zinc-100 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:border-zinc-600 dark:hover:bg-zinc-700 @if ($dayLoad->day === $day) font-semibold @endif"
|
||||||
|
@if ($dayLoad->day === $day) aria-current="date" @endif
|
||||||
|
>
|
||||||
|
{{ __('machines.show.load_row', ['date' => $this->shortDay($dayLoad->day), 'rows' => trans_choice('machines.show.rows', $dayLoad->rows, ['count' => $dayLoad->rows]), 'hours' => $this->quarters($dayLoad->estimatedQuarters)]) }}
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
@endforeach
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3 class="text-lg font-semibold" aria-live="polite">{{ __('machines.show.day_heading', ['date' => $this->dateLabel($day)]) }}</h3>
|
||||||
|
<p class="text-sm">{{ trans_choice('machines.show.row_count', count($this->rows), ['count' => count($this->rows)]) }}</p>
|
||||||
|
|
||||||
|
@if ($this->rows === [])
|
||||||
|
<p class="text-sm">{{ __('machines.show.none') }}</p>
|
||||||
|
@else
|
||||||
|
<ol class="space-y-3">
|
||||||
|
@foreach ($this->rows as $row)
|
||||||
|
@php($task = $row->task)
|
||||||
|
<li wire:key="row-{{ $row->entryId }}" class="space-y-2 rounded-lg border border-zinc-300 p-3 dark:border-zinc-600">
|
||||||
|
<div class="flex flex-wrap items-start justify-between gap-2">
|
||||||
|
<div class="min-w-0 space-y-1">
|
||||||
|
<p class="font-medium break-words">{{ $this->rowTitle($row) }}</p>
|
||||||
|
@if ($task !== null)
|
||||||
|
<p class="text-sm">{{ $task->jobNumber }} {{ $task->jobName }} · {{ $task->customerName }}</p>
|
||||||
|
<p class="flex flex-wrap gap-x-3 gap-y-1 text-sm text-zinc-700 dark:text-zinc-300">
|
||||||
|
@if ($task->dueOn !== null)
|
||||||
|
<span>{{ __('machines.show.due', ['date' => $this->dateLabel($task->dueOn)]) }}</span>
|
||||||
|
@endif
|
||||||
|
<span>{{ __('machines.show.priority', ['priority' => __('jobs.priorities.'.$task->priority)]) }}</span>
|
||||||
|
<span>{{ __('machines.show.status', ['status' => __('jobs.tasks.statuses.'.$task->status)]) }}</span>
|
||||||
|
</p>
|
||||||
|
@endif
|
||||||
|
@if ($row->isLate)
|
||||||
|
<p class="flex items-center gap-1 text-sm font-medium text-red-700 dark:text-red-400">
|
||||||
|
<x-icon name="exclamation-triangle" class="size-4" aria-hidden="true" />
|
||||||
|
{{ __('machines.show.late', ['date' => CarbonImmutable::parse($row->plannedOn)->format('j.n.')]) }}
|
||||||
|
</p>
|
||||||
|
@endif
|
||||||
|
@if ($task !== null && $task->isOutsidePlan($row->isLate ? $this->today : $row->plannedOn))
|
||||||
|
<p class="flex items-center gap-1 text-sm">
|
||||||
|
<x-icon name="information-circle" class="size-4" aria-hidden="true" />
|
||||||
|
{{ __('machines.show.outside_plan') }}
|
||||||
|
</p>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
@if ($task !== null)
|
||||||
|
<x-link :href="route('jobs.show', $task->jobId).'#task-'.$task->taskId" navigate class="text-sm">{{ __('machines.show.open_task') }}</x-link>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if ($task !== null && ($task->instructions !== null || $task->description !== null))
|
||||||
|
<div class="space-y-1 text-sm">
|
||||||
|
@if ($task->instructions !== null)
|
||||||
|
<details>
|
||||||
|
<summary class="cursor-pointer font-medium">{{ __('machines.show.instructions') }}</summary>
|
||||||
|
<p class="mt-1 whitespace-pre-line">{{ $task->instructions }}</p>
|
||||||
|
</details>
|
||||||
|
@endif
|
||||||
|
@if ($task->description !== null)
|
||||||
|
<details>
|
||||||
|
<summary class="cursor-pointer font-medium">{{ __('machines.show.description') }}</summary>
|
||||||
|
<p class="mt-1 whitespace-pre-line">{{ $task->description }}</p>
|
||||||
|
</details>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if ($editingEntryId === $row->entryId)
|
||||||
|
<form wire:submit="reschedule" class="flex flex-wrap items-end gap-2" novalidate>
|
||||||
|
<x-input id="edit-day" wire:model="editDay" type="date" :label="__('machines.task.day')" />
|
||||||
|
<div class="min-w-48">
|
||||||
|
<x-select.native id="edit-machine" wire:model="editMachineId" :label="__('machines.task.machine')" :aria-label="__('machines.task.machine')">
|
||||||
|
@foreach ($this->machinesFor($row) as $option)
|
||||||
|
<option value="{{ $option->id }}">{{ $option->name }}</option>
|
||||||
|
@endforeach
|
||||||
|
</x-select.native>
|
||||||
|
</div>
|
||||||
|
<x-button submit sm :text="__('machines.task.save')" />
|
||||||
|
<x-button wire:click="cancelEdit" subtle color="black" sm :text="__('machines.task.cancel')" />
|
||||||
|
</form>
|
||||||
|
@else
|
||||||
|
<div class="flex flex-wrap gap-2">
|
||||||
|
@unless ($row->isLate || $row->isOrder())
|
||||||
|
<x-button id="row-{{ $row->entryId }}-up" wire:click="move({{ $row->entryId }}, 'up')" subtle color="black" sm icon="arrow-up" :aria-label="__('machines.show.up_named', ['title' => $this->rowTitle($row)])" />
|
||||||
|
<x-button id="row-{{ $row->entryId }}-down" wire:click="move({{ $row->entryId }}, 'down')" subtle color="black" sm icon="arrow-down" :aria-label="__('machines.show.down_named', ['title' => $this->rowTitle($row)])" />
|
||||||
|
@endunless
|
||||||
|
@if ($task !== null)
|
||||||
|
<x-button id="row-{{ $row->entryId }}-edit" wire:click="startEdit({{ $row->entryId }})" subtle color="black" sm icon="calendar-days" :text="__('machines.show.reschedule')" :aria-label="__('machines.show.reschedule_named', ['title' => $task->title])" />
|
||||||
|
<x-button wire:click="remove({{ $row->entryId }})" subtle color="black" sm icon="x-mark" :text="__('machines.task.remove')" :aria-label="__('machines.task.remove').': '.$task->title" />
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
</li>
|
||||||
|
@endforeach
|
||||||
|
</ol>
|
||||||
|
@endif
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,210 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||||||
|
use App\Modules\Machines\Application\Contracts\MachineTasks;
|
||||||
|
use App\Modules\Machines\Application\DTOs\MachineSummary;
|
||||||
|
use App\Modules\Machines\Application\DTOs\MachineTaskInfo;
|
||||||
|
use App\Modules\Machines\Application\DTOs\QueueRow;
|
||||||
|
use App\Modules\Machines\Application\UseCases\MachineWorkList;
|
||||||
|
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\Presentation\Livewire\Concerns\FormatsMachines;
|
||||||
|
use App\Modules\Users\Application\Contracts\Clock;
|
||||||
|
use Livewire\Attributes\Computed;
|
||||||
|
use Livewire\Attributes\Locked;
|
||||||
|
use Livewire\Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A task's machine in its card on the job page (spec 19.4): "Move to
|
||||||
|
* machine" puts the task on a machine's work list for a day (by default
|
||||||
|
* the start of its schedule, else today); the machine and day can be
|
||||||
|
* changed later, and the task taken off. Shown only when the task's
|
||||||
|
* service unit uses stock, or when the task already is on a list.
|
||||||
|
*/
|
||||||
|
new class extends Component
|
||||||
|
{
|
||||||
|
use FormatsMachines;
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public int $jobId;
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public int $taskId;
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public string $taskTitle = '';
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public bool $editing = false;
|
||||||
|
|
||||||
|
public string $machineId = '';
|
||||||
|
|
||||||
|
public string $day = '';
|
||||||
|
|
||||||
|
#[Locked]
|
||||||
|
public string $message = '';
|
||||||
|
|
||||||
|
public function mount(): void
|
||||||
|
{
|
||||||
|
$this->authorize('jobs.view-job', $this->jobId);
|
||||||
|
abort_if($this->task !== null && $this->task->jobId !== $this->jobId, 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Computed]
|
||||||
|
public function task(): ?MachineTaskInfo
|
||||||
|
{
|
||||||
|
return app(MachineTasks::class)->info($this->taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Computed]
|
||||||
|
public function row(): ?QueueRow
|
||||||
|
{
|
||||||
|
return app(MachineQueries::class)->rowForTask($this->taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<MachineSummary>
|
||||||
|
*/
|
||||||
|
#[Computed]
|
||||||
|
public function machines(): array
|
||||||
|
{
|
||||||
|
return $this->task === null ? [] : app(MachineQueries::class)->machinesFor($this->task->allowedServiceUnitIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function machineName(int $machineId): string
|
||||||
|
{
|
||||||
|
return app(MachineQueries::class)->findMachine($machineId)->name ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canPlan(): bool
|
||||||
|
{
|
||||||
|
return $this->task !== null && ! $this->task->isDone && $this->task->allowedServiceUnitIds !== [] && auth()->user()?->can('machines.use') === true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function startEdit(): void
|
||||||
|
{
|
||||||
|
$this->authorizePlanning();
|
||||||
|
|
||||||
|
$today = app(Clock::class)->today()->format('Y-m-d');
|
||||||
|
$this->editing = true;
|
||||||
|
$this->machineId = (string) ($this->row->machineId ?? ($this->machines[0]->id ?? ''));
|
||||||
|
$this->day = $this->row !== null && ! $this->row->isLate
|
||||||
|
? $this->row->plannedOn
|
||||||
|
: ($this->task?->plannedFrom !== null && $this->task->plannedFrom > $today ? $this->task->plannedFrom : $today);
|
||||||
|
$this->resetErrorBag();
|
||||||
|
$this->js("setTimeout(() => document.getElementById('task-{$this->taskId}-machine')?.focus())");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cancel(): void
|
||||||
|
{
|
||||||
|
$this->editing = false;
|
||||||
|
$this->js("document.getElementById('task-{$this->taskId}-move')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save(MachineWorkList $workList): void
|
||||||
|
{
|
||||||
|
$this->authorizePlanning();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$workList->planTask($this->taskId, (int) $this->machineId, $this->day);
|
||||||
|
} catch (InvalidPlannedDay) {
|
||||||
|
$this->addError('day', __('machines.errors.day'));
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (MachineNotFound|MachineInactive|MachineNotForTask) {
|
||||||
|
$this->addError('machineId', __('machines.errors.machine'));
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (MachineTaskNotFound) {
|
||||||
|
$this->message = __('machines.errors.task_done');
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->message = __('machines.task.moved', ['machine' => $this->machineName((int) $this->machineId), 'date' => $this->dateLabel($this->day)]);
|
||||||
|
$this->editing = false;
|
||||||
|
unset($this->row);
|
||||||
|
$this->js("document.getElementById('task-{$this->taskId}-machine-status')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function remove(MachineWorkList $workList): void
|
||||||
|
{
|
||||||
|
$this->authorizePlanning();
|
||||||
|
|
||||||
|
$workList->removeTask($this->taskId);
|
||||||
|
$this->message = __('machines.task.removed');
|
||||||
|
unset($this->row);
|
||||||
|
$this->js("document.getElementById('task-{$this->taskId}-machine-status')?.focus()");
|
||||||
|
}
|
||||||
|
|
||||||
|
private function authorizePlanning(): void
|
||||||
|
{
|
||||||
|
$this->authorize('jobs.view-job', $this->jobId);
|
||||||
|
abort_unless($this->canPlan(), 403);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
@if ($this->canPlan() || $this->row !== null)
|
||||||
|
<section aria-label="{{ __('machines.task.list_of', ['title' => $taskTitle]) }}" class="space-y-2">
|
||||||
|
<h4 class="font-medium">{{ __('machines.task.title') }}</h4>
|
||||||
|
|
||||||
|
<p id="task-{{ $taskId }}-machine-status" tabindex="-1" role="status" aria-live="polite" class="text-sm focus:outline-none">{{ $message }}</p>
|
||||||
|
|
||||||
|
@if ($this->row !== null)
|
||||||
|
<p class="text-sm">
|
||||||
|
<x-link :href="route('machines.show', ['machine' => $this->row->machineId, 'paiva' => $this->row->plannedOn])" navigate>
|
||||||
|
{{ __('machines.task.on_machine', ['machine' => $this->machineName($this->row->machineId), 'date' => $this->dateLabel($this->row->plannedOn)]) }}
|
||||||
|
</x-link>
|
||||||
|
</p>
|
||||||
|
@if ($this->row->plannedOn < app(Clock::class)->today()->format('Y-m-d'))
|
||||||
|
<p class="flex items-center gap-1 text-sm font-medium text-red-700 dark:text-red-400">
|
||||||
|
<x-icon name="exclamation-triangle" class="size-4" aria-hidden="true" />
|
||||||
|
{{ __('machines.task.late') }}
|
||||||
|
</p>
|
||||||
|
@elseif ($this->task !== null && $this->task->isOutsidePlan($this->row->plannedOn))
|
||||||
|
<p class="flex items-center gap-1 text-sm">
|
||||||
|
<x-icon name="information-circle" class="size-4" aria-hidden="true" />
|
||||||
|
{{ __('machines.show.outside_plan') }}
|
||||||
|
</p>
|
||||||
|
@endif
|
||||||
|
@else
|
||||||
|
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('machines.task.not_on_machine') }}</p>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if ($this->canPlan())
|
||||||
|
@if ($editing)
|
||||||
|
@if ($this->machines === [])
|
||||||
|
<p class="text-sm">{{ __('machines.task.no_machines') }}</p>
|
||||||
|
<x-button wire:click="cancel" subtle color="black" sm :text="__('machines.task.cancel')" />
|
||||||
|
@else
|
||||||
|
<form wire:submit="save" class="flex flex-wrap items-end gap-2" novalidate>
|
||||||
|
<div class="min-w-48">
|
||||||
|
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||||||
|
<x-select.native id="task-{{ $taskId }}-machine" wire:model="machineId" :label="__('machines.task.machine')" :aria-label="__('machines.task.machine')">
|
||||||
|
@foreach ($this->machines as $machine)
|
||||||
|
<option value="{{ $machine->id }}">{{ $machine->name }}</option>
|
||||||
|
@endforeach
|
||||||
|
</x-select.native>
|
||||||
|
</div>
|
||||||
|
<x-input id="task-{{ $taskId }}-machine-day" wire:model="day" type="date" :label="__('machines.task.day')" :hint="__('machines.task.day_hint')" />
|
||||||
|
<x-button submit sm :text="__('machines.task.save')" />
|
||||||
|
<x-button wire:click="cancel" subtle color="black" sm :text="__('machines.task.cancel')" />
|
||||||
|
</form>
|
||||||
|
@endif
|
||||||
|
@else
|
||||||
|
<div class="flex flex-wrap gap-2">
|
||||||
|
<x-button id="task-{{ $taskId }}-move" wire:click="startEdit" subtle color="black" sm icon="printer" :text="$this->row === null ? __('machines.task.move') : __('machines.task.change')" />
|
||||||
|
@if ($this->row !== null)
|
||||||
|
<x-button wire:click="remove" subtle color="black" sm icon="x-mark" :text="__('machines.task.remove')" />
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
@endif
|
||||||
|
</section>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
Route::middleware(['web', 'auth', 'can:machines.use'])
|
||||||
|
->prefix('machines')
|
||||||
|
->name('machines.')
|
||||||
|
->group(function () {
|
||||||
|
Route::livewire('/', 'machines::index')->name('index');
|
||||||
|
|
||||||
|
Route::middleware('can:machines.manage')->group(function () {
|
||||||
|
Route::livewire('/create', 'machines::form')->name('create');
|
||||||
|
Route::livewire('/{machine}/edit', 'machines::form')->whereNumber('machine')->name('edit');
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::livewire('/{machine}', 'machines::show')->whereNumber('machine')->name('show');
|
||||||
|
});
|
||||||
@@ -69,6 +69,7 @@ final class MainMenu
|
|||||||
['route' => 'jobs.quick', 'pattern' => ['jobs.quick'], 'label' => 'layout.quick_intake', 'ability' => 'jobs.manage'],
|
['route' => 'jobs.quick', 'pattern' => ['jobs.quick'], 'label' => 'layout.quick_intake', 'ability' => 'jobs.manage'],
|
||||||
['route' => 'jobs.board', 'pattern' => ['jobs.board'], 'label' => 'layout.board', 'ability' => 'jobs.view'],
|
['route' => 'jobs.board', 'pattern' => ['jobs.board'], 'label' => 'layout.board', 'ability' => 'jobs.view'],
|
||||||
['route' => 'jobs.calendar', 'pattern' => ['jobs.calendar'], 'label' => 'layout.calendar', 'ability' => 'jobs.view'],
|
['route' => 'jobs.calendar', 'pattern' => ['jobs.calendar'], 'label' => 'layout.calendar', 'ability' => 'jobs.view'],
|
||||||
|
['route' => 'machines.index', 'pattern' => ['machines.*'], 'label' => 'layout.machines', 'ability' => 'machines.menu'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'own' => [
|
'own' => [
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use App\Modules\Dashboard\Infrastructure\Providers\DashboardServiceProvider;
|
|||||||
use App\Modules\Expenses\Infrastructure\Providers\ExpensesServiceProvider;
|
use App\Modules\Expenses\Infrastructure\Providers\ExpensesServiceProvider;
|
||||||
use App\Modules\Inventory\Infrastructure\Providers\InventoryServiceProvider;
|
use App\Modules\Inventory\Infrastructure\Providers\InventoryServiceProvider;
|
||||||
use App\Modules\Jobs\Infrastructure\Providers\JobsServiceProvider;
|
use App\Modules\Jobs\Infrastructure\Providers\JobsServiceProvider;
|
||||||
|
use App\Modules\Machines\Infrastructure\Providers\MachinesServiceProvider;
|
||||||
use App\Modules\Notifications\Infrastructure\Providers\NotificationsServiceProvider;
|
use App\Modules\Notifications\Infrastructure\Providers\NotificationsServiceProvider;
|
||||||
use App\Modules\Reports\Infrastructure\Providers\ReportsServiceProvider;
|
use App\Modules\Reports\Infrastructure\Providers\ReportsServiceProvider;
|
||||||
use App\Modules\Sales\Infrastructure\Providers\SalesServiceProvider;
|
use App\Modules\Sales\Infrastructure\Providers\SalesServiceProvider;
|
||||||
@@ -28,6 +29,7 @@ return [
|
|||||||
ExpensesServiceProvider::class,
|
ExpensesServiceProvider::class,
|
||||||
SalesServiceProvider::class,
|
SalesServiceProvider::class,
|
||||||
InventoryServiceProvider::class,
|
InventoryServiceProvider::class,
|
||||||
|
MachinesServiceProvider::class,
|
||||||
BillingServiceProvider::class,
|
BillingServiceProvider::class,
|
||||||
NotificationsServiceProvider::class,
|
NotificationsServiceProvider::class,
|
||||||
ReportsServiceProvider::class,
|
ReportsServiceProvider::class,
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
||||||
|
use App\Modules\Machines\Domain\Enums\MachineType;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<MachineModel>
|
||||||
|
*/
|
||||||
|
class MachineFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = MachineModel::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => fake()->unique()->words(2, true),
|
||||||
|
'type' => MachineType::Printer,
|
||||||
|
'is_active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<int> $serviceUnitIds
|
||||||
|
*/
|
||||||
|
public function inUnits(array $serviceUnitIds): static
|
||||||
|
{
|
||||||
|
return $this->afterCreating(function (MachineModel $machine) use ($serviceUnitIds): void {
|
||||||
|
// Replaces the default unit that configure() gave the machine first.
|
||||||
|
DB::table('machine_service_units')->where('machine_id', $machine->id)->delete();
|
||||||
|
DB::table('machine_service_units')->insert(array_map(
|
||||||
|
fn (int $unitId): array => ['machine_id' => $machine->id, 'service_unit_id' => $unitId],
|
||||||
|
$serviceUnitIds,
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every machine has a service unit: Painotuotanto (or a new one) unless given.
|
||||||
|
*/
|
||||||
|
public function configure(): static
|
||||||
|
{
|
||||||
|
return $this->afterCreating(function (MachineModel $machine): void {
|
||||||
|
if (DB::table('machine_service_units')->where('machine_id', $machine->id)->exists()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$unitId = ServiceUnitModel::query()->where('name', 'Painotuotanto')->value('id') ?? ServiceUnitModel::factory()->create()->id;
|
||||||
|
DB::table('machine_service_units')->insert(['machine_id' => $machine->id, 'service_unit_id' => $unitId]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Machines (spec 19.4–19.5): the machines in their service units, their
|
||||||
|
* counters and readings, and the day-by-day work list of each machine,
|
||||||
|
* whose rows are job tasks or steps of quick sale order lines (the
|
||||||
|
* order lines get their foreign key with their table).
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('machines', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name', 150);
|
||||||
|
$table->string('type', 20);
|
||||||
|
$table->text('description')->nullable();
|
||||||
|
$table->foreignId('responsible_user_id')->nullable()->constrained('users')->nullOnDelete();
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('machine_service_units', function (Blueprint $table) {
|
||||||
|
$table->foreignId('machine_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->foreignId('service_unit_id')->constrained()->restrictOnDelete();
|
||||||
|
|
||||||
|
$table->primary(['machine_id', 'service_unit_id']);
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('machine_counters', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('machine_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->string('name', 100);
|
||||||
|
$table->string('unit', 20);
|
||||||
|
$table->unsignedInteger('sort_order');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('machine_counter_readings', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('machine_counter_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->bigInteger('reading_milli');
|
||||||
|
$table->foreignId('user_id')->constrained()->restrictOnDelete();
|
||||||
|
$table->timestamp('read_at');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->index(['machine_counter_id', 'read_at']);
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('machine_queue_entries', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('machine_id')->constrained()->restrictOnDelete();
|
||||||
|
$table->date('planned_on');
|
||||||
|
$table->unsignedInteger('position');
|
||||||
|
$table->foreignId('task_id')->nullable()->constrained()->cascadeOnDelete();
|
||||||
|
$table->unsignedBigInteger('quick_sale_order_line_id')->nullable();
|
||||||
|
$table->unsignedSmallInteger('production_step_position')->nullable();
|
||||||
|
$table->timestamp('completed_at')->nullable();
|
||||||
|
$table->foreignId('completed_by')->nullable()->constrained('users')->nullOnDelete();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->index(['machine_id', 'completed_at', 'planned_on']);
|
||||||
|
$table->index(['task_id', 'completed_at']);
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('stock_movements', function (Blueprint $table) {
|
||||||
|
$table->foreign('machine_id', 'stock_movements_machine_foreign')->references('id')->on('machines')->restrictOnDelete();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
// SQLite (tests) cannot drop a foreign key; there the table is dropped by the earlier migration's rollback.
|
||||||
|
if (Schema::getConnection()->getDriverName() !== 'sqlite') {
|
||||||
|
Schema::table('stock_movements', function (Blueprint $table) {
|
||||||
|
$table->dropForeign('stock_movements_machine_foreign');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Schema::dropIfExists('machine_queue_entries');
|
||||||
|
Schema::dropIfExists('machine_counter_readings');
|
||||||
|
Schema::dropIfExists('machine_counters');
|
||||||
|
Schema::dropIfExists('machine_service_units');
|
||||||
|
Schema::dropIfExists('machines');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'types' => [
|
||||||
|
'printer' => 'Printing press / printer',
|
||||||
|
'cutter' => 'Sticker cutter',
|
||||||
|
'other' => 'Other',
|
||||||
|
],
|
||||||
|
'counter_units' => [
|
||||||
|
'piece' => 'pcs',
|
||||||
|
'sheet' => 'sheet',
|
||||||
|
'square_metre' => 'm²',
|
||||||
|
'millilitre' => 'ml',
|
||||||
|
'metre' => 'm',
|
||||||
|
'hour' => 'h',
|
||||||
|
],
|
||||||
|
'counter_names' => [
|
||||||
|
'prints' => 'Prints',
|
||||||
|
'printed_area' => 'Printed area',
|
||||||
|
'cut_length' => 'Cut length',
|
||||||
|
],
|
||||||
|
|
||||||
|
'index' => [
|
||||||
|
'title' => 'Machines',
|
||||||
|
'intro' => 'Machines by service unit. A machine\'s page has its day-by-day work list; the machine console is meant for the tablet next to the machine.',
|
||||||
|
'new' => 'New machine',
|
||||||
|
'unit' => 'Service unit',
|
||||||
|
'all_units' => 'All service units',
|
||||||
|
'include_inactive' => 'Also show machines out of use',
|
||||||
|
'none' => 'No machines.',
|
||||||
|
'inactive' => 'Out of use',
|
||||||
|
'responsible' => 'Responsible: :name',
|
||||||
|
'open' => 'Work list and details',
|
||||||
|
'open_named' => 'Work list and details of :name',
|
||||||
|
],
|
||||||
|
|
||||||
|
'form' => [
|
||||||
|
'create_title' => 'New machine',
|
||||||
|
'edit_title' => 'Edit machine',
|
||||||
|
'name' => 'Name',
|
||||||
|
'type' => 'Machine type',
|
||||||
|
'description' => 'Description',
|
||||||
|
'service_units' => 'Service units',
|
||||||
|
'service_units_hint' => 'Only service units that use stock and machines. A shared machine belongs to several units.',
|
||||||
|
'no_stock_units' => 'No service unit uses stock and machines. Turn it on in the catalog.',
|
||||||
|
'responsible' => 'Responsible person',
|
||||||
|
'responsible_hint' => 'Gets the notifications of maintenance coming up and late.',
|
||||||
|
'no_responsible' => 'No responsible person',
|
||||||
|
'counters' => 'Counters',
|
||||||
|
'counters_hint' => 'Counters measure the need for maintenance, e.g. prints or printed area. Readings are recorded on the machine console.',
|
||||||
|
'counter_name' => 'Counter name',
|
||||||
|
'counter_unit' => 'Unit',
|
||||||
|
'counter_named' => 'Counter :number',
|
||||||
|
'add_counter' => 'Add counter',
|
||||||
|
'remove_counter_named' => 'Remove counter :number',
|
||||||
|
'suggest_counters' => 'Add the usual counters of the machine type',
|
||||||
|
'counter_added' => 'Counter :number added.',
|
||||||
|
'counter_removed' => 'Counter :number removed.',
|
||||||
|
'counters_suggested' => '{0} There are no suggested counters for the machine type.|{1} 1 counter added.|[2,*] :count counters added.',
|
||||||
|
'save' => 'Save',
|
||||||
|
'cancel' => 'Cancel',
|
||||||
|
'created' => 'Machine :name added.',
|
||||||
|
'updated' => 'Machine :name saved.',
|
||||||
|
'deactivate' => 'Take out of use',
|
||||||
|
'activate' => 'Take into use',
|
||||||
|
'deactivated' => 'The machine was taken out of use. It is not offered in choices, but its history is kept.',
|
||||||
|
'activated' => 'The machine was taken into use.',
|
||||||
|
],
|
||||||
|
|
||||||
|
'show' => [
|
||||||
|
'details' => 'Details',
|
||||||
|
'edit' => 'Edit',
|
||||||
|
'console' => 'Open the machine console',
|
||||||
|
'units' => 'Service units: :units',
|
||||||
|
'work_list' => 'Work list',
|
||||||
|
'day_navigation' => 'Choose day',
|
||||||
|
'previous_day' => 'Previous day',
|
||||||
|
'today' => 'Today',
|
||||||
|
'next_day' => 'Next day',
|
||||||
|
'day_field' => 'Day',
|
||||||
|
'show_day' => 'Show day',
|
||||||
|
'day_heading' => 'Work list :date',
|
||||||
|
'load' => 'Coming days',
|
||||||
|
'load_row' => ':date: :rows, estimate :hours h',
|
||||||
|
'rows' => '{0} no rows|{1} 1 row|[2,*] :count rows',
|
||||||
|
'none' => 'No work for this day.',
|
||||||
|
'row_count' => '{0} No rows.|{1} 1 row.|[2,*] :count rows.',
|
||||||
|
'late' => 'Late (planned :date)',
|
||||||
|
'order' => 'Customer waiting',
|
||||||
|
'outside_plan' => 'The day is outside the task\'s schedule or after its due date.',
|
||||||
|
'due' => 'Due :date',
|
||||||
|
'priority' => 'Priority: :priority',
|
||||||
|
'status' => 'Status: :status',
|
||||||
|
'instructions' => 'Job instructions',
|
||||||
|
'description' => 'Task description',
|
||||||
|
'up_named' => 'Move up: :title',
|
||||||
|
'down_named' => 'Move down: :title',
|
||||||
|
'moved_up' => ':title moved up.',
|
||||||
|
'moved_down' => ':title moved down.',
|
||||||
|
'first' => ':title is already first.',
|
||||||
|
'last' => ':title is already last.',
|
||||||
|
'open_task' => 'Open the task on the job page',
|
||||||
|
'reschedule' => 'Change day or machine',
|
||||||
|
'reschedule_named' => 'Change day or machine: :title',
|
||||||
|
'back' => 'Back to machines',
|
||||||
|
],
|
||||||
|
|
||||||
|
'task' => [
|
||||||
|
'title' => 'Machine',
|
||||||
|
'list_of' => 'Machine: :title',
|
||||||
|
'not_on_machine' => 'The task is not on any machine\'s work list.',
|
||||||
|
'on_machine' => 'On the work list of :machine on :date.',
|
||||||
|
'late' => 'Late: the planned day has passed.',
|
||||||
|
'move' => 'Move to machine',
|
||||||
|
'change' => 'Change machine or day',
|
||||||
|
'machine' => 'Machine',
|
||||||
|
'choose_machine' => 'Choose a machine',
|
||||||
|
'day' => 'Production day',
|
||||||
|
'day_hint' => 'By default the start of the task\'s schedule, else today.',
|
||||||
|
'save' => 'Save',
|
||||||
|
'cancel' => 'Cancel',
|
||||||
|
'remove' => 'Remove from machine',
|
||||||
|
'moved' => 'The task was moved to the work list of :machine on :date.',
|
||||||
|
'removed' => 'The task was removed from the machine\'s work list.',
|
||||||
|
'no_machines' => 'There are no machines in use in the task\'s service unit.',
|
||||||
|
],
|
||||||
|
|
||||||
|
'errors' => [
|
||||||
|
'name' => 'Enter a name (1–150 characters).',
|
||||||
|
'name_taken' => 'The name is already in use.',
|
||||||
|
'service_units' => 'Choose at least one service unit.',
|
||||||
|
'counter' => 'Give the counter a name (1–100 characters) and a unit.',
|
||||||
|
'machine' => 'Choose a machine in use from the task\'s service unit.',
|
||||||
|
'day' => 'Enter the day as dd.mm.yyyy.',
|
||||||
|
'not_found' => 'The row was not found. Refresh the page.',
|
||||||
|
'task_done' => 'The task is already done and cannot be moved to a machine.',
|
||||||
|
],
|
||||||
|
];
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'types' => [
|
||||||
|
'printer' => 'Painokone / tulostin',
|
||||||
|
'cutter' => 'Tarraleikkuri',
|
||||||
|
'other' => 'Muu',
|
||||||
|
],
|
||||||
|
'counter_units' => [
|
||||||
|
'piece' => 'kpl',
|
||||||
|
'sheet' => 'arkki',
|
||||||
|
'square_metre' => 'm²',
|
||||||
|
'millilitre' => 'ml',
|
||||||
|
'metre' => 'm',
|
||||||
|
'hour' => 'h',
|
||||||
|
],
|
||||||
|
'counter_names' => [
|
||||||
|
'prints' => 'Tulosteet',
|
||||||
|
'printed_area' => 'Tulostettu pinta-ala',
|
||||||
|
'cut_length' => 'Leikattu matka',
|
||||||
|
],
|
||||||
|
|
||||||
|
'index' => [
|
||||||
|
'title' => 'Koneet',
|
||||||
|
'intro' => 'Koneet palveluyksiköittäin. Koneen sivulla on sen päiväkohtainen työlista; konekäyttöliittymä on tarkoitettu koneen vieressä olevalle tabletille.',
|
||||||
|
'new' => 'Uusi kone',
|
||||||
|
'unit' => 'Palveluyksikkö',
|
||||||
|
'all_units' => 'Kaikki palveluyksiköt',
|
||||||
|
'include_inactive' => 'Näytä myös käytöstä poistetut',
|
||||||
|
'none' => 'Ei koneita.',
|
||||||
|
'inactive' => 'Poistettu käytöstä',
|
||||||
|
'responsible' => 'Vastuuhenkilö: :name',
|
||||||
|
'open' => 'Työlista ja tiedot',
|
||||||
|
'open_named' => 'Koneen :name työlista ja tiedot',
|
||||||
|
],
|
||||||
|
|
||||||
|
'form' => [
|
||||||
|
'create_title' => 'Uusi kone',
|
||||||
|
'edit_title' => 'Muokkaa konetta',
|
||||||
|
'name' => 'Nimi',
|
||||||
|
'type' => 'Konetyyppi',
|
||||||
|
'description' => 'Kuvaus',
|
||||||
|
'service_units' => 'Palveluyksiköt',
|
||||||
|
'service_units_hint' => 'Vain palveluyksiköt, joissa varaston ja koneiden käyttö on päällä. Yhteiskäyttöinen kone kuuluu useaan yksikköön.',
|
||||||
|
'no_stock_units' => 'Yhdelläkään palveluyksiköllä ei ole varaston ja koneiden käyttöä. Ota se käyttöön Pohjatiedoissa.',
|
||||||
|
'responsible' => 'Vastuuhenkilö',
|
||||||
|
'responsible_hint' => 'Saa ilmoitukset lähestyvistä ja myöhässä olevista huolloista.',
|
||||||
|
'no_responsible' => 'Ei vastuuhenkilöä',
|
||||||
|
'counters' => 'Laskurit',
|
||||||
|
'counters_hint' => 'Laskureilla mitataan huollon tarvetta, esim. tulosteet tai tulostettu pinta-ala. Lukema kirjataan konekäyttöliittymästä.',
|
||||||
|
'counter_name' => 'Laskurin nimi',
|
||||||
|
'counter_unit' => 'Yksikkö',
|
||||||
|
'counter_named' => 'Laskuri :number',
|
||||||
|
'add_counter' => 'Lisää laskuri',
|
||||||
|
'remove_counter_named' => 'Poista laskuri :number',
|
||||||
|
'suggest_counters' => 'Lisää konetyypin tavalliset laskurit',
|
||||||
|
'counter_added' => 'Laskuri :number lisätty.',
|
||||||
|
'counter_removed' => 'Laskuri :number poistettu.',
|
||||||
|
'counters_suggested' => '{0} Konetyypille ei ole ehdotettuja laskureita.|{1} Lisättiin 1 laskuri.|[2,*] Lisättiin :count laskuria.',
|
||||||
|
'save' => 'Tallenna',
|
||||||
|
'cancel' => 'Peruuta',
|
||||||
|
'created' => 'Kone :name lisätty.',
|
||||||
|
'updated' => 'Kone :name tallennettu.',
|
||||||
|
'deactivate' => 'Poista käytöstä',
|
||||||
|
'activate' => 'Ota käyttöön',
|
||||||
|
'deactivated' => 'Kone poistettu käytöstä. Sitä ei näytetä valinnoissa, mutta sen historia säilyy.',
|
||||||
|
'activated' => 'Kone otettu käyttöön.',
|
||||||
|
],
|
||||||
|
|
||||||
|
'show' => [
|
||||||
|
'details' => 'Tiedot',
|
||||||
|
'edit' => 'Muokkaa',
|
||||||
|
'console' => 'Avaa konekäyttöliittymä',
|
||||||
|
'units' => 'Palveluyksiköt: :units',
|
||||||
|
'work_list' => 'Työlista',
|
||||||
|
'day_navigation' => 'Päivän valinta',
|
||||||
|
'previous_day' => 'Edellinen päivä',
|
||||||
|
'today' => 'Tänään',
|
||||||
|
'next_day' => 'Seuraava päivä',
|
||||||
|
'day_field' => 'Päivä',
|
||||||
|
'show_day' => 'Näytä päivä',
|
||||||
|
'day_heading' => 'Työlista :date',
|
||||||
|
'load' => 'Tulevat päivät',
|
||||||
|
'load_row' => ':date: :rows, arvio :hours h',
|
||||||
|
'rows' => '{0} ei rivejä|{1} 1 rivi|[2,*] :count riviä',
|
||||||
|
'none' => 'Ei töitä tälle päivälle.',
|
||||||
|
'row_count' => '{0} Ei rivejä.|{1} 1 rivi.|[2,*] :count riviä.',
|
||||||
|
'late' => 'Myöhässä (ajoitettu :date)',
|
||||||
|
'order' => 'Asiakas odottaa',
|
||||||
|
'outside_plan' => 'Päivä on tehtävän aikataulun ulkopuolella tai määräpäivän jälkeen.',
|
||||||
|
'due' => 'Määräpäivä :date',
|
||||||
|
'priority' => 'Prioriteetti: :priority',
|
||||||
|
'status' => 'Tila: :status',
|
||||||
|
'instructions' => 'Työn ohjeistus',
|
||||||
|
'description' => 'Tehtävän kuvaus',
|
||||||
|
'up_named' => 'Siirrä ylös: :title',
|
||||||
|
'down_named' => 'Siirrä alas: :title',
|
||||||
|
'moved_up' => ':title siirretty ylöspäin.',
|
||||||
|
'moved_down' => ':title siirretty alaspäin.',
|
||||||
|
'first' => ':title on jo ensimmäisenä.',
|
||||||
|
'last' => ':title on jo viimeisenä.',
|
||||||
|
'open_task' => 'Avaa tehtävä työn sivulla',
|
||||||
|
'reschedule' => 'Vaihda päivä tai kone',
|
||||||
|
'reschedule_named' => 'Vaihda päivä tai kone: :title',
|
||||||
|
'back' => 'Takaisin koneisiin',
|
||||||
|
],
|
||||||
|
|
||||||
|
'task' => [
|
||||||
|
'title' => 'Kone',
|
||||||
|
'list_of' => 'Kone: :title',
|
||||||
|
'not_on_machine' => 'Tehtävä ei ole minkään koneen työlistalla.',
|
||||||
|
'on_machine' => 'Koneen :machine työlistalla :date.',
|
||||||
|
'late' => 'Myöhässä: ajoitettu päivä on mennyt.',
|
||||||
|
'move' => 'Siirrä koneelle',
|
||||||
|
'change' => 'Vaihda kone tai päivä',
|
||||||
|
'machine' => 'Kone',
|
||||||
|
'choose_machine' => 'Valitse kone',
|
||||||
|
'day' => 'Valmistuspäivä',
|
||||||
|
'day_hint' => 'Oletuksena tehtävän aikataulun alku, sen puuttuessa tänään.',
|
||||||
|
'save' => 'Tallenna',
|
||||||
|
'cancel' => 'Peruuta',
|
||||||
|
'remove' => 'Poista koneelta',
|
||||||
|
'moved' => 'Tehtävä siirretty koneen :machine työlistalle :date.',
|
||||||
|
'removed' => 'Tehtävä poistettu koneen työlistalta.',
|
||||||
|
'no_machines' => 'Tehtävän palveluyksikössä ei ole käytössä olevia koneita.',
|
||||||
|
],
|
||||||
|
|
||||||
|
'errors' => [
|
||||||
|
'name' => 'Anna nimi (1–150 merkkiä).',
|
||||||
|
'name_taken' => 'Nimi on jo käytössä.',
|
||||||
|
'service_units' => 'Valitse vähintään yksi palveluyksikkö.',
|
||||||
|
'counter' => 'Anna laskurille nimi (1–100 merkkiä) ja yksikkö.',
|
||||||
|
'machine' => 'Valitse käytössä oleva kone tehtävän palveluyksiköstä.',
|
||||||
|
'day' => 'Anna päivä muodossa pp.kk.vvvv.',
|
||||||
|
'not_found' => 'Riviä ei löytynyt. Päivitä sivu.',
|
||||||
|
'task_done' => 'Tehtävä on jo valmis, eikä sitä voi siirtää koneelle.',
|
||||||
|
],
|
||||||
|
];
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
||||||
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||||
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||||
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||||
|
use App\Modules\Machines\Application\UseCases\MachineWorkList;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->user = User::factory()->management()->create(['name' => 'Johtaja']);
|
||||||
|
$this->actingAs($this->user);
|
||||||
|
|
||||||
|
$print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
|
||||||
|
$print->update(['uses_stock' => true]);
|
||||||
|
$this->machine = MachineModel::factory()->inUnits([$print->id])->create(['name' => 'Suurkuvatulostin']);
|
||||||
|
MachineModel::factory()->inUnits([$print->id])->create(['name' => 'Tarraleikkuri', 'type' => 'cutter']);
|
||||||
|
|
||||||
|
$this->job = CustomerJobModel::factory()->create(['name' => 'Messut', 'status' => 'order', 'instructions' => 'Kiiltävä laminointi']);
|
||||||
|
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $print->id]);
|
||||||
|
$this->task = TaskModel::factory()->create([
|
||||||
|
'customer_job_id' => $this->job->id,
|
||||||
|
'work_type_id' => WorkTypeModel::query()->where('name', 'painotyö')->value('id'),
|
||||||
|
'title' => 'Banderolli',
|
||||||
|
'description' => 'Kaksi kappaletta',
|
||||||
|
]);
|
||||||
|
app(MachineWorkList::class)->planTask($this->task->id, $this->machine->id, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('has no accessibility issues on the machine pages', function (string $page, string $device, string $colorScheme) {
|
||||||
|
$url = match ($page) {
|
||||||
|
'index' => route('machines.index'),
|
||||||
|
'show' => route('machines.show', $this->machine->id),
|
||||||
|
'form' => route('machines.edit', $this->machine->id),
|
||||||
|
'job' => route('jobs.show', $this->job->id).'#task-'.$this->task->id,
|
||||||
|
};
|
||||||
|
|
||||||
|
$visit = visit($url);
|
||||||
|
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||||
|
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||||
|
|
||||||
|
$visit->assertSee($page === 'form' ? __('machines.form.edit_title') : 'Suurkuvatulostin')
|
||||||
|
->assertNoJavaScriptErrors()
|
||||||
|
->assertNoAccessibilityIssues(level: 2);
|
||||||
|
})->with(['index', 'show', 'form', 'job'])->with([
|
||||||
|
'desktop, light' => ['desktop', 'light'],
|
||||||
|
'desktop, dark' => ['desktop', 'dark'],
|
||||||
|
'mobile, light' => ['mobile', 'light'],
|
||||||
|
'mobile, dark' => ['mobile', 'dark'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('moves a task to another machine with the keyboard from the work list', function () {
|
||||||
|
visit(route('machines.show', $this->machine->id))
|
||||||
|
->assertSee('Banderolli')
|
||||||
|
->click(__('machines.show.reschedule'))
|
||||||
|
->assertNoAccessibilityIssues(level: 2)
|
||||||
|
->select('#edit-machine', 'Tarraleikkuri')
|
||||||
|
->click(__('machines.task.save'))
|
||||||
|
->assertSee('Tehtävä siirretty koneen Tarraleikkuri työlistalle')
|
||||||
|
->assertDontSee('Kiiltävä laminointi');
|
||||||
|
});
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
||||||
|
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||||
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||||
|
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||||
|
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->travelTo(now()->setTimezone(config('app.business_timezone'))->setTime(10, 0));
|
||||||
|
$this->today = now(config('app.business_timezone'))->toDateString();
|
||||||
|
$this->manager = User::factory()->management()->create(['name' => 'Maija Johto']);
|
||||||
|
$this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']);
|
||||||
|
|
||||||
|
$this->print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
|
||||||
|
$this->print->update(['uses_stock' => true]);
|
||||||
|
$this->printing = WorkTypeModel::query()->where('name', 'painotyö')->sole();
|
||||||
|
|
||||||
|
$this->printer = MachineModel::factory()->inUnits([$this->print->id])->create(['name' => 'Suurkuvatulostin']);
|
||||||
|
$this->cutter = MachineModel::factory()->inUnits([$this->print->id])->create(['name' => 'Tarraleikkuri', 'type' => 'cutter']);
|
||||||
|
|
||||||
|
$this->job = CustomerJobModel::factory()->create(['name' => 'Messut', 'status' => 'order', 'priority' => 'normal', 'instructions' => 'Kiiltävä laminointi']);
|
||||||
|
DB::table('customer_job_service_unit')->where('customer_job_id', $this->job->id)->update(['service_unit_id' => $this->print->id]);
|
||||||
|
$this->urgentJob = CustomerJobModel::factory()->create(['name' => 'Kiire', 'status' => 'order', 'priority' => 'urgent']);
|
||||||
|
DB::table('customer_job_service_unit')->where('customer_job_id', $this->urgentJob->id)->update(['service_unit_id' => $this->print->id]);
|
||||||
|
|
||||||
|
$this->banner = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->printing->id, 'title' => 'Banderolli']);
|
||||||
|
$this->poster = TaskModel::factory()->create(['customer_job_id' => $this->urgentJob->id, 'work_type_id' => $this->printing->id, 'title' => 'Juliste']);
|
||||||
|
});
|
||||||
|
|
||||||
|
function taskMachine(object $test, TaskModel $task): mixed
|
||||||
|
{
|
||||||
|
return Livewire::test('machines::task-machine', ['jobId' => $task->customer_job_id, 'taskId' => $task->id, 'taskTitle' => $task->title]);
|
||||||
|
}
|
||||||
|
|
||||||
|
it('adds a machine with the suggested counters of its type in units that use stock', function () {
|
||||||
|
$this->actingAs($this->manager);
|
||||||
|
$video = ServiceUnitModel::query()->where('name', 'Mediatuotanto')->sole();
|
||||||
|
|
||||||
|
Livewire::test('machines::form')
|
||||||
|
->assertSet('counters', fn (array $counters) => array_column($counters, 'name') === ['Tulosteet', 'Tulostettu pinta-ala'])
|
||||||
|
->set('name', 'Valokuvatulostin')
|
||||||
|
->set('serviceUnitIds', [(string) $this->print->id, (string) $video->id])
|
||||||
|
->call('save')
|
||||||
|
->assertHasNoErrors();
|
||||||
|
|
||||||
|
$machine = MachineModel::query()->where('name', 'Valokuvatulostin')->sole();
|
||||||
|
|
||||||
|
expect(DB::table('machine_service_units')->where('machine_id', $machine->id)->pluck('service_unit_id')->all())->toBe([$this->print->id])
|
||||||
|
->and(MachineCounterModel::query()->where('machine_id', $machine->id)->pluck('unit')->map->value->all())->toBe(['piece', 'square_metre']);
|
||||||
|
|
||||||
|
Livewire::test('machines::form')->set('name', 'Toinen')->set('serviceUnitIds', [])->call('save')->assertHasErrors('serviceUnitIds');
|
||||||
|
Livewire::test('machines::form')->set('name', 'suurkuvatulostin')->set('serviceUnitIds', [(string) $this->print->id])->call('save')->assertHasErrors('name');
|
||||||
|
|
||||||
|
$this->actingAs($this->employee);
|
||||||
|
$this->get(route('machines.create'))->assertForbidden();
|
||||||
|
$this->get(route('machines.index'))->assertOk()->assertSee('Suurkuvatulostin');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('moves a task to a machine for a day and orders the day by priority', function () {
|
||||||
|
$this->actingAs($this->employee);
|
||||||
|
|
||||||
|
taskMachine($this, $this->banner)
|
||||||
|
->assertSee(__('machines.task.not_on_machine'))
|
||||||
|
->call('startEdit')
|
||||||
|
->assertSet('day', $this->today)
|
||||||
|
->set('machineId', (string) $this->printer->id)
|
||||||
|
->call('save')
|
||||||
|
->assertHasNoErrors()
|
||||||
|
->assertSee('Suurkuvatulostin');
|
||||||
|
|
||||||
|
taskMachine($this, $this->poster)->call('startEdit')->set('machineId', (string) $this->printer->id)->call('save');
|
||||||
|
|
||||||
|
Livewire::test('machines::show', ['machine' => $this->printer->id])
|
||||||
|
->assertSeeInOrder(['Juliste', 'Banderolli'])
|
||||||
|
->assertSee('Kiiltävä laminointi')
|
||||||
|
->assertSee('2 riviä.');
|
||||||
|
|
||||||
|
$banner = MachineQueueEntryModel::query()->where('task_id', $this->banner->id)->sole();
|
||||||
|
|
||||||
|
Livewire::test('machines::show', ['machine' => $this->printer->id])
|
||||||
|
->call('move', $banner->id, 'up')
|
||||||
|
->assertSet('message', 'Banderolli siirretty ylöspäin.')
|
||||||
|
->assertSeeInOrder(['Banderolli', 'Juliste'])
|
||||||
|
->call('move', $banner->id, 'up')
|
||||||
|
->assertSet('message', 'Banderolli on jo ensimmäisenä.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('moves a row to another day and machine, shows it late afterwards, and takes it off', function () {
|
||||||
|
$this->actingAs($this->employee);
|
||||||
|
$tomorrow = now(config('app.business_timezone'))->addDay()->toDateString();
|
||||||
|
|
||||||
|
taskMachine($this, $this->banner)->call('startEdit')->set('machineId', (string) $this->printer->id)->set('day', $tomorrow)->call('save');
|
||||||
|
|
||||||
|
Livewire::test('machines::show', ['machine' => $this->printer->id])
|
||||||
|
->assertDontSee('Banderolli')
|
||||||
|
->call('showDay', 'next')
|
||||||
|
->assertSee('Banderolli');
|
||||||
|
|
||||||
|
$entry = MachineQueueEntryModel::query()->sole();
|
||||||
|
$this->travel(2)->days();
|
||||||
|
|
||||||
|
Livewire::test('machines::show', ['machine' => $this->printer->id])
|
||||||
|
->assertSee('Banderolli')
|
||||||
|
->assertSee('Myöhässä (ajoitettu '.now()->subDay()->format('j.n.').')')
|
||||||
|
->call('startEdit', $entry->id)
|
||||||
|
->set('editMachineId', (string) $this->cutter->id)
|
||||||
|
->call('reschedule')
|
||||||
|
->assertHasNoErrors();
|
||||||
|
|
||||||
|
expect($entry->fresh()->machine_id)->toBe($this->cutter->id)
|
||||||
|
->and(substr($entry->fresh()->planned_on, 0, 10))->toBe(now(config('app.business_timezone'))->toDateString());
|
||||||
|
|
||||||
|
Livewire::test('machines::show', ['machine' => $this->cutter->id])->call('remove', $entry->id)->assertSee(__('machines.task.removed'));
|
||||||
|
|
||||||
|
expect(MachineQueueEntryModel::query()->count())->toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('takes a task that is marked done off the work list', function () {
|
||||||
|
$this->actingAs($this->employee);
|
||||||
|
taskMachine($this, $this->banner)->call('startEdit')->set('machineId', (string) $this->printer->id)->call('save');
|
||||||
|
|
||||||
|
Livewire::test('jobs::show', ['job' => $this->job->id])->call('changeTaskStatus', $this->banner->id, 'done');
|
||||||
|
|
||||||
|
expect(MachineQueueEntryModel::query()->sole()->completed_at)->not->toBeNull();
|
||||||
|
Livewire::test('machines::show', ['machine' => $this->printer->id])->assertDontSee('Banderolli');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not offer machines for tasks of units without stock', function () {
|
||||||
|
$this->actingAs($this->employee);
|
||||||
|
$graphic = WorkTypeModel::query()->where('name', 'graafinen työ')->sole();
|
||||||
|
$task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $graphic->id, 'title' => 'Taitto']);
|
||||||
|
|
||||||
|
taskMachine($this, $task)->assertDontSee(__('machines.task.title'))->call('startEdit')->assertForbidden();
|
||||||
|
});
|
||||||
@@ -70,7 +70,7 @@ beforeEach(function () {
|
|||||||
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
|
$this->updateTask = new UpdateTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
|
||||||
$this->scheduleTask = new ScheduleTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
|
$this->scheduleTask = new ScheduleTask($this->jobs, $this->tasks, $checker, $this->planner, $transactions, $this->events);
|
||||||
$this->clearSchedule = new ClearTaskSchedule($this->tasks, $this->planner, $transactions);
|
$this->clearSchedule = new ClearTaskSchedule($this->tasks, $this->planner, $transactions);
|
||||||
$this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions);
|
$this->changeTaskStatus = new ChangeTaskStatus($this->tasks, $transactions, $this->events);
|
||||||
$this->usage = new FakeTaskUsage;
|
$this->usage = new FakeTaskUsage;
|
||||||
$this->removeTask = new RemoveTask($this->tasks, $this->usage, $transactions, new InMemoryTaskImageRepository, new FakeTaskImageFiles);
|
$this->removeTask = new RemoveTask($this->tasks, $this->usage, $transactions, new InMemoryTaskImageRepository, new FakeTaskImageFiles);
|
||||||
$this->changeJobStatus = new ChangeJobStatus($this->jobs, $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')));
|
$this->changeJobStatus = new ChangeJobStatus($this->jobs, $transactions, new FakeClock(new DateTimeImmutable('2026-05-01')));
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Modules\Machines\Domain\Enums\MachineType;
|
||||||
|
use App\Modules\Machines\Domain\Services\QueueOrder;
|
||||||
|
|
||||||
|
it('puts a task after quick sale orders, by priority and then due date', function () {
|
||||||
|
$rows = [
|
||||||
|
['isOrder' => true, 'rank' => 0, 'dueOn' => null],
|
||||||
|
['isOrder' => false, 'rank' => 3, 'dueOn' => '2026-12-01'],
|
||||||
|
['isOrder' => false, 'rank' => 1, 'dueOn' => '2026-11-30'],
|
||||||
|
['isOrder' => false, 'rank' => 1, 'dueOn' => null],
|
||||||
|
];
|
||||||
|
|
||||||
|
expect(QueueOrder::insertionIndex($rows, 4, null))->toBe(1)
|
||||||
|
->and(QueueOrder::insertionIndex($rows, 1, '2026-11-28'))->toBe(2)
|
||||||
|
->and(QueueOrder::insertionIndex($rows, 1, '2026-12-05'))->toBe(3)
|
||||||
|
->and(QueueOrder::insertionIndex($rows, 1, null))->toBe(4)
|
||||||
|
->and(QueueOrder::insertionIndex([], 1, null))->toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('suggests the usual counters by machine type', function () {
|
||||||
|
expect(array_column(MachineType::Printer->suggestedCounters(), 'name'))->toBe(['prints', 'printed_area'])
|
||||||
|
->and(array_column(MachineType::Cutter->suggestedCounters(), 'name'))->toBe(['cut_length'])
|
||||||
|
->and(MachineType::Other->suggestedCounters())->toBe([]);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user