The console under console/ has its own session cookie and console guard (UseConsoleSession), so signing out or changing user there leaves the normal interface alone. Links open it in a new window through a one-time handoff key; a signed-out console asks for a PIN code or password on its own login page. Users set their 4-6 digit PIN on the profile's PIN page, and the console's change user dialog takes it on a keypad; five wrong PINs lock PIN sign-in for 15 minutes. Machine counters are cumulative: users record how much a job used and it is added to the latest reading. A maintenance record restarts the whole counter interval from the reading at the record. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
172 lines
7.1 KiB
PHP
172 lines
7.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Machines\Application\UseCases;
|
|
|
|
use App\Modules\Machines\Application\Contracts\MaintenanceMaterials;
|
|
use App\Modules\Machines\Application\DTOs\MaintenanceItemData;
|
|
use App\Modules\Machines\Domain\Contracts\MachineRepository;
|
|
use App\Modules\Machines\Domain\Contracts\MaintenanceRepository;
|
|
use App\Modules\Machines\Domain\Entities\MaintenanceItem;
|
|
use App\Modules\Machines\Domain\Enums\MaintenancePeriod;
|
|
use App\Modules\Machines\Domain\Exceptions\CounterNotFound;
|
|
use App\Modules\Machines\Domain\Exceptions\InvalidMaintenanceItem;
|
|
use App\Modules\Machines\Domain\Exceptions\InvalidReading;
|
|
use App\Modules\Machines\Domain\Exceptions\MachineNotFound;
|
|
use App\Modules\Machines\Domain\Exceptions\MaintenanceItemNeedsInterval;
|
|
use App\Modules\Machines\Domain\Exceptions\MaintenanceItemNotFound;
|
|
use App\Modules\Machines\Domain\Exceptions\MaintenanceRecordCancelled;
|
|
use App\Modules\Machines\Domain\Exceptions\MaintenanceRecordNotFound;
|
|
use App\Modules\Machines\Domain\ValueObjects\Reading;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
use App\Modules\Users\Application\Contracts\Clock;
|
|
|
|
/**
|
|
* Machine maintenance (spec 19.5): maintenance items are kept by
|
|
* management; counter usage and maintenance done are recorded by
|
|
* anyone using the machine. A maintenance record starts the item's
|
|
* intervals again and uses up its stock item; a mistaken record is
|
|
* cancelled by management, never deleted.
|
|
*/
|
|
final class MachineMaintenance
|
|
{
|
|
public function __construct(
|
|
private MachineRepository $machines,
|
|
private MaintenanceRepository $maintenance,
|
|
private MaintenanceMaterials $materials,
|
|
private TransactionManager $transactions,
|
|
private Clock $clock,
|
|
) {}
|
|
|
|
/**
|
|
* @return int The item's id.
|
|
*
|
|
* @throws MachineNotFound
|
|
* @throws MaintenanceItemNotFound
|
|
* @throws CounterNotFound
|
|
* @throws InvalidReading
|
|
* @throws InvalidMaintenanceItem
|
|
* @throws MaintenanceItemNeedsInterval
|
|
*/
|
|
public function saveItem(int $machineId, ?int $itemId, MaintenanceItemData $data): int
|
|
{
|
|
$interval = Reading::parse($data->interval);
|
|
$quantity = $data->stockItemId === null ? null : Reading::parse($data->stockQuantity);
|
|
$period = $data->period === null || $data->period === '' ? null : (MaintenancePeriod::tryFrom($data->period) ?? throw new InvalidMaintenanceItem);
|
|
|
|
return $this->transactions->run(function () use ($machineId, $itemId, $data, $interval, $quantity, $period): int {
|
|
$this->machines->findById($machineId) ?? throw new MachineNotFound;
|
|
|
|
if ($data->counterId !== null && $this->maintenance->machineOfCounter($data->counterId) !== $machineId) {
|
|
throw new CounterNotFound;
|
|
}
|
|
|
|
$counterReading = $data->counterId === null ? 0 : ($this->maintenance->latestReading($data->counterId) ?? 0);
|
|
|
|
if ($itemId === null) {
|
|
$item = MaintenanceItem::create($machineId, $data->name, $data->counterId, $interval, $counterReading, $period, $data->periodCount, $this->clock->today(), $data->warningPercent, $data->instructions, $data->stockItemId, $quantity);
|
|
} else {
|
|
$item = $this->maintenance->findItem($itemId);
|
|
|
|
if ($item === null || $item->machineId() !== $machineId) {
|
|
throw new MaintenanceItemNotFound;
|
|
}
|
|
|
|
$counterChanged = $item->counterId() !== $data->counterId;
|
|
$item->change($data->name, $data->counterId, $interval, $period, $data->periodCount, $data->warningPercent, $data->instructions, $data->stockItemId, $quantity);
|
|
|
|
if ($counterChanged) {
|
|
$item->rebaseCounter($counterReading);
|
|
}
|
|
}
|
|
|
|
return (int) $this->maintenance->saveItem($item)->id();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @throws MaintenanceItemNotFound
|
|
*/
|
|
public function setItemActive(int $itemId, bool $active): void
|
|
{
|
|
$this->transactions->run(function () use ($itemId, $active): void {
|
|
$item = $this->maintenance->findItem($itemId) ?? throw new MaintenanceItemNotFound;
|
|
$active ? $item->activate() : $item->deactivate();
|
|
$this->maintenance->saveItem($item);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Records how much of a counter one job used (spec 19.5): counters are
|
|
* cumulative, so the usage is added to the counter's latest reading.
|
|
*
|
|
* @return int The counter's new reading.
|
|
*
|
|
* @throws CounterNotFound
|
|
* @throws InvalidReading
|
|
*/
|
|
public function recordUsage(int $counterId, string $usage, int $actorUserId): int
|
|
{
|
|
$usageMilli = Reading::parse($usage);
|
|
|
|
if ($usageMilli === null || $usageMilli <= 0) {
|
|
throw new InvalidReading;
|
|
}
|
|
|
|
return $this->transactions->run(function () use ($counterId, $usageMilli, $actorUserId): int {
|
|
$this->maintenance->machineOfCounter($counterId) ?? throw new CounterNotFound;
|
|
$reading = ($this->maintenance->latestReading($counterId) ?? 0) + $usageMilli;
|
|
$this->maintenance->addReading($counterId, $reading, $actorUserId, $this->clock->now());
|
|
|
|
return $reading;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Records maintenance done (Huolto tehty) at the counter's latest
|
|
* reading and uses up the item's stock item. This resets the item: the
|
|
* whole counter interval is left again, counted from this reading, and
|
|
* the time interval starts again from today.
|
|
*
|
|
* @return array{recordId: int, stockRecorded: bool}
|
|
*
|
|
* @throws MaintenanceItemNotFound
|
|
*/
|
|
public function recordMaintenance(int $itemId, int $actorUserId, ?string $note = null): array
|
|
{
|
|
$note = $note === null ? '' : trim($note);
|
|
|
|
return $this->transactions->run(function () use ($itemId, $actorUserId, $note): array {
|
|
$item = $this->maintenance->findItem($itemId) ?? throw new MaintenanceItemNotFound;
|
|
$readingMilli = $item->counterId() === null
|
|
? null
|
|
: ($this->maintenance->latestReading($item->counterId()) ?? $item->baselineMilli());
|
|
|
|
$recordId = $this->maintenance->addRecord($itemId, $this->clock->now(), $actorUserId, $readingMilli, $note === '' ? null : mb_substr($note, 0, 2000));
|
|
|
|
$stockRecorded = $item->stockItemId() === null
|
|
|| $this->materials->use($item->stockItemId(), (int) $item->stockQuantityMilli(), $item->machineId(), $recordId, $actorUserId);
|
|
|
|
return ['recordId' => $recordId, 'stockRecorded' => $stockRecorded];
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @throws MaintenanceRecordNotFound
|
|
* @throws MaintenanceRecordCancelled
|
|
*/
|
|
public function cancelRecord(int $recordId, int $actorUserId): void
|
|
{
|
|
$this->transactions->run(function () use ($recordId, $actorUserId): void {
|
|
$record = $this->maintenance->findRecord($recordId) ?? throw new MaintenanceRecordNotFound;
|
|
|
|
if ($record['cancelled']) {
|
|
throw new MaintenanceRecordCancelled;
|
|
}
|
|
|
|
$this->maintenance->cancelRecord($recordId, $actorUserId, $this->clock->now());
|
|
});
|
|
}
|
|
}
|