324 lines
13 KiB
PHP
324 lines
13 KiB
PHP
<?php
|
||
|
||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
||
use App\Modules\Machines\Application\DTOs\MachineData;
|
||
use App\Modules\Machines\Application\UseCases\SaveMachine;
|
||
use App\Modules\Machines\Domain\Enums\CounterUnit;
|
||
use App\Modules\Machines\Domain\Enums\MachineType;
|
||
use App\Modules\Machines\Domain\Exceptions\InvalidCounter;
|
||
use App\Modules\Machines\Domain\Exceptions\InvalidMachineName;
|
||
use App\Modules\Machines\Domain\Exceptions\MachineNameTaken;
|
||
use App\Modules\Machines\Domain\Exceptions\MachineNeedsServiceUnit;
|
||
use App\Modules\Machines\Domain\Exceptions\MachineNotFound;
|
||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||
use App\Modules\Users\Application\DTOs\UserOption;
|
||
use Livewire\Attributes\Computed;
|
||
use Livewire\Attributes\Locked;
|
||
use Livewire\Component;
|
||
|
||
/**
|
||
* Add page (no {machine}) and edit page (with {machine}) of a machine
|
||
* (management, spec 19.4–19.5): name, type, description, service units
|
||
* among those that use stock, the responsible person and the counters.
|
||
*/
|
||
new class extends Component
|
||
{
|
||
#[Locked]
|
||
public ?int $machineId = null;
|
||
|
||
/**
|
||
* Units the stored machine has, offered even after the setting is off.
|
||
*
|
||
* @var list<int>
|
||
*/
|
||
#[Locked]
|
||
public array $storedUnitIds = [];
|
||
|
||
#[Locked]
|
||
public bool $isActive = true;
|
||
|
||
public string $name = '';
|
||
|
||
public string $type = 'printer';
|
||
|
||
public string $description = '';
|
||
|
||
/**
|
||
* @var list<string>
|
||
*/
|
||
public array $serviceUnitIds = [];
|
||
|
||
public string $responsibleUserId = '';
|
||
|
||
/**
|
||
* @var list<array{key: int, id: ?int, name: string, unit: string}>
|
||
*/
|
||
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<array{value: int, label: string}>
|
||
*/
|
||
#[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<UserOption>
|
||
*/
|
||
#[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());
|
||
}
|
||
};
|
||
?>
|
||
|
||
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
|
||
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
|
||
|
||
<p role="status" aria-live="polite" class="text-sm font-medium">{{ $message }}</p>
|
||
|
||
<form wire:submit="save" class="space-y-6" novalidate>
|
||
<x-input id="machine-name" wire:model="name" :label="__('machines.form.name')" autocomplete="off" maxlength="150" required />
|
||
|
||
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
||
<x-select.native id="machine-type" wire:model="type" :label="__('machines.form.type')" :aria-label="__('machines.form.type')">
|
||
@foreach (MachineType::cases() as $option)
|
||
<option value="{{ $option->value }}">{{ __('machines.types.'.$option->value) }}</option>
|
||
@endforeach
|
||
</x-select.native>
|
||
|
||
<x-textarea id="machine-description" wire:model="description" :label="__('machines.form.description')" resize-auto />
|
||
|
||
@if ($this->unitOptions === [])
|
||
<p class="text-sm font-medium">{{ __('machines.form.no_stock_units') }}</p>
|
||
@else
|
||
<x-checkbox-group :legend="__('machines.form.service_units')" model="serviceUnitIds" :options="$this->unitOptions" :hint="__('machines.form.service_units_hint')" />
|
||
@endif
|
||
|
||
<x-select.native id="machine-responsible" wire:model="responsibleUserId" :label="__('machines.form.responsible')" :aria-label="__('machines.form.responsible')" :hint="__('machines.form.responsible_hint')">
|
||
<option value="">{{ __('machines.form.no_responsible') }}</option>
|
||
@foreach ($this->users as $user)
|
||
<option value="{{ $user->id }}">{{ $user->name }}</option>
|
||
@endforeach
|
||
</x-select.native>
|
||
|
||
<fieldset class="space-y-3">
|
||
<legend class="text-lg font-semibold">{{ __('machines.form.counters') }}</legend>
|
||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('machines.form.counters_hint') }}</p>
|
||
@error('counters')
|
||
<p role="alert" class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
|
||
@enderror
|
||
|
||
@foreach ($counters as $index => $counter)
|
||
<div wire:key="counter-{{ $counter['key'] }}" class="flex flex-wrap items-end gap-2">
|
||
<div class="min-w-48 flex-1">
|
||
<x-input
|
||
id="counter-{{ $counter['key'] }}-name"
|
||
wire:model="counters.{{ $index }}.name"
|
||
:label="__('machines.form.counter_name')"
|
||
autocomplete="off"
|
||
maxlength="100"
|
||
/>
|
||
</div>
|
||
<div class="w-32">
|
||
<x-select.native
|
||
id="counter-{{ $counter['key'] }}-unit"
|
||
wire:model="counters.{{ $index }}.unit"
|
||
:label="__('machines.form.counter_unit')"
|
||
:aria-label="__('machines.form.counter_unit').': '.__('machines.form.counter_named', ['number' => $index + 1])"
|
||
>
|
||
@foreach (CounterUnit::cases() as $unit)
|
||
<option value="{{ $unit->value }}">{{ __('machines.counter_units.'.$unit->value) }}</option>
|
||
@endforeach
|
||
</x-select.native>
|
||
</div>
|
||
<x-button wire:click="removeCounter({{ $index }})" subtle color="black" sm icon="trash" :aria-label="__('machines.form.remove_counter_named', ['number' => $index + 1])" />
|
||
</div>
|
||
@endforeach
|
||
|
||
<div class="flex flex-wrap gap-2">
|
||
<x-button id="add-counter" wire:click="addCounter" subtle color="black" sm icon="plus" :text="__('machines.form.add_counter')" />
|
||
<x-button wire:click="suggestCounters" subtle color="black" sm icon="sparkles" :text="__('machines.form.suggest_counters')" />
|
||
</div>
|
||
</fieldset>
|
||
|
||
<div class="flex flex-wrap gap-3">
|
||
<x-button submit :text="__('machines.form.save')" />
|
||
<x-button :href="$machineId === null ? route('machines.index') : route('machines.show', $machineId)" wire:navigate subtle color="black" :text="__('machines.form.cancel')" />
|
||
@if ($machineId !== null)
|
||
<x-button wire:click="toggleActive" subtle color="black" :icon="$isActive ? 'archive-box-x-mark' : 'archive-box-arrow-down'" :text="$isActive ? __('machines.form.deactivate') : __('machines.form.activate')" />
|
||
@endif
|
||
</div>
|
||
</form>
|
||
</div>
|