authorize('inventory.view'); $summary = $stock->findItem($item) ?? abort(404); $this->itemId = $summary->id; $this->purchaseDay = $clock->today()->format('Y-m-d'); $this->purchaseSupplier = (string) $summary->supplier; $this->message = (string) session('status', ''); } #[Computed] public function item(): StockItemSummary { return app(StockQueries::class)->findItem($this->itemId) ?? abort(404); } /** * @return list */ #[Computed] public function movements(): array { return app(StockQueries::class)->movementsOf($this->itemId, self::HISTORY_LIMIT); } public function recordPurchase(RecordStockPurchase $recordPurchase): void { $this->authorize('inventory.manage'); $this->resetErrorBag(); if (trim($this->purchaseTotal) === '' && trim($this->purchaseUnitPrice) === '') { $this->addError('purchaseTotal', __('inventory.errors.price_required')); return; } try { $recordPurchase->execute(new PurchaseData( $this->itemId, $this->purchaseDay, $this->purchaseQuantity, $this->purchaseTotal, $this->purchaseUnitPrice, $this->purchaseSupplier, $this->purchaseDocument, ), (int) auth()->id()); } catch (InvalidStockQuantity) { $this->addError('purchaseQuantity', __('inventory.errors.quantity')); return; } catch (InvalidStockCost) { $this->addError(trim($this->purchaseTotal) !== '' ? 'purchaseTotal' : 'purchaseUnitPrice', __('inventory.errors.cost')); return; } catch (InvalidStockDay) { $this->addError('purchaseDay', __('inventory.errors.day')); return; } catch (StockItemNotFound) { abort(404); } $this->refreshItem(); $this->message = __('inventory.purchase.saved', [ 'quantity' => $this->quantity((int) round((float) str_replace(',', '.', $this->purchaseQuantity) * 1000), $this->item->unit), 'balance' => $this->quantity($this->item->balanceMilli, $this->item->unit), 'cost' => $this->unitCost($this->item->averageCostHundredths), ]); $this->reset('purchaseQuantity', 'purchaseTotal', 'purchaseUnitPrice', 'purchaseDocument'); $this->js("document.getElementById('item-status')?.focus()"); } public function recordWriteOff(RecordStockWriteOff $recordWriteOff): void { $this->authorize('inventory.manage'); $this->resetErrorBag(); try { $result = $recordWriteOff->execute(new StockUseData($this->itemId, $this->writeOffQuantity, reason: $this->writeOffReason), (int) auth()->id()); } catch (InvalidStockQuantity) { $this->addError('writeOffQuantity', __('inventory.errors.quantity')); return; } catch (StockReasonRequired) { $this->addError('writeOffReason', __('inventory.errors.reason')); return; } catch (StockItemNotFound) { abort(404); } $this->refreshItem(); $this->message = __('inventory.write_off.saved', [ 'quantity' => $this->quantity($result->quantityMilli, $this->item->unit), 'balance' => $this->quantity($result->balanceMilli, $this->item->unit), ]); $this->reset('writeOffQuantity', 'writeOffReason'); $this->js("document.getElementById('item-status')?.focus()"); } public function recordUse(RecordStockUse $recordUse): void { $this->authorize('inventory.record-use'); $this->resetErrorBag(); try { $result = $recordUse->execute(new StockUseData($this->itemId, $this->useQuantity, reason: $this->useReason), (int) auth()->id()); } catch (InvalidStockQuantity) { $this->addError('useQuantity', __('inventory.errors.quantity')); return; } catch (StockReasonRequired) { $this->addError('useReason', __('inventory.errors.reason')); return; } catch (StockItemInactive) { $this->addError('useQuantity', __('inventory.errors.inactive')); return; } catch (StockItemNotFound) { abort(404); } $this->refreshItem(); $this->message = __('inventory.use.saved', [ 'quantity' => $this->quantity($result->quantityMilli, $this->item->unit), 'balance' => $this->quantity($result->balanceMilli, $this->item->unit), ]); $this->reset('useQuantity', 'useReason'); $this->js("document.getElementById('item-status')?.focus()"); } public function confirmReverse(int $movementId): void { $movement = $this->movement($movementId); abort_unless($this->mayReverse($movement), 403); $this->reverseMovementId = $movementId; $this->js("document.getElementById('reverse-dialog').showModal()"); } public function reverse(ReverseStockMovement $reverseMovement): void { $movement = $this->movement((int) $this->reverseMovementId); abort_unless($this->mayReverse($movement), 403); try { $reverseMovement->execute($movement->id, (int) auth()->id(), Gate::allows('inventory.reverse-any')); $this->refreshItem(); $this->message = __('inventory.item.reversed', ['balance' => $this->quantity($this->item->balanceMilli, $this->item->unit)]); } catch (StockMovementNotFound) { $this->message = __('inventory.errors.not_found'); } catch (StockMovementAlreadyReversed) { $this->message = __('inventory.errors.already_reversed'); } catch (StockMovementNotReversible) { $this->message = __('inventory.errors.not_reversible'); } catch (StockReversalNotAllowed) { $this->message = __('inventory.errors.reversal_not_allowed'); } $this->reverseMovementId = null; $this->refreshItem(); $this->js("document.getElementById('reverse-dialog').close(); document.getElementById('item-status').focus()"); } public function toggleActive(SetStockItemActive $setActive): void { $this->authorize('inventory.manage'); $active = ! $this->item->isActive; $setActive->execute($this->itemId, $active); $this->refreshItem(); $this->message = __($active ? 'inventory.item.activated' : 'inventory.item.deactivated'); } public function mayReverse(StockMovementSummary $movement): bool { if (! $movement->canBeReversed()) { return false; } if (Gate::allows('inventory.reverse-any')) { return true; } return Gate::allows('inventory.record-use') && $movement->kind->value === 'consumption' && $movement->userId === (int) auth()->id() && $movement->occurredOn === app(Clock::class)->today()->format('Y-m-d'); } public function targetText(StockMovementSummary $movement): string { $parts = []; if ($movement->taskId !== null) { $parts[] = __('inventory.item.target_task', ['id' => $movement->taskId]); } if ($movement->machineId !== null) { $parts[] = __('inventory.item.target_machine', ['id' => $movement->machineId]); } if ($movement->orderLineId !== null) { $parts[] = __('inventory.item.target_order'); } if ($movement->maintenanceRecordId !== null) { $parts[] = __('inventory.item.target_maintenance'); } if ($movement->inventoryCountId !== null) { $parts[] = __('inventory.item.target_count', ['id' => $movement->inventoryCountId]); } if ($movement->supplier !== null) { $parts[] = $movement->supplier; } if ($movement->documentNumber !== null) { $parts[] = __('inventory.item.document', ['number' => $movement->documentNumber]); } if ($movement->reason !== null) { $parts[] = $movement->reason; } if ($movement->reversesMovementId !== null) { $parts[] = __('inventory.item.reverses', ['number' => $movement->reversesMovementId]); } return $parts === [] ? '–' : implode(', ', $parts); } /** * @return list */ public function headers(): array { $headers = [ ['index' => 'time', 'label' => __('inventory.columns.time'), 'sortable' => false], ['index' => 'kind', 'label' => __('inventory.columns.kind'), 'sortable' => false], ['index' => 'quantity', 'label' => __('inventory.columns.quantity'), 'sortable' => false, 'align' => 'right'], ['index' => 'balance', 'label' => __('inventory.columns.balance_after'), 'sortable' => false, 'align' => 'right'], ['index' => 'user', 'label' => __('inventory.columns.user'), 'sortable' => false], ['index' => 'target', 'label' => __('inventory.columns.target'), 'sortable' => false], ]; if (Gate::allows('inventory.see-value')) { $headers[] = ['index' => 'value', 'label' => __('inventory.columns.value'), 'sortable' => false, 'align' => 'right']; } $headers[] = ['index' => 'actions', 'label' => __('inventory.columns.actions'), 'sortable' => false]; return $headers; } public function render(): mixed { return $this->view()->title($this->item->name); } private function movement(int $movementId): StockMovementSummary { $movement = app(StockQueries::class)->findMovement($movementId); abort_if($movement === null || $movement->itemId !== $this->itemId, 404); return $movement; } private function refreshItem(): void { unset($this->item, $this->movements); } }; ?>
@php($item = $this->item)
@if ($item->hasImage) {{ __('inventory.item.image_alt', ['name' => $item->name]) }} @endif

{{ __('inventory.groups.back') }}

{{ $item->name }}

@can('inventory.manage')
@endcan

{{ $message }}

{{ __('inventory.item.details') }}

{{ __('inventory.item.balance') }}
{{ $this->quantity($item->balanceMilli, $item->unit) }}
{{ __('inventory.columns.group') }}
{{ $item->groupName }}
{{ __('inventory.columns.reorder_level') }}
{{ $item->reorderLevelMilli === null ? '–' : $this->quantity($item->reorderLevelMilli, $item->unit) }}
{{ __('inventory.form.code') }}
{{ $item->code ?? '–' }}
{{ __('inventory.form.supplier') }}
{{ $item->supplier ?? '–' }}
@can('inventory.see-value')
{{ __('inventory.columns.average_cost') }}
{{ $this->unitCost($item->averageCostHundredths) }} / {{ __('inventory.units.'.$item->unit->value) }}
{{ __('inventory.columns.value') }}
{{ $this->money($item->valueCents) }}
@endcan
@if ($item->isNegative())

@endif
@php($unitLabel = __('inventory.units.'.$item->unit->value))
@can('inventory.manage')

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

{{ __('inventory.purchase.intro') }}

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

@endcan @if ($item->isActive) @can('inventory.record-use')

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

{{ __('inventory.use.intro') }}

@endcan @else

{{ __('inventory.item.inactive_note') }}

@endif

{{ __('inventory.item.history') }}

@if ($this->movements === [])

{{ __('inventory.item.history_none') }}

@else @if (count($this->movements) >= $this::HISTORY_LIMIT)

{{ __('inventory.item.history_limit', ['count' => $this::HISTORY_LIMIT]) }}

@endif @interact('column_time', $movement) {{ $this->dateLabel($movement->recordedAt) }} @if ($movement->occurredOn !== substr($movement->recordedAt, 0, 10)) {{ $this->dateLabel($movement->occurredOn) }} @endif @endinteract @interact('column_kind', $movement) {{ __('inventory.kinds.'.$movement->kind->value) }} @if ($movement->reversedByMovementId !== null) {{ __('inventory.item.reversed_by', ['number' => $movement->reversedByMovementId]) }} @endif @endinteract @interact('column_quantity', $movement) {{ ($movement->quantityMilli > 0 ? '+' : '').$this->quantity($movement->quantityMilli) }} @endinteract @interact('column_balance', $movement) {{ $this->quantity($movement->balanceAfterMilli) }} @endinteract @interact('column_user', $movement) {{ $movement->userName }} @endinteract @interact('column_target', $movement) {{ $this->targetText($movement) }} @endinteract @interact('column_value', $movement) {{ $this->money($movement->valueCents) }} @endinteract @interact('column_actions', $movement) @if ($this->mayReverse($movement)) @endif @endinteract @endif
@php($reverseMovement = $reverseMovementId === null ? null : collect($this->movements)->firstWhere('id', $reverseMovementId)) @if ($reverseMovement !== null)

{{ __('inventory.item.reverse_text', ['kind' => __('inventory.kinds.'.$reverseMovement->kind->value), 'quantity' => $this->quantity($reverseMovement->quantityMilli, $item->unit)]) }}

@endif