Files
petriandClaude Sonnet 5.5 32c33e2a64 Give the machine console its own session with PIN sign-in, make counters cumulative
The console under console/ has its own session cookie and console guard
(UseConsoleSession), so signing out or changing user there leaves the
normal interface alone. Links open it in a new window through a one-time
handoff key; a signed-out console asks for a PIN code or password on its
own login page. Users set their 4-6 digit PIN on the profile's PIN page,
and the console's change user dialog takes it on a keypad; five wrong
PINs lock PIN sign-in for 15 minutes.

Machine counters are cumulative: users record how much a job used and it
is added to the latest reading. A maintenance record restarts the whole
counter interval from the reading at the record.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-29 09:58:11 +02:00

75 lines
2.9 KiB
PHP

<?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, so a
* maintenance starts the whole interval again; 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;
}
}