Track machine counters and maintenance with counter and time intervals

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:52:49 +02:00
co-authored by Claude Sonnet 5.5
parent aac579257a
commit 3e1c671949
53 changed files with 2636 additions and 6 deletions
@@ -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);
}
}