Files
2026-11-26 16:18:59 +02:00

213 lines
8.5 KiB
PHP

<?php
use App\Modules\Inventory\Application\Contracts\StockQueries;
use App\Modules\Inventory\Application\DTOs\StockGroupSummary;
use App\Modules\Inventory\Application\UseCases\AddStockItemGroup;
use App\Modules\Inventory\Application\UseCases\ChangeStockItemGroup;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockName;
use App\Modules\Inventory\Domain\Exceptions\StockItemGroupNotFound;
use App\Modules\Inventory\Domain\Exceptions\StockNameTaken;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Stock item groups (nimikeryhmät, spec 19.1): adding, renaming, ordering
* with Move up / Move down and taking out of use. Management only.
*/
new class extends Component
{
public string $newName = '';
#[Locked]
public ?int $editingId = null;
public string $editingName = '';
#[Locked]
public string $message = '';
public function mount(): void
{
$this->authorize('inventory.manage');
}
/**
* @return list<StockGroupSummary>
*/
#[Computed]
public function groups(): array
{
return app(StockQueries::class)->groups();
}
public function add(AddStockItemGroup $addGroup): void
{
$this->authorize('inventory.manage');
try {
$addGroup->execute($this->newName);
} catch (InvalidStockName) {
$this->addError('newName', __('inventory.errors.name'));
return;
} catch (StockNameTaken) {
$this->addError('newName', __('inventory.errors.name_taken'));
return;
}
$this->message = __('inventory.groups.added', ['name' => trim($this->newName)]);
$this->newName = '';
unset($this->groups);
$this->js("document.getElementById('new-group-name')?.focus()");
}
public function startRename(int $groupId): void
{
$this->authorize('inventory.manage');
$group = collect($this->groups)->firstWhere('id', $groupId) ?? abort(404);
$this->editingId = $group->id;
$this->editingName = $group->name;
$this->resetErrorBag();
$this->js("setTimeout(() => document.getElementById('rename-group')?.focus())");
}
public function cancelRename(): void
{
$id = $this->editingId;
$this->editingId = null;
$this->js("document.getElementById('rename-group-{$id}')?.focus()");
}
public function rename(ChangeStockItemGroup $change): void
{
$this->authorize('inventory.manage');
try {
$change->rename((int) $this->editingId, $this->editingName);
} catch (InvalidStockName) {
$this->addError('editingName', __('inventory.errors.name'));
return;
} catch (StockNameTaken) {
$this->addError('editingName', __('inventory.errors.name_taken'));
return;
} catch (StockItemGroupNotFound) {
$this->message = __('inventory.errors.not_found');
}
$id = $this->editingId;
$this->message = __('inventory.groups.renamed', ['name' => trim($this->editingName)]);
$this->editingId = null;
unset($this->groups);
$this->js("document.getElementById('rename-group-{$id}')?.focus()");
}
public function move(int $groupId, string $direction, ChangeStockItemGroup $change): void
{
$this->authorize('inventory.manage');
$group = collect($this->groups)->firstWhere('id', $groupId) ?? abort(404);
$up = $direction === 'up';
try {
$moved = $change->move($groupId, $up);
} catch (StockItemGroupNotFound) {
$this->message = __('inventory.errors.not_found');
return;
}
$this->message = __('inventory.groups.'.($moved ? ($up ? 'moved_up' : 'moved_down') : ($up ? 'first' : 'last')), ['name' => $group->name]);
unset($this->groups);
}
public function toggleActive(int $groupId, ChangeStockItemGroup $change): void
{
$this->authorize('inventory.manage');
$group = collect($this->groups)->firstWhere('id', $groupId) ?? abort(404);
try {
$change->setActive($groupId, ! $group->isActive);
} catch (StockItemGroupNotFound) {
$this->message = __('inventory.errors.not_found');
return;
}
$this->message = __($group->isActive ? 'inventory.groups.deactivated' : 'inventory.groups.activated', ['name' => $group->name]);
unset($this->groups);
}
public function render(): mixed
{
return $this->view()->title(__('inventory.groups.title'));
}
};
?>
<div class="mx-auto max-w-3xl space-y-6 p-4 sm:p-6">
<div class="space-y-2">
<h1 class="text-2xl font-semibold">{{ __('inventory.groups.title') }}</h1>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('inventory.groups.intro') }}</p>
<p class="text-sm"><x-link :href="route('inventory.index')" navigate>{{ __('inventory.groups.back') }}</x-link></p>
</div>
<p role="status" aria-live="polite" class="text-sm font-medium">{{ $message }}</p>
<form wire:submit="add" class="flex flex-wrap items-end gap-3" novalidate>
<div class="min-w-0 flex-1">
<x-input id="new-group-name" wire:model="newName" :label="__('inventory.groups.name')" autocomplete="off" maxlength="150" />
</div>
<x-button submit icon="plus" :text="__('inventory.groups.add')" />
</form>
@if ($this->groups === [])
<p class="text-sm">{{ __('inventory.groups.none') }}</p>
@else
<ul class="divide-y divide-zinc-200 rounded-lg border border-zinc-300 dark:divide-zinc-700 dark:border-zinc-600">
@foreach ($this->groups as $group)
<li wire:key="group-{{ $group->id }}" class="flex flex-wrap items-center gap-3 p-3">
@if ($editingId === $group->id)
<form wire:submit="rename" class="flex w-full flex-wrap items-end gap-2" novalidate>
<div class="min-w-0 flex-1">
<x-input id="rename-group" wire:model="editingName" :label="__('inventory.groups.name')" autocomplete="off" maxlength="150" />
</div>
<x-button submit :text="__('inventory.groups.save')" />
<x-button wire:click="cancelRename" subtle color="black" :text="__('inventory.groups.cancel')" />
</form>
@else
<div class="min-w-0 flex-1">
<p class="font-medium break-words">{{ $group->name }}</p>
<p class="text-sm text-zinc-600 dark:text-zinc-300">
{{ trans_choice('inventory.groups.items', $group->itemCount, ['count' => $group->itemCount]) }}
@unless ($group->isActive)
· {{ __('inventory.status.inactive') }}
@endunless
</p>
</div>
<div class="flex flex-wrap gap-1">
<x-button wire:click="move({{ $group->id }}, 'up')" subtle color="black" sm icon="arrow-up" :aria-label="__('inventory.groups.up_named', ['name' => $group->name])" />
<x-button wire:click="move({{ $group->id }}, 'down')" subtle color="black" sm icon="arrow-down" :aria-label="__('inventory.groups.down_named', ['name' => $group->name])" />
<x-button id="rename-group-{{ $group->id }}" wire:click="startRename({{ $group->id }})" subtle color="black" sm icon="pencil" :text="__('inventory.groups.rename')" :aria-label="__('inventory.groups.rename_named', ['name' => $group->name])" />
<x-button
wire:click="toggleActive({{ $group->id }})"
subtle
color="black"
sm
:icon="$group->isActive ? 'archive-box-x-mark' : 'archive-box-arrow-down'"
:text="$group->isActive ? __('inventory.groups.deactivate') : __('inventory.groups.activate')"
:aria-label="__($group->isActive ? 'inventory.groups.deactivate_named' : 'inventory.groups.activate_named', ['name' => $group->name])"
/>
</div>
@endif
</li>
@endforeach
</ul>
@endif
</div>