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

170 lines
6.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use App\Modules\Inventory\Application\Contracts\StockQueries;
use App\Modules\Inventory\Application\DTOs\StockGroupSummary;
use App\Modules\Inventory\Application\DTOs\StockItemSummary;
use App\Modules\Inventory\Presentation\Livewire\Concerns\FormatsStock;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
use Livewire\Component;
/**
* The stock list (spec 19.1): items with their balances, searched by name
* or code and filtered by group and "only items to order". Management also
* sees the average costs and the stock value.
*/
new class extends Component
{
use FormatsStock;
#[Url(as: 'haku', except: '')]
public string $search = '';
#[Url(as: 'ryhma', except: '')]
public string $group = '';
#[Url(as: 'tilattavat', except: false)]
public bool $onlyReorder = false;
public bool $includeInactive = false;
public function mount(): void
{
$this->authorize('inventory.view');
}
/**
* @return list<StockItemSummary>
*/
#[Computed]
public function items(): array
{
return app(StockQueries::class)->items(
$this->search,
$this->group === '' ? null : (int) $this->group,
$this->onlyReorder,
$this->includeInactive && Gate::allows('inventory.manage'),
);
}
/**
* @return list<StockGroupSummary>
*/
#[Computed]
public function groups(): array
{
return app(StockQueries::class)->groups(includeInactive: false);
}
public function updated(): void
{
$this->authorize('inventory.view');
unset($this->items);
}
/**
* @return list<array{index: string, label: string, sortable: bool, align?: string}>
*/
public function headers(): array
{
$headers = [
['index' => 'name', 'label' => __('inventory.columns.name'), 'sortable' => false],
['index' => 'group', 'label' => __('inventory.columns.group'), 'sortable' => false],
['index' => 'balance', 'label' => __('inventory.columns.balance'), 'sortable' => false, 'align' => 'right'],
['index' => 'reorder', 'label' => __('inventory.columns.reorder_level'), 'sortable' => false, 'align' => 'right'],
['index' => 'status', 'label' => __('inventory.columns.status'), 'sortable' => false],
];
if (Gate::allows('inventory.see-value')) {
$headers[] = ['index' => 'cost', 'label' => __('inventory.columns.average_cost'), 'sortable' => false, 'align' => 'right'];
$headers[] = ['index' => 'value', 'label' => __('inventory.columns.value'), 'sortable' => false, 'align' => 'right'];
}
return $headers;
}
public function render(): mixed
{
return $this->view()->title(__('inventory.index.title'));
}
};
?>
<div class="mx-auto max-w-6xl space-y-6 p-4 sm:p-6">
<div class="space-y-2">
<h1 class="text-2xl font-semibold">{{ __('inventory.index.title') }}</h1>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('inventory.index.intro') }}</p>
@can('inventory.manage')
<div class="flex flex-wrap gap-3">
<x-button :href="route('inventory.items.create')" wire:navigate icon="plus" :text="__('inventory.index.new_item')" />
<x-button :href="route('inventory.groups')" wire:navigate subtle color="black" icon="folder" :text="__('inventory.index.groups')" />
</div>
@endcan
</div>
<div class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
<x-input id="stock-search" wire:model.live.debounce.300ms="search" type="search" icon="magnifying-glass" :label="__('inventory.index.search')" autocomplete="off" />
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native id="stock-group" wire:model.live="group" :label="__('inventory.index.group')" :aria-label="__('inventory.index.group')">
<option value="">{{ __('inventory.index.all_groups') }}</option>
@foreach ($this->groups as $option)
<option value="{{ $option->id }}">{{ $option->name }}</option>
@endforeach
</x-select.native>
<div class="flex flex-col justify-end gap-2">
<x-checkbox id="only-reorder" wire:model.live="onlyReorder" :label="__('inventory.index.only_reorder')" />
@can('inventory.manage')
<x-checkbox id="include-inactive" wire:model.live="includeInactive" :label="__('inventory.index.include_inactive')" />
@endcan
</div>
</div>
<p role="status" aria-live="polite" class="text-sm font-medium">
{{ trans_choice('inventory.index.count', count($this->items), ['count' => count($this->items)]) }}
</p>
@if ($this->items === [])
<p class="text-sm">{{ __('inventory.index.none') }}</p>
@else
<x-table :headers="$this->headers()" :rows="$this->items">
@interact('column_name', $item)
<x-link :href="route('inventory.items.show', $item->id)" navigate class="whitespace-normal">{{ $item->name }}</x-link>
@if ($item->code !== null)
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ $item->code }}</span>
@endif
@endinteract
@interact('column_group', $item)
<span class="whitespace-normal">{{ $item->groupName }}</span>
@endinteract
@interact('column_balance', $item)
<span class="whitespace-nowrap">{{ $this->quantity($item->balanceMilli, $item->unit) }}</span>
@endinteract
@interact('column_reorder', $item)
<span class="whitespace-nowrap">{{ $item->reorderLevelMilli === null ? '–' : $this->quantity($item->reorderLevelMilli, $item->unit) }}</span>
@endinteract
@interact('column_status', $item)
<x-inventory.stock-status :item="$item" />
@endinteract
@interact('column_cost', $item)
<span class="whitespace-nowrap">{{ $this->unitCost($item->averageCostHundredths) }}</span>
@endinteract
@interact('column_value', $item)
<span class="whitespace-nowrap">{{ $this->money($item->valueCents) }}</span>
@endinteract
</x-table>
@can('inventory.see-value')
<p class="text-right font-semibold">{{ __('inventory.index.total_value', ['value' => $this->money(array_sum(array_map(fn ($item) => $item->valueCents, $this->items)))]) }}</p>
@endcan
@endif
</div>