Track machine counters and maintenance with counter and time intervals
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Application\Contracts;
|
||||
|
||||
/**
|
||||
* Records the stock used at a maintenance through the Inventory module.
|
||||
*/
|
||||
interface MaintenanceMaterials
|
||||
{
|
||||
/**
|
||||
* @return bool False when the stock could not be recorded (e.g. the item is out of use).
|
||||
*/
|
||||
public function use(int $stockItemId, int $quantityMilli, int $machineId, int $maintenanceRecordId, int $userId): bool;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Application\Contracts;
|
||||
|
||||
use App\Modules\Machines\Application\DTOs\MaintenanceItemStatus;
|
||||
use App\Modules\Machines\Application\DTOs\MaintenanceRecordSummary;
|
||||
|
||||
interface MaintenanceQueries
|
||||
{
|
||||
/**
|
||||
* A machine's maintenance items with their states, late ones first.
|
||||
*
|
||||
* @return list<MaintenanceItemStatus>
|
||||
*/
|
||||
public function itemsOf(int $machineId, bool $includeInactive = false): array;
|
||||
|
||||
public function findItem(int $itemId): ?MaintenanceItemStatus;
|
||||
|
||||
/**
|
||||
* Items in use of machines in use whose maintenance is coming up or
|
||||
* late: the maintenance requests.
|
||||
*
|
||||
* @return list<MaintenanceItemStatus>
|
||||
*/
|
||||
public function due(): array;
|
||||
|
||||
/**
|
||||
* A machine's maintenance history, newest first, filtered by item and
|
||||
* by days (Y-m-d, both inclusive).
|
||||
*
|
||||
* @return list<MaintenanceRecordSummary>
|
||||
*/
|
||||
public function history(int $machineId, ?int $itemId = null, ?string $from = null, ?string $to = null, int $limit = 200): array;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A maintenance item as entered: the counter interval as typed with its
|
||||
* counter, the time interval as a period key (day, week, month, year) and
|
||||
* count, the advance warning in percent, and an optional stock item used
|
||||
* up at each maintenance with its quantity as typed.
|
||||
*/
|
||||
final readonly class MaintenanceItemData
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public ?int $counterId = null,
|
||||
public string $interval = '',
|
||||
public ?string $period = null,
|
||||
public int $periodCount = 1,
|
||||
public int $warningPercent = 90,
|
||||
public ?string $instructions = null,
|
||||
public ?int $stockItemId = null,
|
||||
public string $stockQuantity = '',
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Application\DTOs;
|
||||
|
||||
use App\Modules\Machines\Domain\Enums\CounterUnit;
|
||||
use App\Modules\Machines\Domain\Enums\MaintenanceLevel;
|
||||
use App\Modules\Machines\Domain\Enums\MaintenancePeriod;
|
||||
|
||||
/**
|
||||
* A maintenance item with its state now. Counter amounts are in thousandths
|
||||
* of the counter's unit; $remainingMilli is negative when past. Days are
|
||||
* Y-m-d; $lastPerformedAt is "Y-m-d H:i" in the company's time zone.
|
||||
*/
|
||||
final readonly class MaintenanceItemStatus
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $machineId,
|
||||
public string $machineName,
|
||||
public ?int $responsibleUserId,
|
||||
public string $name,
|
||||
public ?int $counterId,
|
||||
public ?string $counterName,
|
||||
public ?CounterUnit $counterUnit,
|
||||
public ?int $intervalMilli,
|
||||
public ?MaintenancePeriod $period,
|
||||
public int $periodCount,
|
||||
public int $warningPercent,
|
||||
public ?string $instructions,
|
||||
public ?int $stockItemId,
|
||||
public ?int $stockQuantityMilli,
|
||||
public bool $isActive,
|
||||
public MaintenanceLevel $level,
|
||||
public ?int $remainingMilli,
|
||||
public ?string $dueOn,
|
||||
public ?int $readingMilli,
|
||||
public ?int $lastRecordId,
|
||||
public ?string $lastPerformedAt,
|
||||
public ?string $lastPerformedByName,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A maintenance done, as in a machine's maintenance history. Times are
|
||||
* "Y-m-d H:i" in the company's time zone.
|
||||
*/
|
||||
final readonly class MaintenanceRecordSummary
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $itemId,
|
||||
public string $itemName,
|
||||
public string $performedAt,
|
||||
public string $userName,
|
||||
public ?int $readingMilli,
|
||||
public ?string $note,
|
||||
public ?string $cancelledAt,
|
||||
public ?string $cancelledByName,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?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());
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user