Files
Toiminnanohjaus/app/Modules/Machines/Application/UseCases/MachineMaintenance.php
T

170 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 readings 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 a counter's reading, e.g. the machine's own total counter.
*
* @throws CounterNotFound
* @throws InvalidReading
*/
public function recordReading(int $counterId, string $reading, int $actorUserId): int
{
$milli = Reading::parse($reading) ?? throw new InvalidReading;
$this->transactions->run(function () use ($counterId, $milli, $actorUserId): void {
$this->maintenance->machineOfCounter($counterId) ?? throw new CounterNotFound;
$this->maintenance->addReading($counterId, $milli, $actorUserId, $this->clock->now());
});
return $milli;
}
/**
* Records maintenance done (Huolto tehty) with the counter's reading
* (the latest one when not given) and uses up the item's stock item.
*
* @return array{recordId: int, stockRecorded: bool}
*
* @throws MaintenanceItemNotFound
* @throws InvalidReading
*/
public function recordMaintenance(int $itemId, int $actorUserId, string $reading = '', ?string $note = null): array
{
$readingMilli = Reading::parse($reading);
$note = $note === null ? '' : trim($note);
return $this->transactions->run(function () use ($itemId, $actorUserId, $readingMilli, $note): array {
$item = $this->maintenance->findItem($itemId) ?? throw new MaintenanceItemNotFound;
$now = $this->clock->now();
if ($item->counterId() !== null) {
if ($readingMilli !== null) {
$this->maintenance->addReading($item->counterId(), $readingMilli, $actorUserId, $now);
} else {
$readingMilli = $this->maintenance->latestReading($item->counterId()) ?? $item->baselineMilli();
}
}
$recordId = $this->maintenance->addRecord($itemId, $now, $actorUserId, $item->counterId() === null ? null : $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());
});
}
}