*/ public array $usage = []; #[Locked] public ?int $recordingItemId = null; public string $recordNote = ''; public string $historyItem = ''; public string $historyFrom = ''; public string $historyTo = ''; #[Locked] public ?int $cancelRecordId = null; #[Locked] public string $message = ''; public function mount(): void { $this->authorize('machines.use'); abort_if(app(MachineQueries::class)->findMachine($this->machineId) === null, 404); } #[Computed] public function machine(): MachineSummary { return app(MachineQueries::class)->findMachine($this->machineId) ?? abort(404); } /** * @return list */ #[Computed] public function items(): array { return app(MaintenanceQueries::class)->itemsOf($this->machineId, auth()->user()?->can('machines.manage') === true); } /** * @return list */ #[Computed] public function history(): array { return app(MaintenanceQueries::class)->history( $this->machineId, $this->historyItem === '' ? null : (int) $this->historyItem, $this->validDay($this->historyFrom), $this->validDay($this->historyTo), ); } public function updated(string $property): void { $this->authorize('machines.use'); if (str_starts_with($property, 'history')) { unset($this->history); } } public function saveUsage(int $counterId, MachineMaintenance $maintenance): void { $this->authorize('machines.use'); $this->resetErrorBag('usage.'.$counterId); $counter = collect($this->machine->counters)->firstWhere('id', $counterId) ?? abort(404); $usage = $this->usage[$counterId] ?? ''; try { $milli = $maintenance->recordUsage($counterId, $usage, (int) auth()->id()); } catch (InvalidReading) { $this->addError('usage.'.$counterId, __('machines.maintenance.errors.usage')); return; } catch (CounterNotFound) { abort(404); } $this->message = $this->usageSaved($counter, $usage, $milli); unset($this->usage[$counterId], $this->machine, $this->items); } public function startRecord(int $itemId): void { $this->authorize('machines.use'); $this->recordingItemId = $this->item($itemId)->id; $this->recordNote = ''; $this->resetErrorBag(); $this->js("setTimeout(() => document.getElementById('record-note')?.focus())"); } public function cancelRecording(): void { $itemId = $this->recordingItemId; $this->recordingItemId = null; $this->js("document.getElementById('item-{$itemId}-done')?.focus()"); } public function record(MachineMaintenance $maintenance): void { $this->authorize('machines.use'); $item = $this->item((int) $this->recordingItemId); try { $result = $maintenance->recordMaintenance($item->id, (int) auth()->id(), $this->recordNote); } catch (MaintenanceItemNotFound) { abort(404); } $this->message = __('machines.maintenance.recorded', ['name' => $item->name]) .($result['stockRecorded'] ? '' : ' '.__('machines.maintenance.stock_not_recorded')); $this->recordingItemId = null; unset($this->items, $this->history, $this->machine); $this->js("document.getElementById('maintenance-status')?.focus()"); } public function confirmCancel(int $recordId): void { $this->authorize('machines.manage'); $this->cancelRecordId = $recordId; $this->js("document.getElementById('cancel-record-dialog').showModal()"); } public function cancelRecord(MachineMaintenance $maintenance): void { $this->authorize('machines.manage'); $record = collect($this->history)->firstWhere('id', $this->cancelRecordId) ?? abort(404); try { $maintenance->cancelRecord($record->id, (int) auth()->id()); $this->message = __('machines.maintenance.cancelled', ['name' => $record->itemName]); } catch (MaintenanceRecordNotFound|MaintenanceRecordCancelled) { $this->message = __('machines.errors.not_found'); } $this->cancelRecordId = null; unset($this->items, $this->history); $this->js("document.getElementById('cancel-record-dialog').close(); document.getElementById('maintenance-status').focus()"); } public function render(): mixed { return $this->view(); } private function item(int $itemId): MaintenanceItemStatus { return collect($this->items)->firstWhere('id', $itemId) ?? abort(404); } private function validDay(string $value): ?string { if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) !== 1) { return null; } $date = CarbonImmutable::createFromFormat('!Y-m-d', $value); return $date !== null && $date->format('Y-m-d') === $value ? $value : null; } }; ?>

{{ $message }}

@if ($this->machine->counters !== [])

{{ __('machines.maintenance.counters') }}

    @foreach ($this->machine->counters as $counter)
  • {{ $counter->name }}

    @if ($counter->readingMilli === null) {{ __('machines.maintenance.no_reading') }} @else {{ __('machines.maintenance.latest_reading', ['reading' => $this->reading($counter->readingMilli), 'unit' => __('machines.counter_units.'.$counter->unit->value), 'at' => $this->dateLabel((string) $counter->readAt)]) }} @endif

  • @endforeach
@endif @php($hasMaintenance = $this->machine->counters !== [] || $this->items !== []) @if ($hasMaintenance || auth()->user()?->can('machines.manage'))

{{ __('machines.maintenance.items') }}

@can('machines.manage')

@endcan @if ($this->items === []) {{ __('machines.maintenance.none') }} @else
    @foreach ($this->items as $item)
  • {{ $item->name }}@unless ($item->isActive) ({{ __('machines.index.inactive') }})@endunless

    {{ __('machines.maintenance.interval_text', ['interval' => $this->intervalText($item)]) }}

    @if ($item->lastPerformedAt === null) {{ __('machines.maintenance.never_done') }} @else {{ __('machines.maintenance.last_done', ['at' => $this->dateLabel($item->lastPerformedAt), 'name' => $item->lastPerformedByName]) }} @endif

    @can('machines.manage') {{ __('machines.maintenance.edit_named', ['name' => $item->name]) }} @endcan
    @if ($item->instructions !== null)
    {{ __('machines.maintenance.instructions') }}

    {{ $item->instructions }}

    @endif @if ($item->isActive) @if ($recordingItemId === $item->id)
    @else @endif @endif
  • @endforeach
@endif
@endif @if ($hasMaintenance)

{{ __('machines.maintenance.history') }}

{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}} @foreach ($this->items as $item) @endforeach

{{ trans_choice('machines.maintenance.history_count', count($this->history), ['count' => count($this->history)]) }}

@if ($this->history !== [])
    @foreach ($this->history as $record)
  • {{ $record->itemName }} · {{ $this->dateLabel($record->performedAt) }} · {{ $record->userName }}

    @if ($record->readingMilli !== null)

    {{ __('machines.maintenance.reading_was', ['reading' => $this->reading($record->readingMilli)]) }}

    @endif @if ($record->note !== null)

    {{ $record->note }}

    @endif @if ($record->cancelledAt !== null)

    @endif
    @if ($record->cancelledAt === null) @can('machines.manage') @endcan @endif
  • @endforeach
@endif
@endif @can('machines.manage')

{{ __('machines.maintenance.cancel_text') }}

@endcan