*/ #[Locked] public array $knownEntryIds = []; public string $search = ''; #[Locked] public ?int $groupId = null; #[Locked] public ?int $selectedItemId = null; public string $quantity = '1'; public string $reason = ''; #[Locked] public ?int $lastMovementId = null; public string $hours = ''; /** * @var array */ public array $readings = []; #[Locked] public ?int $recordingItemId = null; public string $recordReading = ''; #[Locked] public string $message = ''; #[Locked] public string $refreshMessage = ''; public function mount(int $machine): void { $this->authorize('machines.use'); $this->machineId = (app(MachineQueries::class)->findMachine($machine) ?? abort(404))->id; $this->knownEntryIds = array_map(fn (QueueRow $row): int => $row->entryId, $this->rows); } #[Computed] public function machine(): MachineSummary { return app(MachineQueries::class)->findMachine($this->machineId) ?? abort(404); } #[Computed] public function today(): string { return app(Clock::class)->today()->format('Y-m-d'); } public function day(): string { return $this->nextDay ? CarbonImmutable::parse($this->today)->addDay()->format('Y-m-d') : $this->today; } /** * @return list */ #[Computed] public function rows(): array { return app(MachineQueries::class)->rowsOn($this->machineId, $this->day(), $this->today); } #[Computed] public function selectedRow(): ?QueueRow { return $this->selectedEntryId === null ? null : collect($this->rows)->firstWhere('entryId', $this->selectedEntryId); } /** * @return list */ #[Computed] public function maintenance(): array { return app(MaintenanceQueries::class)->itemsOf($this->machineId); } /** * @return list */ #[Computed] public function groups(): array { return app(StockQueries::class)->groups(includeInactive: false); } /** * Items to show as buttons: the search or group results, or else this * machine's frequent and recent items. * * @return list */ #[Computed] public function items(): array { $stock = app(StockQueries::class); $search = trim($this->search); if ($search !== '' || $this->groupId !== null) { return array_slice($stock->items($search === '' ? null : $search, $this->groupId), 0, self::ITEM_LIMIT); } $favourites = $stock->machineFavourites($this->machineId); $ids = array_values(array_unique([...$favourites['frequent'], ...$favourites['recent']])); $items = $stock->itemsByIds($ids); return array_values(array_filter(array_map(fn (int $id): ?StockItemSummary => $items[$id] ?? null, $ids), fn ($item): bool => $item !== null && $item->isActive)); } #[Computed] public function selectedItem(): ?StockItemSummary { return $this->selectedItemId === null ? null : app(StockQueries::class)->findItem($this->selectedItemId); } /** * The polled refresh: new rows are announced, the rest updates quietly. */ public function refreshList(): void { $this->authorize('machines.use'); $new = $this->reload(); if ($new > 0) { $this->refreshMessage = trans_choice('machines.console.new_rows', $new, ['count' => $new]); } } public function refresh(Clock $clock): void { $this->authorize('machines.use'); $new = $this->reload(); $this->refreshMessage = __('machines.console.refreshed', ['time' => $clock->now()->setTimezone(new DateTimeZone((string) config('app.business_timezone')))->format('H.i')]) .' '.trans_choice('machines.console.new_rows', $new, ['count' => $new]); } public function showDay(bool $next): void { $this->authorize('machines.use'); $this->nextDay = $next; $this->selectedEntryId = null; unset($this->rows); } public function selectRow(int $entryId): void { $this->authorize('machines.use'); $this->selectedEntryId = $this->selectedEntryId === $entryId ? null : $entryId; $this->hours = ''; unset($this->selectedRow); if ($this->selectedEntryId !== null) { $this->js("setTimeout(() => document.getElementById('selected-task')?.focus())"); } } public function setStatus(int $entryId, string $status, MachineTasks $tasks): void { $this->authorize('machines.use'); abort_unless(in_array($status, ['in_progress', 'in_review'], true), 404); $row = $this->row($entryId); abort_if($row->task === null || ! $this->canFinish($row), 403); $this->authorize('jobs.update-task-status', $row->task->jobId); $tasks->changeStatus((int) $row->taskId, $status); $this->message = __('machines.console.status_changed', ['title' => $row->task->title, 'status' => __('jobs.tasks.statuses.'.$status)]); unset($this->rows, $this->selectedRow); } public function finish(int $entryId, MachineWorkList $workList): void { $this->authorize('machines.use'); $row = $this->row($entryId); abort_if($row->task === null || ! $this->canFinish($row), 403); $this->authorize('jobs.update-task-status', $row->task->jobId); try { $workList->finish($entryId, (int) auth()->id()); } catch (DomainException) { $this->message = __('machines.errors.not_found'); $this->reload(); return; } $this->message = __('machines.console.finished', ['title' => $row->task->title]); $this->selectedEntryId = $this->selectedEntryId === $entryId ? null : $this->selectedEntryId; $this->reload(); $this->js("document.getElementById('console-status')?.focus()"); } public function chooseGroup(?int $groupId): void { $this->authorize('inventory.record-use'); $this->groupId = $this->groupId === $groupId ? null : $groupId; $this->selectedItemId = null; unset($this->items); } public function selectItem(int $itemId): void { $this->authorize('inventory.record-use'); $this->selectedItemId = $itemId; $this->quantity = '1'; $this->resetErrorBag(); unset($this->selectedItem); $this->js("setTimeout(() => document.getElementById('material-quantity')?.focus())"); } public function changeQuantity(int $step): void { $current = StockQuantity::parse($this->quantity)?->milli ?? 0; $next = max(1000, intdiv($current, 1000) * 1000 + $step * 1000); $this->quantity = StockQuantity::inputValue($next); } public function recordMaterial(RecordStockUse $recordUse): void { $this->authorize('inventory.record-use'); $this->resetErrorBag(); $item = $this->selectedItem ?? abort(404); $row = $this->selectedRow; $task = $row?->task; try { $result = $recordUse->execute(new StockUseData( $item->id, $this->quantity, taskId: $task?->taskId, jobId: $task?->jobId, machineId: $this->machineId, reason: $task === null ? $this->reason : null, ), (int) auth()->id()); } catch (DomainException $exception) { $this->addError($exception instanceof App\Modules\Inventory\Domain\Exceptions\StockReasonRequired ? 'reason' : 'quantity', __(match (true) { $exception instanceof App\Modules\Inventory\Domain\Exceptions\StockReasonRequired => 'machines.console.reason_required', $exception instanceof App\Modules\Inventory\Domain\Exceptions\StockItemInactive => 'inventory.errors.inactive', default => 'inventory.errors.quantity', })); return; } $this->lastMovementId = $result->movementId; $this->message = __('machines.console.material_recorded', [ 'item' => $result->itemName, 'quantity' => $this->quantity($result->quantityMilli, $item->unit), 'balance' => $this->quantity($result->balanceMilli, $item->unit), ]); $this->selectedItemId = null; $this->quantity = '1'; unset($this->items, $this->selectedItem); $this->js("document.getElementById('console-status')?.focus()"); } public function undoLast(ReverseStockMovement $reverse): void { $this->authorize('inventory.record-use'); if ($this->lastMovementId === null) { return; } try { $reverse->execute($this->lastMovementId, (int) auth()->id(), Gate::allows('inventory.reverse-any')); $this->message = __('machines.console.material_undone'); } catch (DomainException) { $this->message = __('inventory.errors.reversal_not_allowed'); } $this->lastMovementId = null; unset($this->items); $this->js("document.getElementById('console-status')?.focus()"); } public function saveHours(AddTaskHours $addHours, Clock $clock): void { $this->authorize('time.use'); $task = $this->selectedRow?->task ?? abort(404); $this->authorize('time.book', $task->jobId); try { $total = $addHours->execute((int) auth()->id(), $task->taskId, $clock->today()->format('Y-m-d'), $this->hours, __('machines.console.hours_note', ['machine' => $this->machine->name])); } catch (DomainException) { $this->addError('hours', __('machines.console.hours_error')); return; } $this->message = __('machines.console.hours_saved', ['hours' => $this->hours, 'title' => $task->title, 'total' => $total]); $this->hours = ''; } public function saveReading(int $counterId, MachineMaintenance $maintenance): void { $this->authorize('machines.use'); $this->resetErrorBag('readings.'.$counterId); $counter = collect($this->machine->counters)->firstWhere('id', $counterId) ?? abort(404); try { $milli = $maintenance->recordReading($counterId, $this->readings[$counterId] ?? '', (int) auth()->id()); } catch (DomainException) { $this->addError('readings.'.$counterId, __('machines.maintenance.errors.reading')); return; } $this->message = __('machines.maintenance.reading_saved', ['counter' => $counter->name, 'reading' => $this->reading($milli), 'unit' => __('machines.counter_units.'.$counter->unit->value)]); unset($this->readings[$counterId], $this->machine, $this->maintenance); } public function startMaintenance(int $itemId): void { $this->authorize('machines.use'); $item = collect($this->maintenance)->firstWhere('id', $itemId) ?? abort(404); $this->recordingItemId = $itemId; $this->recordReading = $item->readingMilli === null ? '' : str_replace("\u{00A0}", '', $this->reading($item->readingMilli)); $this->js("setTimeout(() => document.getElementById('maintenance-confirm')?.focus())"); } public function recordMaintenance(MachineMaintenance $maintenance): void { $this->authorize('machines.use'); $item = collect($this->maintenance)->firstWhere('id', $this->recordingItemId) ?? abort(404); try { $result = $maintenance->recordMaintenance($item->id, (int) auth()->id(), $item->counterId === null ? '' : $this->recordReading); } catch (DomainException) { $this->addError('recordReading', __('machines.maintenance.errors.reading')); return; } $this->message = __('machines.maintenance.recorded', ['name' => $item->name]).($result['stockRecorded'] ? '' : ' '.__('machines.maintenance.stock_not_recorded')); $this->recordingItemId = null; unset($this->maintenance); $this->js("document.getElementById('console-status')?.focus()"); } /** * Rows of today and late rows are made; the next day's are only shown. */ public function canFinish(QueueRow $row): bool { return ! $this->nextDay; } /** * The first open task row of today: marked as next with text. */ public function nextEntryId(): ?int { if ($this->nextDay) { return null; } foreach ($this->rows as $row) { return $row->entryId; } return null; } public function render(): mixed { return $this->view()->title(__('machines.console.title', ['name' => $this->machine->name])); } /** * @return int How many rows are new since the last look. */ private function reload(): int { unset($this->rows, $this->maintenance, $this->items, $this->selectedRow, $this->machine); $ids = array_map(fn (QueueRow $row): int => $row->entryId, $this->rows); $new = count(array_diff($ids, $this->knownEntryIds)); $this->knownEntryIds = array_values(array_unique([...$this->knownEntryIds, ...$ids])); if ($this->selectedEntryId !== null && ! in_array($this->selectedEntryId, $ids, true)) { $this->selectedEntryId = null; } return $new; } private function row(int $entryId): QueueRow { return collect($this->rows)->firstWhere('entryId', $entryId) ?? abort(404); } }; ?> @php $touch = 'inline-flex min-h-11 min-w-11 items-center justify-center gap-2 rounded-lg px-4 py-2 font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:focus-visible:outline-blue-400'; $primary = $touch.' bg-primary-600 text-white hover:bg-primary-700'; $secondary = $touch.' border border-zinc-500 bg-white text-zinc-900 hover:bg-zinc-100 dark:border-zinc-400 dark:bg-zinc-900 dark:text-zinc-100 dark:hover:bg-zinc-700'; $machine = $this->machine; $lateMaintenance = array_values(array_filter($this->maintenance, fn ($item) => $item->level->value === 'overdue')); @endphp

{{ $machine->name }}

{{ __('machines.console.user', ['name' => auth()->user()?->name]) }}

@csrf

{{ $refreshMessage }}

{{ $message }}

@if ($lateMaintenance !== [])
@foreach ($lateMaintenance as $late)

@endforeach
@endif

{{ $nextDay ? __('machines.console.next_list', ['date' => $this->dateLabel($this->day())]) : __('machines.console.today_list') }}

@if ($this->rows === [])

{{ __('machines.show.none') }}

@else
    @foreach ($this->rows as $row) @php($task = $row->task)
  1. @if ($this->nextEntryId() === $row->entryId)

    {{ __('machines.console.next') }}

    @endif @if ($row->isOrder())

    {{ __('machines.show.order') }}

    @elseif ($task !== null)

    {{ $task->title }}

    {{ $task->jobNumber }} {{ $task->jobName }} · {{ $task->customerName }}

    {{ __('machines.show.status', ['status' => __('jobs.tasks.statuses.'.$task->status)]) }} · {{ __('machines.show.priority', ['priority' => __('jobs.priorities.'.$task->priority)]) }} @if ($task->dueOn !== null) · {{ __('machines.show.due', ['date' => $this->dateLabel($task->dueOn)]) }} @endif

    @endif @if ($row->isLate)

    @endif @if ($task !== null)
    @if ($this->canFinish($row)) @endif
    @endif
  2. @endforeach
@endif @if ($this->selectedRow?->task !== null) @php($selected = $this->selectedRow->task)

{{ __('machines.console.selected', ['title' => $selected->title]) }}

@if ($selected->instructions !== null)

{{ __('machines.show.instructions') }}

{{ $selected->instructions }}

@endif @if ($selected->description !== null)

{{ __('machines.show.description') }}

{{ $selected->description }}

@endif @can('time.book', $selected->jobId)
@endcan
@endif
@can('inventory.record-use')

{{ __('machines.console.material') }}

{{ $this->selectedRow?->task !== null ? __('machines.console.material_for', ['title' => $this->selectedRow->task->title]) : __('machines.console.material_without_task') }}

@if ($this->groups !== [])
@foreach ($this->groups as $group) @endforeach
@endif

{{ trim($search) === '' && $groupId === null ? __('machines.console.favourites') : trans_choice('inventory.task.results', count($this->items), ['count' => count($this->items)]) }}

    @foreach ($this->items as $item)
  • @endforeach
@if ($this->selectedItem !== null)

{{ $this->selectedItem->name }} ({{ __('inventory.task.balance', ['balance' => $this->quantity($this->selectedItem->balanceMilli, $this->selectedItem->unit)]) }})

@if ($this->selectedRow?->task === null) @endif @endif @if ($lastMovementId !== null) @endif
@endcan

{{ __('machines.console.counters_and_maintenance') }}

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

    @if ($recordingItemId === $item->id)
    @if ($item->counterId !== null)
    @endif
    @else @endif
  • @endforeach