}> */ public array $steps = []; public int $nextKey = 1; #[Locked] public string $message = ''; public function mount(): void { $this->authorize('machines.manage'); foreach (app(ProductionProcessQueries::class)->processOf($this->productId) as $step) { $this->steps[] = [ 'key' => $this->nextKey++, 'machine_id' => (string) ($step->machineId ?? ''), 'duration' => rtrim(rtrim(number_format($step->durationQuarters / 4, 2, ',', ''), '0'), ','), 'work_type_id' => (string) $step->workTypeId, 'materials' => array_map(fn (array $material): array => [ 'key' => $this->nextKey++, 'stock_item_id' => (string) $material['stockItemId'], 'quantity' => rtrim(rtrim(number_format($material['quantityMilli'] / 1000, 3, ',', ''), '0'), ','), ], $step->materials), ]; } } public function isAvailable(): bool { return app(CatalogQueries::class)->stockServiceUnitIds() !== []; } /** * Machines of the product's work type's service unit (any unit using * stock when the product has none or a shared one). * * @return list */ #[Computed] public function machines(): array { $catalog = app(CatalogQueries::class); $product = app(PriceListQueries::class)->findProduct($this->productId); $workType = $product?->workTypeId === null ? null : $catalog->findWorkType($product->workTypeId); $unit = $workType === null ? null : $catalog->findServiceUnit($workType->serviceUnitId); $units = $unit === null || $unit->isShared ? $catalog->stockServiceUnitIds() : [$unit->id]; return app(MachineQueries::class)->machinesFor($units); } /** * @return list */ #[Computed] public function workTypes(): array { $types = []; foreach (app(CatalogQueries::class)->activeWorkTypesFor(app(CatalogQueries::class)->stockServiceUnitIds()) as $node) { foreach ($node->workTypes as $workType) { $types[] = $workType; } foreach ($node->services as $service) { foreach ($service->workTypes as $workType) { $types[] = $workType; } } } return $types; } /** * @return list */ #[Computed] public function stockItems(): array { return app(StockQueries::class)->items(); } public function addStep(): void { $this->authorize('machines.manage'); $product = app(PriceListQueries::class)->findProduct($this->productId); $key = $this->nextKey++; $this->steps[] = ['key' => $key, 'machine_id' => (string) ($this->machines[0]->id ?? ''), 'duration' => '0', 'work_type_id' => (string) ($product->workTypeId ?? ($this->workTypes[0]->id ?? '')), 'materials' => []]; $this->message = __('machines.process.step_added', ['number' => count($this->steps)]); $this->js("document.getElementById('process-step-{$key}-machine')?.focus()"); } public function removeStep(int $index): void { $this->authorize('machines.manage'); if (isset($this->steps[$index])) { array_splice($this->steps, $index, 1); $this->message = __('machines.process.step_removed', ['number' => $index + 1]); $this->js("document.getElementById('process-add-step')?.focus()"); } } public function moveStep(int $index, string $direction): void { $this->authorize('machines.manage'); $target = $direction === 'up' ? $index - 1 : $index + 1; if (! isset($this->steps[$index], $this->steps[$target])) { return; } [$this->steps[$index], $this->steps[$target]] = [$this->steps[$target], $this->steps[$index]]; $this->message = __('machines.process.step_moved', ['from' => $index + 1, 'to' => $target + 1]); $key = $this->steps[$target]['key']; $this->js("document.getElementById('process-step-{$key}-{$direction}')?.focus()"); } public function addMaterial(int $index): void { $this->authorize('machines.manage'); if (isset($this->steps[$index])) { $key = $this->nextKey++; $this->steps[$index]['materials'][] = ['key' => $key, 'stock_item_id' => '', 'quantity' => '1']; $this->js("document.getElementById('process-material-{$key}-item')?.focus()"); } } public function removeMaterial(int $index, int $materialIndex): void { $this->authorize('machines.manage'); if (isset($this->steps[$index]['materials'][$materialIndex])) { array_splice($this->steps[$index]['materials'], $materialIndex, 1); $key = $this->steps[$index]['key']; $this->js("document.getElementById('process-step-{$key}-add-material')?.focus()"); } } public function save(SaveProductionProcess $saveProcess): void { $this->authorize('machines.manage'); $this->resetErrorBag(); $data = []; foreach ($this->steps as $step) { $data[] = new ProductionStepData( $step['machine_id'] === '' ? null : (int) $step['machine_id'], $step['duration'], (int) $step['work_type_id'], array_values(array_map( fn (array $material): array => ['stockItemId' => (int) $material['stock_item_id'], 'quantity' => $material['quantity']], array_filter($step['materials'], fn (array $material): bool => $material['stock_item_id'] !== ''), )), ); } try { $saveProcess->execute($this->productId, $data); } catch (InvalidProductionStep) { $this->addError('steps', __('machines.process.errors.invalid')); return; } catch (ProductionMachineNotAllowed) { $this->addError('steps', __('machines.process.errors.machine')); return; } $this->message = $this->steps === [] ? __('machines.process.removed') : __('machines.process.saved'); } }; ?>
@if ($this->isAvailable())

{{ __('machines.process.title') }}

{{ __('machines.process.intro') }}

{{ $message }}

@error('steps') @enderror
    @foreach ($steps as $index => $step)
  1. {{ __('machines.process.step', ['number' => $index + 1]) }}
    {{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}} @foreach ($this->machines as $machine) @endforeach @foreach ($this->workTypes as $workType) @endforeach

    {{ __('machines.process.materials') }}

    @foreach ($step['materials'] as $materialIndex => $material)
    @foreach ($this->stockItems as $stockItem) @endforeach
    @endforeach
  2. @endforeach
@endif