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 */ #[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()); } }; ?>
{{ $this->heading() }} @if ($this->groups === [])

{{ __('inventory.form.no_groups') }} {{ __('inventory.index.groups') }}

@else
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}} @foreach ($this->groups as $group) @endforeach @foreach (StockUnit::cases() as $option) @endforeach

{{ __('inventory.form.image_hint', ['size' => $this::MAX_KILOBYTES / 1024]) }}

@if ($hasImage && $itemId !== null) {{ __('inventory.form.current_image') }} @endif @error('image')

{{ $message }}

@enderror
@endif