*/ #[Locked] public array $serviceUnitIds = []; public string $templateId = ''; /** * One row per step of the chosen template, in step order. * * @var list */ public array $rows = []; public function mount(CustomerJobQueries $jobQueries, int $job): void { $details = $jobQueries->findDetails($job); abort_if($details === null, 404); $this->authorize('jobs.manage-tasks', $details->id); abort_unless($details->status->allowsTasks(), 404); $this->jobId = $details->id; $this->jobTitle = $details->number.' '.$details->name; $this->serviceUnitIds = array_map(fn (NamedId $unit): int => $unit->id, $details->serviceUnits); } /** * @return list */ #[Computed] public function templates(): array { return app(WorkflowTemplateQueries::class)->activeFor($this->serviceUnitIds); } /** * The templates as radio options; the description lists the steps. * * @return list */ #[Computed] public function options(): array { return array_map(fn (WorkflowTemplateDetails $template): array => [ 'label' => $this->labelOf($template), 'value' => (string) $template->id, 'description' => __('jobs.tasks.from_template.steps', [ 'steps' => implode(' → ', array_map( fn (WorkflowStepSummary $step): string => $step->workTypeId === null ? $step->title : $step->title.' ('.$step->workTypeName.')', $template->steps, )), ]), ], $this->templates); } #[Computed] public function selectedTemplate(): ?WorkflowTemplateDetails { foreach ($this->templates as $template) { if ((string) $template->id === $this->templateId) { return $template; } } return null; } /** * A new template gets empty rows for its steps. */ public function updatedTemplateId(): void { $this->resetErrorBag(); $this->rows = array_map(fn (): array => ['due_on' => '', 'estimated_hours' => ''], $this->selectedTemplate?->steps ?? []); } public function add(AddTasksFromWorkflowTemplate $addTasks): void { $this->authorize('jobs.manage-tasks', $this->jobId); $this->validate( [ 'templateId' => ['required', 'integer'], 'rows' => ['array'], 'rows.*.due_on' => ['nullable', 'date_format:Y-m-d'], 'rows.*.estimated_hours' => ['nullable', 'regex:/^\s*\d{1,5}([.,]\d{1,2})?\s*$/'], ], [ 'templateId.required' => __('jobs.tasks.from_template.choose'), 'templateId.integer' => __('jobs.tasks.from_template.not_available'), 'rows.*.due_on.date_format' => __('jobs.errors.invalid_date'), 'rows.*.estimated_hours.regex' => __('jobs.errors.invalid_hours'), ], ); $template = $this->selectedTemplate; $schedules = array_map(fn (array $row): TemplateTaskSchedule => new TemplateTaskSchedule( $row['due_on'] === '' ? null : $row['due_on'], trim($row['estimated_hours']) === '' ? null : $row['estimated_hours'], ), $this->rows); try { $count = $addTasks->execute($this->jobId, (int) $this->templateId, (int) auth()->id(), $schedules); } catch (CustomerJobNotFound) { abort(404); } catch (TasksNotAllowedYet) { $this->addError('templateId', __('jobs.tasks.not_allowed_yet')); return; } catch (InvalidEstimatedHours) { $this->addError('templateId', __('jobs.errors.invalid_hours')); return; } catch (WorkflowTemplateNotAvailable|InvalidTaskReference) { unset($this->templates, $this->options, $this->selectedTemplate); $this->addError('templateId', __('jobs.tasks.from_template.not_available')); return; } session()->flash('status', trans_choice('jobs.tasks.from_template.added', $count, [ 'count' => $count, 'name' => $template === null ? '' : $this->labelOf($template), ])); $this->redirect(route('jobs.show', $this->jobId).'#tasks', navigate: true); } public function render(): mixed { return $this->view()->title(__('jobs.tasks.from_template.title').' – '.$this->jobTitle); } private function labelOf(WorkflowTemplateDetails $template): string { return __('jobs.tasks.from_template.option', ['name' => $template->name, 'work_type' => $template->workTypeName]); } }; ?>

{{ __('jobs.tasks.from_template.title') }}

{{ __('jobs.tasks.job_label') }}: {{ $jobTitle }}

{{ __('jobs.tasks.from_template.intro') }}

@if ($this->options === []) @error('templateId') @enderror

{{ __('jobs.tasks.from_template.none') }}

@else
@if (($template = $this->selectedTemplate) !== null) {{-- An input grid is a form, so a native table rather than . --}}

{{ __('jobs.tasks.from_template.schedule_help') }}

@foreach ($template->steps as $index => $step) @endforeach
{{ __('jobs.tasks.from_template.table_caption', ['name' => $template->name]) }}
{{ __('jobs.tasks.from_template.task') }} {{ __('jobs.tasks.fields.due_on') }} {{ __('jobs.tasks.fields.estimated_hours') }}
{{ $step->title }} {{ $step->workTypeName }} @error('rows.'.$index.'.due_on')

{{ $message }}

@enderror
@error('rows.'.$index.'.estimated_hours')

{{ $message }}

@enderror
@endif
@endif