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,282 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Inventory\Application\Contracts\InventoryCountQueries;
|
||||
use App\Modules\Inventory\Application\DTOs\InventoryCountLine;
|
||||
use App\Modules\Inventory\Application\DTOs\InventoryCountSummary;
|
||||
use App\Modules\Inventory\Application\UseCases\InventoryCounts;
|
||||
use App\Modules\Inventory\Domain\Exceptions\InvalidStockQuantity;
|
||||
use App\Modules\Inventory\Domain\Exceptions\InventoryCountConfirmed;
|
||||
use App\Modules\Inventory\Domain\Exceptions\InventoryCountNotFound;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockItemNotFound;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockItemNotInCount;
|
||||
use App\Modules\Inventory\Domain\Services\StockValuation;
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
|
||||
use App\Modules\Inventory\Presentation\Livewire\Concerns\FormatsStock;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* A stock count's counting list (spec 19.3): items by group, with a field
|
||||
* for the counted quantity that saves when left (several people may count
|
||||
* at the same time, also on tablets). The difference to the book balance
|
||||
* at the moment of counting is shown as a quantity, and for management as
|
||||
* a value. Management confirms the count in a dialog that tells how many
|
||||
* items are left uncounted.
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
use FormatsStock;
|
||||
|
||||
#[Locked]
|
||||
public int $countId;
|
||||
|
||||
public bool $onlyUncounted = false;
|
||||
|
||||
#[Locked]
|
||||
public string $message = '';
|
||||
|
||||
public function mount(int $count): void
|
||||
{
|
||||
$this->authorize('inventory.count');
|
||||
|
||||
abort_if(app(InventoryCountQueries::class)->findCount($count) === null, 404);
|
||||
$this->countId = $count;
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
public function count(): InventoryCountSummary
|
||||
{
|
||||
return app(InventoryCountQueries::class)->findCount($this->countId) ?? abort(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<InventoryCountLine>
|
||||
*/
|
||||
#[Computed]
|
||||
public function lines(): array
|
||||
{
|
||||
return app(InventoryCountQueries::class)->lines($this->countId);
|
||||
}
|
||||
|
||||
/**
|
||||
* The lines shown, by group name.
|
||||
*
|
||||
* @return array<string, list<InventoryCountLine>>
|
||||
*/
|
||||
public function groupedLines(): array
|
||||
{
|
||||
$groups = [];
|
||||
|
||||
foreach ($this->lines as $line) {
|
||||
if (! $this->onlyUncounted || ! $line->isCounted()) {
|
||||
$groups[$line->groupName][] = $line;
|
||||
}
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
public function uncountedCount(): int
|
||||
{
|
||||
return count(array_filter($this->lines, fn (InventoryCountLine $line): bool => ! $line->isCounted()));
|
||||
}
|
||||
|
||||
public function updatedOnlyUncounted(): void
|
||||
{
|
||||
$this->authorize('inventory.count');
|
||||
}
|
||||
|
||||
public function saveCounted(int $itemId, string $value, InventoryCounts $counts): void
|
||||
{
|
||||
$this->authorize('inventory.count');
|
||||
$this->resetErrorBag('counted.'.$itemId);
|
||||
|
||||
$line = collect($this->lines)->firstWhere('itemId', $itemId) ?? abort(404);
|
||||
|
||||
try {
|
||||
$book = $counts->record($this->countId, $itemId, $value, (int) auth()->id());
|
||||
} catch (InvalidStockQuantity) {
|
||||
$this->addError('counted.'.$itemId, __('inventory.counts.errors.quantity'));
|
||||
|
||||
return;
|
||||
} catch (InventoryCountConfirmed) {
|
||||
$this->message = __('inventory.counts.errors.confirmed');
|
||||
|
||||
return;
|
||||
} catch (InventoryCountNotFound|StockItemNotFound|StockItemNotInCount) {
|
||||
$this->message = __('inventory.errors.not_found');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
unset($this->lines);
|
||||
|
||||
if ($book === null) {
|
||||
$this->message = __('inventory.counts.cleared', ['item' => $line->itemName]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$counted = (int) StockQuantity::parse($value)?->milli;
|
||||
$this->message = __('inventory.counts.saved', [
|
||||
'item' => $line->itemName,
|
||||
'counted' => $this->quantity($counted, $line->unit),
|
||||
'difference' => $this->signed($counted - $book, $line->unit),
|
||||
]);
|
||||
}
|
||||
|
||||
public function confirmCount(): void
|
||||
{
|
||||
$this->authorize('inventory.manage-counts');
|
||||
$this->js("document.getElementById('confirm-count-dialog').showModal()");
|
||||
}
|
||||
|
||||
public function confirm(InventoryCounts $counts): void
|
||||
{
|
||||
$this->authorize('inventory.manage-counts');
|
||||
|
||||
try {
|
||||
$adjustments = $counts->confirm($this->countId, (int) auth()->id());
|
||||
$this->message = trans_choice('inventory.counts.confirmed_message', $adjustments, ['count' => $adjustments]);
|
||||
} catch (InventoryCountConfirmed) {
|
||||
$this->message = __('inventory.counts.errors.confirmed');
|
||||
} catch (InventoryCountNotFound|StockItemNotFound) {
|
||||
$this->message = __('inventory.errors.not_found');
|
||||
}
|
||||
|
||||
unset($this->count, $this->lines);
|
||||
$this->js("document.getElementById('confirm-count-dialog').close(); document.getElementById('count-status').focus()");
|
||||
}
|
||||
|
||||
public function signed(int $milli, mixed $unit): string
|
||||
{
|
||||
return ($milli > 0 ? '+' : '').$this->quantity($milli, $unit);
|
||||
}
|
||||
|
||||
public function differenceValue(InventoryCountLine $line): int
|
||||
{
|
||||
return StockValuation::valueCents((int) $line->differenceMilli(), $line->unitCostHundredths);
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title(__('inventory.counts.name', ['date' => $this->dateLabel($this->count->countedOn)]));
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
|
||||
@php($count = $this->count)
|
||||
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm"><x-link :href="route('inventory.counts')" navigate>{{ __('inventory.counts.back') }}</x-link></p>
|
||||
<h1 class="text-2xl font-semibold">{{ __('inventory.counts.name', ['date' => $this->dateLabel($count->countedOn)]) }}</h1>
|
||||
<p class="text-sm">
|
||||
{{ $count->groupNames === [] ? __('inventory.counts.whole_stock') : implode(', ', $count->groupNames) }}
|
||||
· {{ __('inventory.counts.started_by', ['name' => $count->startedByName]) }}
|
||||
</p>
|
||||
@if ($count->isConfirmed)
|
||||
<p class="flex items-center gap-2 text-sm font-medium">
|
||||
<x-icon name="lock-closed" class="size-5" aria-hidden="true" />
|
||||
{{ __('inventory.counts.confirmed_by', ['name' => $count->confirmedByName, 'at' => $this->dateLabel((string) $count->confirmedAt)]) }}
|
||||
</p>
|
||||
@can('inventory.see-value')
|
||||
<p class="text-sm">{{ __('inventory.counts.values', ['before' => $this->money((int) $count->valueBeforeCents), 'after' => $this->money((int) $count->valueAfterCents)]) }}</p>
|
||||
@endcan
|
||||
@else
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('inventory.counts.count_help') }}</p>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<p id="count-status" tabindex="-1" role="status" aria-live="polite" class="text-sm font-medium focus:outline-none">{{ $message }}</p>
|
||||
|
||||
<div class="flex flex-wrap items-center gap-4">
|
||||
@unless ($count->isConfirmed)
|
||||
<x-checkbox id="only-uncounted" wire:model.live="onlyUncounted" :label="__('inventory.counts.only_uncounted')" />
|
||||
@endunless
|
||||
<p class="text-sm">{{ trans_choice('inventory.counts.uncounted', $this->uncountedCount(), ['count' => $this->uncountedCount()]) }}</p>
|
||||
@if (! $count->isConfirmed)
|
||||
@can('inventory.manage-counts')
|
||||
<x-button wire:click="confirmCount" icon="check-badge" :text="__('inventory.counts.confirm')" />
|
||||
@endcan
|
||||
@else
|
||||
@can('inventory.manage-counts')
|
||||
<a href="{{ route('inventory.counts.csv', $count->id) }}" class="inline-flex items-center gap-1 text-sm 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.download_csv') }}
|
||||
</a>
|
||||
@endcan
|
||||
@endif
|
||||
</div>
|
||||
|
||||
@forelse ($this->groupedLines() as $groupName => $lines)
|
||||
<section wire:key="count-group-{{ md5($groupName) }}" aria-label="{{ $groupName }}" class="space-y-2">
|
||||
<h2 class="text-lg font-semibold">{{ $groupName }}</h2>
|
||||
<ul class="divide-y divide-zinc-200 rounded-lg border border-zinc-300 dark:divide-zinc-700 dark:border-zinc-600">
|
||||
@foreach ($lines as $line)
|
||||
@php($difference = $line->differenceMilli())
|
||||
<li wire:key="count-line-{{ $line->itemId }}" class="flex flex-wrap items-end justify-between gap-3 p-3">
|
||||
<div class="min-w-0 flex-1 space-y-1">
|
||||
<p class="font-medium break-words">{{ $line->itemName }}</p>
|
||||
<p class="text-sm text-zinc-700 dark:text-zinc-300">
|
||||
@if ($line->isCounted())
|
||||
{{ __('inventory.counts.book', ['book' => $this->quantity((int) $line->bookMilli, $line->unit)]) }}
|
||||
· <span class="font-medium">{{ __('inventory.counts.difference', ['difference' => $this->signed((int) $difference, $line->unit)]) }}</span>
|
||||
@can('inventory.see-value')
|
||||
({{ $this->money($this->differenceValue($line)) }})
|
||||
@endcan
|
||||
· {{ $line->countedByName }}, {{ $this->dateLabel((string) $line->countedAt) }}
|
||||
@else
|
||||
<span class="inline-flex items-center gap-1">
|
||||
<x-icon name="question-mark-circle" class="size-4" aria-hidden="true" />
|
||||
{{ __('inventory.counts.not_counted') }}
|
||||
</span>
|
||||
· {{ __('inventory.counts.current', ['balance' => $this->quantity($line->currentBalanceMilli, $line->unit)]) }}
|
||||
@endif
|
||||
</p>
|
||||
</div>
|
||||
@if ($count->isConfirmed)
|
||||
<p class="text-sm font-medium">{{ $line->isCounted() ? $this->quantity((int) $line->countedMilli, $line->unit) : '–' }}</p>
|
||||
@else
|
||||
<div class="w-36">
|
||||
<label for="counted-{{ $line->itemId }}" class="block text-sm font-semibold text-zinc-700 dark:text-zinc-300">
|
||||
{{ __('inventory.counts.counted', ['unit' => __('inventory.units.'.$line->unit->value)]) }}
|
||||
<span class="sr-only">: {{ $line->itemName }}</span>
|
||||
</label>
|
||||
<input
|
||||
id="counted-{{ $line->itemId }}"
|
||||
type="text"
|
||||
inputmode="decimal"
|
||||
autocomplete="off"
|
||||
value="{{ $line->isCounted() ? StockQuantity::inputValue((int) $line->countedMilli) : '' }}"
|
||||
wire:change="saveCounted({{ $line->itemId }}, $event.target.value)"
|
||||
@error('counted.'.$line->itemId) aria-invalid="true" aria-describedby="counted-{{ $line->itemId }}-error" @enderror
|
||||
class="mt-1 h-10 w-full rounded-md border border-zinc-500 bg-white px-2 text-right text-zinc-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:border-zinc-400 dark:bg-zinc-900 dark:text-zinc-100"
|
||||
/>
|
||||
@error('counted.'.$line->itemId)
|
||||
<p id="counted-{{ $line->itemId }}-error" class="mt-1 text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
|
||||
@enderror
|
||||
</div>
|
||||
@endif
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</section>
|
||||
@empty
|
||||
<p class="text-sm">{{ __('inventory.counts.no_lines') }}</p>
|
||||
@endforelse
|
||||
|
||||
@can('inventory.manage-counts')
|
||||
<x-confirm-dialog id="confirm-count-dialog" :title="__('inventory.counts.confirm_title')">
|
||||
<p>{{ __('inventory.counts.confirm_text') }}</p>
|
||||
@if ($this->uncountedCount() > 0)
|
||||
<p class="mt-2 font-medium">{{ trans_choice('inventory.counts.confirm_uncounted', $this->uncountedCount(), ['count' => $this->uncountedCount()]) }}</p>
|
||||
@endif
|
||||
|
||||
<x-slot:actions>
|
||||
<x-button wire:click="confirm" :text="__('inventory.counts.confirm')" />
|
||||
</x-slot:actions>
|
||||
</x-confirm-dialog>
|
||||
@endcan
|
||||
</div>
|
||||
@@ -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