*/ #[Locked] public array $serviceUnitIds = []; public string $templateId = ''; /** * One row per step of the chosen template, in step order. * * @var list */ public array $rows = []; public bool $scheduleToDueDates = true; 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); } /** * @return list */ #[Computed] public function userOptions(): array { return array_map(fn ($user): array => [ 'value' => $user->id, 'label' => $user->role === Role::Subcontractor ? $user->name.' ('.__('users.roles.subcontractor').')' : $user->name, ], app(UserAccountQueries::class)->activeUserOptions()); } #[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' => '', 'assignee_user_id' => (string) auth()->id()], $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*$/'], 'rows.*.assignee_user_id' => ['nullable', 'integer'], 'scheduleToDueDates' => ['boolean'], ], [ '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'], $row['assignee_user_id'] === '' ? null : (int) $row['assignee_user_id'], ), $this->rows); try { $result = $addTasks->execute($this->jobId, (int) $this->templateId, (int) auth()->id(), $schedules, $this->scheduleToDueDates); } 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 (InvalidTaskReference $exception) { if ($exception->field === 'assignee') { unset($this->userOptions); $this->addError('templateId', __('jobs.errors.invalid_reference')); return; } unset($this->templates, $this->options, $this->selectedTemplate); $this->addError('templateId', __('jobs.tasks.from_template.not_available')); return; } catch (WorkflowTemplateNotAvailable) { unset($this->templates, $this->options, $this->selectedTemplate); $this->addError('templateId', __('jobs.tasks.from_template.not_available')); return; } $message = trans_choice('jobs.tasks.from_template.added', $result->added, [ 'count' => $result->added, 'name' => $template === null ? '' : $this->labelOf($template), ]); if ($result->scheduled > 0) { $message .= ' '.trans_choice('jobs.tasks.from_template.scheduled', $result->scheduled, ['count' => $result->scheduled]); } if ($result->late > 0) { $message .= ' '.trans_choice('jobs.tasks.from_template.late', $result->late, ['count' => $result->late]); } session()->flash('status', $message); $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') }}

{{-- Below sm the rows stack (each cell on its own line, with its own visible label) so that the page reflows at 320 px. --}} @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') }} {{ __('jobs.tasks.fields.assignee') }}
{{ $step->title }} {{ $step->workTypeName }} @error('rows.'.$index.'.due_on')

{{ $message }}

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

{{ $message }}

@enderror

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

@endif
@endif