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,213 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Entities;
|
||||
|
||||
use App\Modules\Machines\Domain\Enums\MaintenancePeriod;
|
||||
use App\Modules\Machines\Domain\Exceptions\InvalidMaintenanceItem;
|
||||
use App\Modules\Machines\Domain\Exceptions\MaintenanceItemNeedsInterval;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* A maintenance item of a machine (huoltokohde, spec 19.5), e.g. emptying
|
||||
* the waste ink tank or changing the blade. It is due by a counter
|
||||
* interval (e.g. every 20 000 prints), a time interval (daily, weekly,
|
||||
* monthly or yearly maintenance, every N periods) or whichever comes
|
||||
* first; using a time interval is optional. A stock item, e.g. a spare
|
||||
* drum, may be used up at each maintenance. Items are taken out of use,
|
||||
* not deleted.
|
||||
*/
|
||||
final class MaintenanceItem
|
||||
{
|
||||
private function __construct(
|
||||
private ?int $id,
|
||||
private int $machineId,
|
||||
private string $name,
|
||||
private ?int $counterId,
|
||||
private ?int $intervalMilli,
|
||||
private int $baselineMilli,
|
||||
private ?MaintenancePeriod $period,
|
||||
private int $periodCount,
|
||||
private DateTimeImmutable $startsOn,
|
||||
private int $warningPercent,
|
||||
private ?string $instructions,
|
||||
private ?int $stockItemId,
|
||||
private ?int $stockQuantityMilli,
|
||||
private bool $active,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws InvalidMaintenanceItem
|
||||
* @throws MaintenanceItemNeedsInterval
|
||||
*/
|
||||
public static function create(
|
||||
int $machineId,
|
||||
string $name,
|
||||
?int $counterId,
|
||||
?int $intervalMilli,
|
||||
int $baselineMilli,
|
||||
?MaintenancePeriod $period,
|
||||
int $periodCount,
|
||||
DateTimeImmutable $startsOn,
|
||||
int $warningPercent,
|
||||
?string $instructions,
|
||||
?int $stockItemId,
|
||||
?int $stockQuantityMilli,
|
||||
): self {
|
||||
$item = new self(null, $machineId, '', null, null, $baselineMilli, null, 1, $startsOn, 90, null, null, null, true);
|
||||
$item->change($name, $counterId, $intervalMilli, $period, $periodCount, $warningPercent, $instructions, $stockItemId, $stockQuantityMilli);
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
public static function reconstitute(
|
||||
int $id,
|
||||
int $machineId,
|
||||
string $name,
|
||||
?int $counterId,
|
||||
?int $intervalMilli,
|
||||
int $baselineMilli,
|
||||
?MaintenancePeriod $period,
|
||||
int $periodCount,
|
||||
DateTimeImmutable $startsOn,
|
||||
int $warningPercent,
|
||||
?string $instructions,
|
||||
?int $stockItemId,
|
||||
?int $stockQuantityMilli,
|
||||
bool $active,
|
||||
): self {
|
||||
return new self($id, $machineId, $name, $counterId, $intervalMilli, $baselineMilli, $period, $periodCount, $startsOn, $warningPercent, $instructions, $stockItemId, $stockQuantityMilli, $active);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidMaintenanceItem
|
||||
* @throws MaintenanceItemNeedsInterval
|
||||
*/
|
||||
public function change(
|
||||
string $name,
|
||||
?int $counterId,
|
||||
?int $intervalMilli,
|
||||
?MaintenancePeriod $period,
|
||||
int $periodCount,
|
||||
int $warningPercent,
|
||||
?string $instructions,
|
||||
?int $stockItemId,
|
||||
?int $stockQuantityMilli,
|
||||
): void {
|
||||
$name = trim(preg_replace('/\s+/u', ' ', $name) ?? $name);
|
||||
$hasCounterInterval = $counterId !== null && $intervalMilli !== null;
|
||||
|
||||
if ($name === '' || mb_strlen($name) > 150
|
||||
|| ($intervalMilli !== null && $intervalMilli < 1)
|
||||
|| $periodCount < 1 || $periodCount > 365
|
||||
|| $warningPercent < 1 || $warningPercent > 99
|
||||
|| ($stockItemId !== null && ($stockQuantityMilli === null || $stockQuantityMilli < 1))) {
|
||||
throw new InvalidMaintenanceItem;
|
||||
}
|
||||
|
||||
if (! $hasCounterInterval && $period === null) {
|
||||
throw new MaintenanceItemNeedsInterval;
|
||||
}
|
||||
|
||||
$instructions = $instructions === null ? '' : trim($instructions);
|
||||
|
||||
$this->name = $name;
|
||||
$this->counterId = $hasCounterInterval ? $counterId : null;
|
||||
$this->intervalMilli = $hasCounterInterval ? $intervalMilli : null;
|
||||
$this->period = $period;
|
||||
$this->periodCount = $periodCount;
|
||||
$this->warningPercent = $warningPercent;
|
||||
$this->instructions = $instructions === '' ? null : $instructions;
|
||||
$this->stockItemId = $stockItemId;
|
||||
$this->stockQuantityMilli = $stockItemId === null ? null : $stockQuantityMilli;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the counter changes, the amount counts from its latest reading.
|
||||
*/
|
||||
public function rebaseCounter(int $readingMilli): void
|
||||
{
|
||||
$this->baselineMilli = $readingMilli;
|
||||
}
|
||||
|
||||
public function activate(): void
|
||||
{
|
||||
$this->active = true;
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->active = false;
|
||||
}
|
||||
|
||||
public function id(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function machineId(): int
|
||||
{
|
||||
return $this->machineId;
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function counterId(): ?int
|
||||
{
|
||||
return $this->counterId;
|
||||
}
|
||||
|
||||
public function intervalMilli(): ?int
|
||||
{
|
||||
return $this->intervalMilli;
|
||||
}
|
||||
|
||||
public function baselineMilli(): int
|
||||
{
|
||||
return $this->baselineMilli;
|
||||
}
|
||||
|
||||
public function period(): ?MaintenancePeriod
|
||||
{
|
||||
return $this->period;
|
||||
}
|
||||
|
||||
public function periodCount(): int
|
||||
{
|
||||
return $this->periodCount;
|
||||
}
|
||||
|
||||
public function startsOn(): DateTimeImmutable
|
||||
{
|
||||
return $this->startsOn;
|
||||
}
|
||||
|
||||
public function warningPercent(): int
|
||||
{
|
||||
return $this->warningPercent;
|
||||
}
|
||||
|
||||
public function instructions(): ?string
|
||||
{
|
||||
return $this->instructions;
|
||||
}
|
||||
|
||||
public function stockItemId(): ?int
|
||||
{
|
||||
return $this->stockItemId;
|
||||
}
|
||||
|
||||
public function stockQuantityMilli(): ?int
|
||||
{
|
||||
return $this->stockQuantityMilli;
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->active;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user