*/ public array $suggestionQuantities = []; #[Locked] public string $message = ''; public function mount(): void { $this->authorize('jobs.view-job', $this->jobId); $context = $this->context; abort_if($context !== null && $context->jobId !== $this->jobId, 404); foreach ($this->suggestions as $suggestion) { $this->suggestionQuantities[$suggestion->id] = StockQuantity::inputValue($suggestion->quantityMilli); } } #[Computed] public function context(): ?StockTaskContext { return app(StockTasks::class)->context($this->taskId); } /** * @return list */ #[Computed] public function movements(): array { return app(StockQueries::class)->taskMovements($this->taskId); } /** * @return list */ #[Computed] public function suggestions(): array { return app(StockQueries::class)->taskSuggestions($this->taskId); } /** * @return list */ #[Computed] public function results(): array { $term = trim($this->search); return mb_strlen($term) < 2 ? [] : array_slice(app(StockQueries::class)->items($term), 0, self::RESULT_LIMIT); } #[Computed] public function selectedItem(): ?StockItemSummary { return $this->selectedItemId === null ? null : app(StockQueries::class)->findItem($this->selectedItemId); } public function canRecord(): bool { $context = $this->context; return $context !== null && $context->usesStock && ! $context->isLocked && Gate::allows('inventory.record-use'); } public function selectItem(int $itemId): void { $this->authorizeRecording(); $this->selectedItemId = $itemId; $this->quantity = '1'; $this->search = ''; $this->resetErrorBag(); $this->js("document.getElementById('task-{$this->taskId}-material-quantity')?.focus()"); } public function clearItem(): void { $this->selectedItemId = null; $this->js("document.getElementById('task-{$this->taskId}-material-search')?.focus()"); } public function record(TaskMaterials $materials): void { $this->authorizeRecording(); $this->resetErrorBag(); if ($this->selectedItemId === null) { return; } try { $result = $materials->record($this->taskId, $this->selectedItemId, $this->quantity, (int) auth()->id()); } catch (InvalidStockQuantity) { $this->addError('quantity', __('inventory.errors.quantity')); return; } catch (StockItemInactive) { $this->addError('quantity', __('inventory.errors.inactive')); return; } catch (StockItemNotFound|StockTaskNotAvailable) { $this->message = __('inventory.errors.not_found'); return; } $unit = $this->selectedItem?->unit; $this->message = __('inventory.task.recorded', [ 'item' => $result->itemName, 'quantity' => $this->quantity($result->quantityMilli, $unit), 'balance' => $this->quantity($result->balanceMilli, $unit), ]); $this->selectedItemId = null; $this->quantity = '1'; unset($this->movements); $this->js("document.getElementById('task-{$this->taskId}-material-search')?.focus()"); } public function confirmSuggestion(int $suggestionId, TaskMaterials $materials): void { $this->authorizeRecording(); $this->resetErrorBag(); $suggestion = collect($this->suggestions)->firstWhere('id', $suggestionId); try { $result = $materials->confirm($this->taskId, $suggestionId, $this->suggestionQuantities[$suggestionId] ?? '', (int) auth()->id()); } catch (InvalidStockQuantity) { $this->addError('suggestionQuantities.'.$suggestionId, __('inventory.errors.quantity')); return; } catch (StockItemInactive) { $this->addError('suggestionQuantities.'.$suggestionId, __('inventory.errors.inactive')); return; } catch (StockItemNotFound|StockTaskNotAvailable|StockTaskSuggestionNotFound) { $this->message = __('inventory.errors.not_found'); unset($this->suggestions); return; } $this->message = __('inventory.task.recorded', [ 'item' => $result->itemName, 'quantity' => $this->quantity($result->quantityMilli, $suggestion?->unit), 'balance' => $this->quantity($result->balanceMilli, $suggestion?->unit), ]); unset($this->suggestions, $this->movements, $this->suggestionQuantities[$suggestionId]); $this->js("document.getElementById('task-{$this->taskId}-materials-status')?.focus()"); } public function dismissSuggestion(int $suggestionId, TaskMaterials $materials): void { $this->authorizeRecording(); $suggestion = collect($this->suggestions)->firstWhere('id', $suggestionId); try { $materials->dismiss($this->taskId, $suggestionId); } catch (StockTaskSuggestionNotFound) { // Already handled elsewhere: the list is refreshed below. } $this->message = __('inventory.task.dismissed', ['item' => $suggestion?->itemName ?? '']); unset($this->suggestions, $this->suggestionQuantities[$suggestionId]); $this->js("document.getElementById('task-{$this->taskId}-materials-status')?.focus()"); } public function undo(int $movementId, ReverseStockMovement $reverse): void { $this->authorize('inventory.record-use'); $movement = collect($this->movements)->firstWhere('id', $movementId); abort_if($movement === null || ! $this->mayUndo($movement), 403); try { $reverse->execute($movementId, (int) auth()->id(), Gate::allows('inventory.reverse-any')); $this->message = __('inventory.task.undone', ['item' => $movement->itemName, 'quantity' => $this->quantity(-$movement->quantityMilli, $movement->unit)]); } catch (StockMovementNotFound|StockMovementAlreadyReversed) { $this->message = __('inventory.errors.already_reversed'); } catch (StockReversalNotAllowed) { $this->message = __('inventory.errors.reversal_not_allowed'); } unset($this->movements); $this->js("document.getElementById('task-{$this->taskId}-materials-status')?.focus()"); } /** * Users undo their own use on the day it was recorded; management any use. */ public function mayUndo(StockMovementSummary $movement): bool { if (! $movement->canBeReversed() || $movement->kind->value !== 'consumption' || ! $this->canRecord()) { return false; } return Gate::allows('inventory.reverse-any') || ($movement->userId === (int) auth()->id() && $movement->occurredOn === app(Clock::class)->today()->format('Y-m-d')); } private function authorizeRecording(): void { $this->authorize('jobs.view-job', $this->jobId); abort_unless($this->canRecord(), 403); } }; ?>
@if (($this->context?->usesStock ?? false) || $this->movements !== [])

{{ __('inventory.task.title') }}

{{ $message }}

@if ($this->canRecord()) @if ($this->suggestions !== [])

{{ __('inventory.task.suggestions') }}

    @foreach ($this->suggestions as $suggestion)
  • @endforeach
@endif @if ($this->selectedItem !== null)

{{ $this->selectedItem->name }} ({{ __('inventory.task.balance', ['balance' => $this->quantity($this->selectedItem->balanceMilli, $this->selectedItem->unit)]) }})

@else
@if (mb_strlen(trim($search)) >= 2)

{{ trans_choice('inventory.task.results', count($this->results), ['count' => count($this->results)]) }}

    @foreach ($this->results as $result)
  • @endforeach
@endif
@endif @endif @if ($this->movements === [])

{{ __('inventory.task.none') }}

@else
    @foreach ($this->movements as $movement)
  • {{ $movement->itemName }} {{ $this->quantity(-$movement->quantityMilli, $movement->unit) }} · {{ __('inventory.kinds.'.$movement->kind->value) }} · {{ $movement->userName }}, {{ $this->dateLabel($movement->recordedAt) }} @if ($movement->reversedByMovementId !== null) · {{ __('inventory.task.undone_mark') }} @endif @if ($this->mayUndo($movement)) @endif
  • @endforeach
@endif
@endif