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]>
This commit is contained in:
2026-11-29 09:58:11 +02:00
co-authored by Claude Sonnet 5.5
parent 87e38260dc
commit 32c33e2a64
55 changed files with 1660 additions and 194 deletions
@@ -24,7 +24,7 @@ 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
* management; counter usage 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.
@@ -98,50 +98,52 @@ final class MachineMaintenance
}
/**
* Records a counter's reading, e.g. the machine's own total counter.
* Records how much of a counter one job used (spec 19.5): counters are
* cumulative, so the usage is added to the counter's latest reading.
*
* @return int The counter's new reading.
*
* @throws CounterNotFound
* @throws InvalidReading
*/
public function recordReading(int $counterId, string $reading, int $actorUserId): int
public function recordUsage(int $counterId, string $usage, int $actorUserId): int
{
$milli = Reading::parse($reading) ?? throw new InvalidReading;
$usageMilli = Reading::parse($usage);
$this->transactions->run(function () use ($counterId, $milli, $actorUserId): void {
if ($usageMilli === null || $usageMilli <= 0) {
throw new InvalidReading;
}
return $this->transactions->run(function () use ($counterId, $usageMilli, $actorUserId): int {
$this->maintenance->machineOfCounter($counterId) ?? throw new CounterNotFound;
$this->maintenance->addReading($counterId, $milli, $actorUserId, $this->clock->now());
});
$reading = ($this->maintenance->latestReading($counterId) ?? 0) + $usageMilli;
$this->maintenance->addReading($counterId, $reading, $actorUserId, $this->clock->now());
return $milli;
return $reading;
});
}
/**
* Records maintenance done (Huolto tehty) with the counter's reading
* (the latest one when not given) and uses up the item's stock item.
* Records maintenance done (Huolto tehty) at the counter's latest
* reading and uses up the item's stock item. This resets the item: the
* whole counter interval is left again, counted from this reading, and
* the time interval starts again from today.
*
* @return array{recordId: int, stockRecorded: bool}
*
* @throws MaintenanceItemNotFound
* @throws InvalidReading
*/
public function recordMaintenance(int $itemId, int $actorUserId, string $reading = '', ?string $note = null): array
public function recordMaintenance(int $itemId, int $actorUserId, ?string $note = null): array
{
$readingMilli = Reading::parse($reading);
$note = $note === null ? '' : trim($note);
return $this->transactions->run(function () use ($itemId, $actorUserId, $readingMilli, $note): array {
return $this->transactions->run(function () use ($itemId, $actorUserId, $note): array {
$item = $this->maintenance->findItem($itemId) ?? throw new MaintenanceItemNotFound;
$now = $this->clock->now();
$readingMilli = $item->counterId() === null
? null
: ($this->maintenance->latestReading($item->counterId()) ?? $item->baselineMilli());
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));
$recordId = $this->maintenance->addRecord($itemId, $this->clock->now(), $actorUserId, $readingMilli, $note === '' ? null : mb_substr($note, 0, 2000));
$stockRecorded = $item->stockItemId() === null
|| $this->materials->use($item->stockItemId(), (int) $item->stockQuantityMilli(), $item->machineId(), $recordId, $actorUserId);