Add stock items, groups and movements at a moving average cost

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:18:59 +02:00
co-authored by Claude Sonnet 5.5
parent 361a2974cf
commit 30132deb0e
81 changed files with 4968 additions and 115 deletions
@@ -0,0 +1,227 @@
<?php
use App\Modules\Inventory\Application\Contracts\StockQueries;
use App\Modules\Inventory\Application\DTOs\StockGroupSummary;
use App\Modules\Inventory\Application\DTOs\StockItemData;
use App\Modules\Inventory\Application\UseCases\AddStockItem;
use App\Modules\Inventory\Application\UseCases\ChangeStockItemImage;
use App\Modules\Inventory\Application\UseCases\UpdateStockItem;
use App\Modules\Inventory\Domain\Enums\StockUnit;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockImage;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockName;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockQuantity;
use App\Modules\Inventory\Domain\Exceptions\StockItemGroupNotFound;
use App\Modules\Inventory\Domain\Exceptions\StockItemNotFound;
use App\Modules\Inventory\Domain\Exceptions\StockNameTaken;
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use Livewire\WithFileUploads;
/**
* Add page (no {item}) and edit page (with {item}) of a stock item: group,
* name, unit, code, supplier, reorder level and an optional image.
* Management only.
*/
new class extends Component
{
use WithFileUploads;
public const int MAX_KILOBYTES = 12288;
#[Locked]
public ?int $itemId = null;
#[Locked]
public bool $hasImage = false;
public string $groupId = '';
public string $name = '';
public string $unit = 'piece';
public string $code = '';
public string $supplier = '';
public string $reorderLevel = '';
/**
* @var TemporaryUploadedFile|null
*/
public $image = null;
public bool $removeImage = false;
public function mount(StockQueries $stock, ?int $item = null): void
{
$this->authorize('inventory.manage');
if ($item === null) {
$this->groupId = (string) ($this->groups[0]->id ?? '');
return;
}
$summary = $stock->findItem($item) ?? abort(404);
$this->itemId = $summary->id;
$this->groupId = (string) $summary->groupId;
$this->name = $summary->name;
$this->unit = $summary->unit->value;
$this->code = (string) $summary->code;
$this->supplier = (string) $summary->supplier;
$this->reorderLevel = $summary->reorderLevelMilli === null ? '' : StockQuantity::inputValue($summary->reorderLevelMilli);
$this->hasImage = $summary->hasImage;
}
/**
* Groups in use, and the item's own group even when out of use.
*
* @return list<StockGroupSummary>
*/
#[Computed]
public function groups(): array
{
return array_values(array_filter(
app(StockQueries::class)->groups(),
fn (StockGroupSummary $group): bool => $group->isActive || (string) $group->id === $this->groupId,
));
}
public function save(AddStockItem $addItem, UpdateStockItem $updateItem, ChangeStockItemImage $changeImage): void
{
$this->authorize('inventory.manage');
$this->resetErrorBag();
$this->validate(
[
'groupId' => ['required', 'integer'],
'unit' => ['required', 'in:'.implode(',', array_column(StockUnit::cases(), 'value'))],
'image' => ['nullable', 'file', 'image', 'mimes:jpg,jpeg,png,webp,gif', 'max:'.self::MAX_KILOBYTES],
],
[
'groupId.required' => __('inventory.errors.group'),
'groupId.integer' => __('inventory.errors.group'),
'image.file' => __('inventory.errors.image'),
'image.image' => __('inventory.errors.image'),
'image.mimes' => __('inventory.errors.image'),
'image.max' => __('inventory.errors.image_too_large', ['size' => self::MAX_KILOBYTES / 1024]),
],
);
$data = new StockItemData((int) $this->groupId, $this->name, $this->unit, $this->code, $this->supplier, $this->reorderLevel);
try {
if ($this->itemId === null) {
$this->itemId = $addItem->execute($data);
$status = __('inventory.form.created', ['name' => trim($this->name)]);
} else {
$updateItem->execute($this->itemId, $data);
$status = __('inventory.form.updated', ['name' => trim($this->name)]);
}
if ($this->image instanceof TemporaryUploadedFile) {
$changeImage->execute($this->itemId, (string) $this->image->getRealPath());
} elseif ($this->removeImage) {
$changeImage->execute($this->itemId, null);
}
} catch (InvalidStockName) {
$this->addError('name', __('inventory.errors.name'));
return;
} catch (StockNameTaken) {
$this->addError('name', __('inventory.errors.name_taken'));
return;
} catch (InvalidStockQuantity) {
$this->addError('reorderLevel', __('inventory.errors.reorder_level'));
return;
} catch (StockItemGroupNotFound) {
$this->addError('groupId', __('inventory.errors.group'));
return;
} catch (InvalidStockImage) {
$this->addError('image', __('inventory.errors.image'));
return;
} catch (StockItemNotFound) {
abort(404);
}
session()->flash('status', $status);
$this->redirectRoute('inventory.items.show', $this->itemId, navigate: true);
}
public function heading(): string
{
return __($this->itemId === null ? 'inventory.form.create_title' : 'inventory.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>
@if ($this->groups === [])
<p class="text-sm">{{ __('inventory.form.no_groups') }} <x-link :href="route('inventory.groups')" navigate>{{ __('inventory.index.groups') }}</x-link></p>
@else
<form wire:submit="save" class="space-y-6" novalidate>
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native id="item-group" wire:model="groupId" :label="__('inventory.form.group')" :aria-label="__('inventory.form.group')">
@foreach ($this->groups as $group)
<option value="{{ $group->id }}">{{ $group->name }}</option>
@endforeach
</x-select.native>
<x-input id="item-name" wire:model="name" :label="__('inventory.form.name')" autocomplete="off" maxlength="150" required />
<x-select.native id="item-unit" wire:model="unit" :label="__('inventory.form.unit')" :aria-label="__('inventory.form.unit')">
@foreach (StockUnit::cases() as $option)
<option value="{{ $option->value }}">{{ __('inventory.units.'.$option->value) }}</option>
@endforeach
</x-select.native>
<x-input id="item-code" wire:model="code" :label="__('inventory.form.code')" :hint="__('inventory.form.code_hint')" autocomplete="off" maxlength="150" />
<x-input id="item-supplier" wire:model="supplier" :label="__('inventory.form.supplier')" autocomplete="off" maxlength="150" />
<x-input id="item-reorder" wire:model="reorderLevel" inputmode="decimal" :label="__('inventory.form.reorder_level')" :hint="__('inventory.form.reorder_level_hint')" autocomplete="off" />
<div class="space-y-2">
<label for="item-image" class="block text-sm font-semibold text-zinc-700 dark:text-zinc-300">{{ __('inventory.form.image') }}</label>
<p id="item-image-hint" class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('inventory.form.image_hint', ['size' => $this::MAX_KILOBYTES / 1024]) }}</p>
@if ($hasImage && $itemId !== null)
<img src="{{ route('inventory.items.image', [$itemId, 'thumbnail']) }}" alt="{{ __('inventory.form.current_image') }}" class="size-24 rounded-md border border-zinc-300 object-cover dark:border-zinc-600" />
<x-checkbox id="remove-image" wire:model="removeImage" :label="__('inventory.form.remove_image')" />
@endif
<input
type="file"
id="item-image"
wire:model="image"
accept="image/jpeg,image/png,image/webp,image/gif"
aria-describedby="item-image-hint @error('image') item-image-error @enderror"
@error('image') aria-invalid="true" @enderror
class="block w-full max-w-md rounded-md text-sm text-zinc-900 file:me-3 file:min-h-9 file:cursor-pointer file:rounded-md file:border-0 file:bg-zinc-200 file:px-3 file:py-2 file:font-medium file:text-zinc-900 hover:file:bg-zinc-300 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:text-zinc-100 dark:file:bg-zinc-700 dark:file:text-zinc-100 dark:hover:file:bg-zinc-600 dark:focus-visible:outline-blue-400"
/>
@error('image')
<p id="item-image-error" class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
</div>
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('inventory.form.save')" />
<x-button :href="$itemId === null ? route('inventory.index') : route('inventory.items.show', $itemId)" wire:navigate subtle color="black" :text="__('inventory.form.cancel')" />
</div>
</form>
@endif
</div>