*/ #[Locked] public array $storedUnitIds = []; #[Locked] public bool $isActive = true; public string $name = ''; public string $type = 'printer'; public string $description = ''; /** * @var list */ public array $serviceUnitIds = []; public string $responsibleUserId = ''; /** * @var list */ public array $counters = []; public int $nextKey = 1; #[Locked] public string $message = ''; public function mount(MachineQueries $machines, ?int $machine = null): void { $this->authorize('machines.manage'); if ($machine === null) { $stockUnits = app(CatalogQueries::class)->stockServiceUnitIds(); $this->serviceUnitIds = count($stockUnits) === 1 ? [(string) $stockUnits[0]] : []; $this->suggestCounters(announce: false); return; } $summary = $machines->findMachine($machine) ?? abort(404); $this->machineId = $summary->id; $this->isActive = $summary->isActive; $this->name = $summary->name; $this->type = $summary->type->value; $this->description = (string) $summary->description; $this->serviceUnitIds = array_map('strval', $summary->serviceUnitIds); $this->storedUnitIds = $summary->serviceUnitIds; $this->responsibleUserId = (string) ($summary->responsibleUserId ?? ''); foreach ($summary->counters as $counter) { $this->counters[] = ['key' => $this->nextKey++, 'id' => $counter->id, 'name' => $counter->name, 'unit' => $counter->unit->value]; } } /** * @return list */ #[Computed] public function unitOptions(): array { $ids = array_values(array_unique(array_merge(app(CatalogQueries::class)->stockServiceUnitIds(), $this->storedUnitIds))); $options = []; foreach (app(CatalogQueries::class)->serviceUnitNames($ids) as $id => $name) { $options[] = ['value' => $id, 'label' => $name]; } return $options; } /** * @return list */ #[Computed] public function users(): array { return app(UserAccountQueries::class)->activeUserOptions(); } public function addCounter(): void { $this->authorize('machines.manage'); $key = $this->nextKey++; $this->counters[] = ['key' => $key, 'id' => null, 'name' => '', 'unit' => CounterUnit::Piece->value]; $this->message = __('machines.form.counter_added', ['number' => count($this->counters)]); $this->js("document.getElementById('counter-{$key}-name')?.focus()"); } public function removeCounter(int $index): void { $this->authorize('machines.manage'); if (! isset($this->counters[$index])) { return; } array_splice($this->counters, $index, 1); $this->resetErrorBag(); $this->message = __('machines.form.counter_removed', ['number' => $index + 1]); $this->js("document.getElementById('add-counter')?.focus()"); } /** * Adds the usual counters of the machine type that are not there yet. */ public function suggestCounters(bool $announce = true): void { $this->authorize('machines.manage'); $added = 0; $names = array_map(fn (array $counter): string => mb_strtolower($counter['name']), $this->counters); foreach ((MachineType::tryFrom($this->type) ?? MachineType::Other)->suggestedCounters() as $suggestion) { $name = __('machines.counter_names.'.$suggestion['name']); if (! in_array(mb_strtolower($name), $names, true)) { $this->counters[] = ['key' => $this->nextKey++, 'id' => null, 'name' => $name, 'unit' => $suggestion['unit']->value]; $added++; } } if ($announce) { $this->message = trans_choice('machines.form.counters_suggested', $added, ['count' => $added]); } } public function save(SaveMachine $saveMachine): void { $this->authorize('machines.manage'); $this->resetErrorBag(); $this->validate([ 'type' => ['required', 'in:'.implode(',', array_column(MachineType::cases(), 'value'))], 'description' => ['nullable', 'string', 'max:5000'], 'serviceUnitIds' => ['array'], 'serviceUnitIds.*' => ['integer'], 'responsibleUserId' => ['nullable', 'integer'], 'counters.*.unit' => ['required', 'in:'.implode(',', array_column(CounterUnit::cases(), 'value'))], ]); $data = new MachineData( $this->name, $this->type, $this->description, array_map('intval', $this->serviceUnitIds), $this->responsibleUserId === '' ? null : (int) $this->responsibleUserId, array_map(fn (array $counter): array => ['id' => $counter['id'], 'name' => $counter['name'], 'unit' => $counter['unit']], $this->counters), ); try { if ($this->machineId === null) { $this->machineId = $saveMachine->add($data); $status = __('machines.form.created', ['name' => trim($this->name)]); } else { $saveMachine->update($this->machineId, $data); $status = __('machines.form.updated', ['name' => trim($this->name)]); } } catch (InvalidMachineName) { $this->addError('name', __('machines.errors.name')); return; } catch (MachineNameTaken) { $this->addError('name', __('machines.errors.name_taken')); return; } catch (MachineNeedsServiceUnit) { $this->addError('serviceUnitIds', __('machines.errors.service_units')); return; } catch (InvalidCounter) { $this->addError('counters', __('machines.errors.counter')); return; } catch (MachineNotFound) { abort(404); } session()->flash('status', $status); $this->redirectRoute('machines.show', $this->machineId, navigate: true); } public function toggleActive(SaveMachine $saveMachine): void { $this->authorize('machines.manage'); if ($this->machineId === null) { return; } $saveMachine->setActive($this->machineId, ! $this->isActive); $this->isActive = ! $this->isActive; $this->message = __($this->isActive ? 'machines.form.activated' : 'machines.form.deactivated'); } public function heading(): string { return __($this->machineId === null ? 'machines.form.create_title' : 'machines.form.edit_title'); } public function render(): mixed { return $this->view()->title($this->heading()); } }; ?>

{{ $this->heading() }}

{{ $message }}

{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}} @foreach (MachineType::cases() as $option) @endforeach @if ($this->unitOptions === [])

{{ __('machines.form.no_stock_units') }}

@else @endif @foreach ($this->users as $user) @endforeach
{{ __('machines.form.counters') }}

{{ __('machines.form.counters_hint') }}

@error('counters') @enderror @foreach ($counters as $index => $counter)
@foreach (CounterUnit::cases() as $unit) @endforeach
@endforeach
@if ($machineId !== null) @endif