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,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Contracts;
|
||||
|
||||
use App\Modules\Machines\Domain\Entities\MaintenanceItem;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* Maintenance items, maintenance records and counter readings.
|
||||
*/
|
||||
interface MaintenanceRepository
|
||||
{
|
||||
public function findItem(int $id): ?MaintenanceItem;
|
||||
|
||||
public function saveItem(MaintenanceItem $item): MaintenanceItem;
|
||||
|
||||
/**
|
||||
* The machine a counter belongs to, or null when there is no such counter.
|
||||
*/
|
||||
public function machineOfCounter(int $counterId): ?int;
|
||||
|
||||
public function latestReading(int $counterId): ?int;
|
||||
|
||||
public function addReading(int $counterId, int $readingMilli, int $userId, DateTimeImmutable $at): void;
|
||||
|
||||
/**
|
||||
* @return int The record's id.
|
||||
*/
|
||||
public function addRecord(int $itemId, DateTimeImmutable $performedAt, int $userId, ?int $readingMilli, ?string $note): int;
|
||||
|
||||
/**
|
||||
* @return array{id: int, itemId: int, cancelled: bool}|null
|
||||
*/
|
||||
public function findRecord(int $id): ?array;
|
||||
|
||||
public function cancelRecord(int $id, int $userId, DateTimeImmutable $at): void;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Enums;
|
||||
|
||||
/**
|
||||
* The state of a maintenance item (spec 19.5): in order, maintenance
|
||||
* coming up (past the advance warning), or late (a maintenance request).
|
||||
*/
|
||||
enum MaintenanceLevel: string
|
||||
{
|
||||
case Ok = 'ok';
|
||||
case Approaching = 'approaching';
|
||||
case Overdue = 'overdue';
|
||||
|
||||
public function rank(): int
|
||||
{
|
||||
return match ($this) {
|
||||
self::Ok => 0,
|
||||
self::Approaching => 1,
|
||||
self::Overdue => 2,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Enums;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* A time-based maintenance interval (spec 19.5): daily, weekly, monthly or
|
||||
* yearly maintenance, possibly every N periods. Several items of a machine
|
||||
* can have different periods at the same time.
|
||||
*/
|
||||
enum MaintenancePeriod: string
|
||||
{
|
||||
case Day = 'day';
|
||||
case Week = 'week';
|
||||
case Month = 'month';
|
||||
case Year = 'year';
|
||||
|
||||
/**
|
||||
* The day the next maintenance is due after the given day. A month or
|
||||
* year from the 31st (or 29 February) ends on the last day of the month.
|
||||
*/
|
||||
public function next(DateTimeImmutable $from, int $count): DateTimeImmutable
|
||||
{
|
||||
$from = $from->setTime(0, 0);
|
||||
|
||||
return match ($this) {
|
||||
self::Day => $from->modify("+{$count} days"),
|
||||
self::Week => $from->modify('+'.(7 * $count).' days'),
|
||||
self::Month => self::addMonths($from, $count),
|
||||
self::Year => self::addMonths($from, 12 * $count),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The interval's length in days from the day, for the advance warning.
|
||||
*/
|
||||
public function days(DateTimeImmutable $from, int $count): int
|
||||
{
|
||||
return (int) $from->setTime(0, 0)->diff($this->next($from, $count))->days;
|
||||
}
|
||||
|
||||
private static function addMonths(DateTimeImmutable $from, int $months): DateTimeImmutable
|
||||
{
|
||||
$first = $from->modify('first day of this month')->modify("+{$months} months");
|
||||
$day = min((int) $from->format('j'), (int) $first->format('t'));
|
||||
|
||||
return $first->setDate((int) $first->format('Y'), (int) $first->format('n'), $day);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class CounterNotFound extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The counter 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 InvalidMaintenanceItem extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A maintenance item needs a name of 1-150 characters, positive intervals and a warning of 1-99 percent.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class InvalidReading extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A reading must be a number of zero or more with at most three decimals.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class MaintenanceItemNeedsInterval extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('A maintenance item needs a counter interval, a time interval or both.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class MaintenanceItemNotFound extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The maintenance item 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 MaintenanceRecordCancelled extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The maintenance record has already been cancelled.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Exceptions;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
|
||||
final class MaintenanceRecordNotFound extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The maintenance record was not found.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\Services;
|
||||
|
||||
use App\Modules\Machines\Domain\Enums\MaintenanceLevel;
|
||||
use App\Modules\Machines\Domain\Enums\MaintenancePeriod;
|
||||
use App\Modules\Machines\Domain\ValueObjects\MaintenanceState;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* Works out a maintenance item's state from what has accumulated since its
|
||||
* last maintenance (spec 19.5). The counter interval counts the counter's
|
||||
* latest reading minus its reading at the last maintenance; the time
|
||||
* interval counts calendar periods from the day of the last maintenance.
|
||||
* With both, maintenance is due when either is. The advance warning
|
||||
* applies to both, but not to daily maintenance.
|
||||
*/
|
||||
final class MaintenanceStatus
|
||||
{
|
||||
/**
|
||||
* @param int|null $intervalMilli The counter interval, null when none.
|
||||
* @param int $baselineMilli The counter's reading at the last maintenance (or when the item was added).
|
||||
* @param int|null $readingMilli The counter's latest reading.
|
||||
* @param DateTimeImmutable $since The day of the last maintenance (or when the item was added).
|
||||
*/
|
||||
public static function evaluate(
|
||||
?int $intervalMilli,
|
||||
int $baselineMilli,
|
||||
?int $readingMilli,
|
||||
?MaintenancePeriod $period,
|
||||
int $periodCount,
|
||||
DateTimeImmutable $since,
|
||||
int $warningPercent,
|
||||
DateTimeImmutable $today,
|
||||
): MaintenanceState {
|
||||
$level = MaintenanceLevel::Ok;
|
||||
$remaining = null;
|
||||
$dueOn = null;
|
||||
|
||||
if ($intervalMilli !== null) {
|
||||
$used = max(0, ($readingMilli ?? $baselineMilli) - $baselineMilli);
|
||||
$remaining = $intervalMilli - $used;
|
||||
$level = self::worse($level, match (true) {
|
||||
$used >= $intervalMilli => MaintenanceLevel::Overdue,
|
||||
$used * 100 >= $intervalMilli * $warningPercent => MaintenanceLevel::Approaching,
|
||||
default => MaintenanceLevel::Ok,
|
||||
});
|
||||
}
|
||||
|
||||
if ($period !== null) {
|
||||
$due = $period->next($since, $periodCount);
|
||||
$dueOn = $due->format('Y-m-d');
|
||||
$elapsed = $since->setTime(0, 0) <= $today ? (int) $since->setTime(0, 0)->diff($today->setTime(0, 0))->days : 0;
|
||||
$length = max(1, $period->days($since, $periodCount));
|
||||
$warns = ! ($period === MaintenancePeriod::Day && $periodCount === 1);
|
||||
|
||||
$level = self::worse($level, match (true) {
|
||||
$today->format('Y-m-d') >= $dueOn => MaintenanceLevel::Overdue,
|
||||
$warns && $elapsed * 100 >= $length * $warningPercent => MaintenanceLevel::Approaching,
|
||||
default => MaintenanceLevel::Ok,
|
||||
});
|
||||
}
|
||||
|
||||
return new MaintenanceState($level, $remaining, $dueOn);
|
||||
}
|
||||
|
||||
private static function worse(MaintenanceLevel $a, MaintenanceLevel $b): MaintenanceLevel
|
||||
{
|
||||
return $b->rank() > $a->rank() ? $b : $a;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Machines\Domain\Enums\MaintenanceLevel;
|
||||
|
||||
/**
|
||||
* How a maintenance item stands now: its level (the worse of its counter
|
||||
* and time intervals), the counter amount left (in thousandths, negative
|
||||
* when past) and the day the time interval is due (Y-m-d).
|
||||
*/
|
||||
final readonly class MaintenanceState
|
||||
{
|
||||
public function __construct(
|
||||
public MaintenanceLevel $level,
|
||||
public ?int $remainingMilli,
|
||||
public ?string $dueOn,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Machines\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Machines\Domain\Exceptions\InvalidReading;
|
||||
|
||||
/**
|
||||
* A counter reading or a counter amount in thousandths of the counter's
|
||||
* unit, typed as "20000", "20 000" or "12,5".
|
||||
*/
|
||||
final class Reading
|
||||
{
|
||||
/**
|
||||
* @return int|null Null for an empty value.
|
||||
*
|
||||
* @throws InvalidReading
|
||||
*/
|
||||
public static function parse(string $value): ?int
|
||||
{
|
||||
$value = str_replace(',', '.', preg_replace('/[\s\x{00A0}\x{202F}]+/u', '', $value) ?? '');
|
||||
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (preg_match('/^(\d{1,12})(?:\.(\d{1,3}))?$/', $value, $matches) !== 1) {
|
||||
throw new InvalidReading;
|
||||
}
|
||||
|
||||
return (int) $matches[1] * 1000 + (int) str_pad($matches[2] ?? '', 3, '0');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user