Record used material on tasks and suggest workflow steps' default materials
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Modules\Catalog\Application\DTOs\WorkflowStepData;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateData;
|
||||
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepMaterial;
|
||||
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNameTaken;
|
||||
@@ -24,16 +25,34 @@ class WorkflowTemplateForm extends Form
|
||||
public string $work_type_id = '';
|
||||
|
||||
/**
|
||||
* @var list<array{key: int, title: string, work_type_id: string, description: string}>
|
||||
* @var list<array{key: int, title: string, work_type_id: string, description: string, materials: list<array{key: int, stock_item_id: string, quantity: string}>}>
|
||||
*/
|
||||
public array $steps = [];
|
||||
|
||||
public int $nextKey = 1;
|
||||
|
||||
public function addStep(string $title = '', string $workTypeId = '', string $description = ''): int
|
||||
/**
|
||||
* @param list<array{stock_item_id: string, quantity: string}> $materials
|
||||
*/
|
||||
public function addStep(string $title = '', string $workTypeId = '', string $description = '', array $materials = []): int
|
||||
{
|
||||
$key = $this->nextKey++;
|
||||
$this->steps[] = ['key' => $key, 'title' => $title, 'work_type_id' => $workTypeId, 'description' => $description];
|
||||
$this->steps[] = ['key' => $key, 'title' => $title, 'work_type_id' => $workTypeId, 'description' => $description, 'materials' => []];
|
||||
|
||||
foreach ($materials as $material) {
|
||||
$this->addMaterial(count($this->steps) - 1, $material['stock_item_id'], $material['quantity']);
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a default material row to a step and returns its key.
|
||||
*/
|
||||
public function addMaterial(int $stepIndex, string $stockItemId = '', string $quantity = '1'): int
|
||||
{
|
||||
$key = $this->nextKey++;
|
||||
$this->steps[$stepIndex]['materials'][] = ['key' => $key, 'stock_item_id' => $stockItemId, 'quantity' => $quantity];
|
||||
|
||||
return $key;
|
||||
}
|
||||
@@ -60,6 +79,7 @@ class WorkflowTemplateForm extends Form
|
||||
$exception instanceof CatalogItemNotFound => ['work_type_id', 'catalog.errors.work_type_unavailable'],
|
||||
$exception instanceof WorkflowTemplateNeedsSteps => ['steps', 'catalog.workflows.errors.steps_required'],
|
||||
$exception instanceof InvalidWorkflowStepTitle => ['steps.'.($exception->stepIndex ?? 0).'.title', 'catalog.workflows.errors.invalid_step_title'],
|
||||
$exception instanceof InvalidWorkflowStepMaterial => ['steps.'.($exception->stepIndex ?? 0).'.materials.'.($exception->materialIndex ?? 0).'.quantity', 'catalog.workflows.errors.invalid_material'],
|
||||
$exception instanceof WorkflowStepWorkTypeNotAllowed => ['steps.'.$exception->stepIndex.'.work_type_id', 'catalog.workflows.errors.step_work_type_not_allowed'],
|
||||
default => throw $exception,
|
||||
};
|
||||
@@ -79,6 +99,9 @@ class WorkflowTemplateForm extends Form
|
||||
'steps.*.title' => ['required', 'string', 'max:255'],
|
||||
'steps.*.work_type_id' => ['nullable', 'integer'],
|
||||
'steps.*.description' => ['nullable', 'string', 'max:5000'],
|
||||
'steps.*.materials' => ['array'],
|
||||
'steps.*.materials.*.stock_item_id' => ['nullable', 'integer'],
|
||||
'steps.*.materials.*.quantity' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -98,6 +121,8 @@ class WorkflowTemplateForm extends Form
|
||||
'steps.*.title.max' => __('catalog.workflows.errors.invalid_step_title'),
|
||||
'steps.*.work_type_id.integer' => __('catalog.workflows.errors.step_work_type_not_allowed'),
|
||||
'steps.*.description.max' => __('catalog.workflows.errors.description_too_long'),
|
||||
'steps.*.materials.*.stock_item_id.integer' => __('catalog.workflows.errors.invalid_material'),
|
||||
'steps.*.materials.*.quantity.max' => __('catalog.workflows.errors.invalid_material'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ use App\Modules\Catalog\Application\UseCases\AddWorkflowTemplate;
|
||||
use App\Modules\Catalog\Application\UseCases\UpdateWorkflowTemplate;
|
||||
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
|
||||
use App\Modules\Catalog\Presentation\Livewire\Forms\WorkflowTemplateForm;
|
||||
use App\Modules\Inventory\Application\Contracts\StockQueries;
|
||||
use App\Modules\Inventory\Application\DTOs\StockItemSummary;
|
||||
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
||||
use Livewire\Attributes\Computed;
|
||||
use Livewire\Attributes\Locked;
|
||||
@@ -15,7 +17,8 @@ use Livewire\Component;
|
||||
/**
|
||||
* Add page and edit page (with {template}) of a workflow template: its name,
|
||||
* work type and steps. Steps are added, moved and removed with buttons, and
|
||||
* everything is saved at once.
|
||||
* everything is saved at once. A step whose work type's service unit uses
|
||||
* stock may have default materials, suggested on its tasks (spec 19.4).
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
@@ -54,7 +57,15 @@ new class extends Component
|
||||
$this->storedWorkTypeIds = [$details->workTypeId];
|
||||
|
||||
foreach ($details->steps as $step) {
|
||||
$this->form->addStep($step->title, $step->workTypeId === null ? '' : (string) $step->workTypeId, $step->description ?? '');
|
||||
$this->form->addStep(
|
||||
$step->title,
|
||||
$step->workTypeId === null ? '' : (string) $step->workTypeId,
|
||||
$step->description ?? '',
|
||||
array_map(fn ($material): array => [
|
||||
'stock_item_id' => (string) $material->stockItemId,
|
||||
'quantity' => rtrim(rtrim(number_format($material->quantityMilli / 1000, 3, ',', ''), '0'), ','),
|
||||
], $step->materials),
|
||||
);
|
||||
|
||||
if ($step->workTypeId !== null) {
|
||||
$this->storedWorkTypeIds[] = $step->workTypeId;
|
||||
@@ -87,6 +98,77 @@ new class extends Component
|
||||
return $workType === null ? [] : $this->groups($catalog->activeWorkTypesFor([$workType->serviceUnitId]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stock items in use, for default materials.
|
||||
*
|
||||
* @return list<StockItemSummary>
|
||||
*/
|
||||
#[Computed]
|
||||
public function stockItems(): array
|
||||
{
|
||||
return app(StockQueries::class)->items();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the step's work type (or the template's) belongs to a service
|
||||
* unit that uses stock; a shared work type follows the template's unit.
|
||||
*/
|
||||
public function stepUsesStock(int $index): bool
|
||||
{
|
||||
$catalog = app(CatalogQueries::class);
|
||||
$stockUnitIds = $catalog->stockServiceUnitIds();
|
||||
|
||||
if ($stockUnitIds === [] || $this->stockItems === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$templateWorkType = $this->form->work_type_id === '' ? null : $catalog->findWorkType((int) $this->form->work_type_id);
|
||||
$stepWorkTypeId = $this->form->steps[$index]['work_type_id'] ?? '';
|
||||
$workType = $stepWorkTypeId === '' ? $templateWorkType : $catalog->findWorkType((int) $stepWorkTypeId);
|
||||
|
||||
if ($workType === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$unitId = ($catalog->findServiceUnit($workType->serviceUnitId)?->isShared ?? false)
|
||||
? $templateWorkType?->serviceUnitId
|
||||
: $workType->serviceUnitId;
|
||||
|
||||
return $unitId !== null && in_array($unitId, $stockUnitIds, true);
|
||||
}
|
||||
|
||||
public function addMaterial(int $index): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
if (! isset($this->form->steps[$index])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$key = $this->form->addMaterial($index);
|
||||
$this->statusMessage = __('catalog.workflows.status.material_added', ['number' => $index + 1]);
|
||||
$this->js("document.getElementById('material-{$key}-item')?.focus()");
|
||||
}
|
||||
|
||||
public function removeMaterial(int $index, int $materialIndex): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
|
||||
$materials = array_values($this->form->steps[$index]['materials'] ?? []);
|
||||
|
||||
if (! isset($materials[$materialIndex])) {
|
||||
return;
|
||||
}
|
||||
|
||||
array_splice($materials, $materialIndex, 1);
|
||||
$this->form->steps[$index]['materials'] = $materials;
|
||||
$this->resetErrorBag();
|
||||
|
||||
$this->statusMessage = __('catalog.workflows.status.material_removed', ['number' => $index + 1]);
|
||||
$stepKey = $this->form->steps[$index]['key'];
|
||||
$this->js("document.getElementById('step-{$stepKey}-add-material')?.focus()");
|
||||
}
|
||||
|
||||
public function addStep(): void
|
||||
{
|
||||
$this->authorize('catalog.manage');
|
||||
@@ -303,6 +385,59 @@ new class extends Component
|
||||
resize-auto
|
||||
/>
|
||||
|
||||
@if ($this->stepUsesStock($index) || ($step['materials'] ?? []) !== [])
|
||||
<fieldset class="space-y-3">
|
||||
<legend class="text-sm font-semibold">{{ __('catalog.workflows.materials') }}</legend>
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.workflows.materials_help') }}</p>
|
||||
|
||||
@foreach ($step['materials'] ?? [] as $materialIndex => $material)
|
||||
<div wire:key="material-{{ $material['key'] }}-{{ $materialIndex }}" class="flex flex-wrap items-end gap-2">
|
||||
<div class="min-w-48 flex-1">
|
||||
<x-select.native
|
||||
id="material-{{ $material['key'] }}-item"
|
||||
wire:model="form.steps.{{ $index }}.materials.{{ $materialIndex }}.stock_item_id"
|
||||
:label="__('catalog.workflows.material_item')"
|
||||
:aria-label="__('catalog.workflows.material_item_named', ['step' => $index + 1, 'number' => $materialIndex + 1])"
|
||||
>
|
||||
<option value="">{{ __('catalog.workflows.choose_material') }}</option>
|
||||
@foreach ($this->stockItems as $stockItem)
|
||||
<option value="{{ $stockItem->id }}">{{ $stockItem->name }} ({{ __('inventory.units.'.$stockItem->unit->value) }})</option>
|
||||
@endforeach
|
||||
</x-select.native>
|
||||
</div>
|
||||
<div class="w-28">
|
||||
<x-input
|
||||
id="material-{{ $material['key'] }}-quantity"
|
||||
wire:model="form.steps.{{ $index }}.materials.{{ $materialIndex }}.quantity"
|
||||
inputmode="decimal"
|
||||
:label="__('catalog.workflows.material_quantity')"
|
||||
autocomplete="off"
|
||||
/>
|
||||
</div>
|
||||
<x-button
|
||||
wire:click="removeMaterial({{ $index }}, {{ $materialIndex }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="trash"
|
||||
:aria-label="__('catalog.workflows.remove_material_named', ['step' => $index + 1, 'number' => $materialIndex + 1])"
|
||||
/>
|
||||
</div>
|
||||
@endforeach
|
||||
|
||||
<x-button
|
||||
id="step-{{ $step['key'] }}-add-material"
|
||||
wire:click="addMaterial({{ $index }})"
|
||||
subtle
|
||||
color="black"
|
||||
sm
|
||||
icon="plus"
|
||||
:text="__('catalog.workflows.add_material')"
|
||||
:aria-label="__('catalog.workflows.add_material_named', ['number' => $index + 1])"
|
||||
/>
|
||||
</fieldset>
|
||||
@endif
|
||||
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<x-button
|
||||
id="step-{{ $step['key'] }}-up"
|
||||
|
||||
Reference in New Issue
Block a user