Count stock and correct the differences on confirmation
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Inventory\Application\Contracts\InventoryCountQueries;
|
||||
use App\Modules\Inventory\Application\Contracts\StockQueries;
|
||||
use App\Modules\Inventory\Application\DTOs\InventoryCountSummary;
|
||||
use App\Modules\Inventory\Application\UseCases\InventoryCounts;
|
||||
use App\Modules\Inventory\Domain\Exceptions\InventoryCountAlreadyOpen;
|
||||
use App\Modules\Inventory\Presentation\Livewire\Concerns\FormatsStock;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* Stock counts (inventaario, spec 19.3): the list of counts, newest first,
|
||||
* and for management the start of a new one for the whole stock or chosen
|
||||
* item groups. Only one count is open at a time.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
use FormatsStock;
|
||||
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
public array $groupIds = [];
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->authorize('inventory.count');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<InventoryCountSummary>
|
||||
*/
|
||||
#[Computed]
|
||||
public function counts(): array
|
||||
{
|
||||
return app(InventoryCountQueries::class)->counts();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{value: int, label: string}>
|
||||
*/
|
||||
#[Computed]
|
||||
public function groupOptions(): array
|
||||
{
|
||||
return array_map(
|
||||
fn ($group): array => ['value' => $group->id, 'label' => $group->name],
|
||||
app(StockQueries::class)->groups(includeInactive: false),
|
||||
);
|
||||
}
|
||||
|
||||
public function start(InventoryCounts $counts): void
|
||||
{
|
||||
$this->authorize('inventory.manage-counts');
|
||||
|
||||
try {
|
||||
$id = $counts->start(array_map('intval', $this->groupIds), (int) auth()->id());
|
||||
} catch (InventoryCountAlreadyOpen) {
|
||||
$this->addError('groupIds', __('inventory.counts.errors.already_open'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->redirectRoute('inventory.counts.show', $id, navigate: true);
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title(__('inventory.counts.title'));
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
|
||||
<div class="space-y-2">
|
||||
<h1 class="text-2xl font-semibold">{{ __('inventory.counts.title') }}</h1>
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('inventory.counts.intro') }}</p>
|
||||
</div>
|
||||
|
||||
@can('inventory.manage-counts')
|
||||
<section aria-labelledby="start-heading" class="space-y-3 rounded-lg border border-zinc-300 p-4 dark:border-zinc-600">
|
||||
<h2 id="start-heading" class="text-lg font-semibold">{{ __('inventory.counts.start_title') }}</h2>
|
||||
<form wire:submit="start" class="space-y-4" novalidate>
|
||||
@if ($this->groupOptions !== [])
|
||||
<x-checkbox-group
|
||||
:legend="__('inventory.counts.groups')"
|
||||
model="groupIds"
|
||||
:options="$this->groupOptions"
|
||||
:hint="__('inventory.counts.groups_hint')"
|
||||
/>
|
||||
@endif
|
||||
@error('groupIds')
|
||||
<p role="alert" class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
|
||||
@enderror
|
||||
<x-button submit icon="clipboard-document-check" :text="__('inventory.counts.start')" />
|
||||
</form>
|
||||
</section>
|
||||
@endcan
|
||||
|
||||
<section aria-labelledby="counts-heading" class="space-y-3">
|
||||
<h2 id="counts-heading" class="text-lg font-semibold">{{ __('inventory.counts.list') }}</h2>
|
||||
|
||||
@if ($this->counts === [])
|
||||
<p class="text-sm">{{ __('inventory.counts.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->counts as $count)
|
||||
<li wire:key="count-{{ $count->id }}" class="flex flex-wrap items-center justify-between gap-3 p-3">
|
||||
<div class="min-w-0 space-y-1">
|
||||
<p class="font-medium">
|
||||
<x-link :href="route('inventory.counts.show', $count->id)" navigate>
|
||||
{{ __('inventory.counts.name', ['date' => $this->dateLabel($count->countedOn)]) }}
|
||||
</x-link>
|
||||
</p>
|
||||
<p class="text-sm">
|
||||
{{ $count->groupNames === [] ? __('inventory.counts.whole_stock') : implode(', ', $count->groupNames) }}
|
||||
· {{ trans_choice('inventory.counts.counted_items', $count->countedItems, ['count' => $count->countedItems]) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex flex-wrap items-center gap-3 text-sm">
|
||||
@if ($count->isConfirmed)
|
||||
<span class="inline-flex items-center gap-1">
|
||||
<x-icon name="lock-closed" class="size-4" aria-hidden="true" />
|
||||
{{ __('inventory.counts.confirmed_by', ['name' => $count->confirmedByName, 'at' => $this->dateLabel((string) $count->confirmedAt)]) }}
|
||||
</span>
|
||||
@can('inventory.manage-counts')
|
||||
<a href="{{ route('inventory.counts.csv', $count->id) }}" class="inline-flex items-center gap-1 font-medium underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700">
|
||||
<x-icon name="arrow-down-tray" class="size-4" aria-hidden="true" />
|
||||
{{ __('inventory.counts.csv_named', ['date' => $this->dateLabel($count->countedOn)]) }}
|
||||
</a>
|
||||
@endcan
|
||||
@else
|
||||
<span class="inline-flex items-center gap-1 font-medium">
|
||||
<x-icon name="pencil-square" class="size-4" aria-hidden="true" />
|
||||
{{ __('inventory.counts.open') }}
|
||||
</span>
|
||||
@endif
|
||||
</div>
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
@endif
|
||||
</section>
|
||||
</div>
|
||||
Reference in New Issue
Block a user