Files
Toiminnanohjaus/app/Modules/Jobs/Presentation/Livewire/⚡tasks-from-template.blade.php
T
petriandClaude Opus 5.5 de168619b3 Card the remaining form pages in the new look
The tasks-from-template, expense, stock item, workflow template and task
hour forms sat inside @if blocks, so the earlier pass left them as bare
fields on the page background. They are now cards; the template table has
the indigo header row and primary focus rings, the expense billing fieldset
and workflow steps get an inner surface instead of a nested card, locked-job
notices are amber, and the item image picker button uses the primary colour.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 11:15:09 +02:00

332 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
use App\Modules\Catalog\Application\DTOs\WorkflowStepSummary;
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateDetails;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\DTOs\NamedId;
use App\Modules\Jobs\Application\DTOs\TemplateTaskSchedule;
use App\Modules\Jobs\Application\UseCases\AddTasksFromWorkflowTemplate;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
use App\Modules\Jobs\Domain\Exceptions\WorkflowTemplateNotAvailable;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Domain\Enums\Role;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Adds a job's tasks from a workflow template (työnkulkupohja): one task per
* step. Once a template is chosen, a table of its tasks takes an optional
* due date, estimate and assignee (the adder by default) for each; tasks
* with a due date are scheduled to end on it unless that is turned off.
*/
new class extends Component
{
#[Locked]
public int $jobId;
#[Locked]
public string $jobTitle = '';
/** @var list<int> */
#[Locked]
public array $serviceUnitIds = [];
public string $templateId = '';
/**
* One row per step of the chosen template, in step order.
*
* @var list<array{due_on: string, estimated_hours: string, assignee_user_id: string}>
*/
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<WorkflowTemplateDetails>
*/
#[Computed]
public function templates(): array
{
return app(WorkflowTemplateQueries::class)->activeFor($this->serviceUnitIds);
}
/**
* The templates as radio options; the description lists the steps.
*
* @return list<array{label: string, value: string, description: string}>
*/
#[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<array{value: int, label: string}>
*/
#[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]);
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<div class="space-y-1">
<x-page-title icon="queue-list">{{ __('jobs.tasks.from_template.title') }}</x-page-title>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.job_label') }}: {{ $jobTitle }}</p>
</div>
<p class="text-sm">{{ __('jobs.tasks.from_template.intro') }}</p>
@if ($this->options === [])
@error('templateId')
<p role="alert" class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
<x-empty-state icon="queue-list">{{ __('jobs.tasks.from_template.none') }}</x-empty-state>
<x-button :href="route('jobs.show', $jobId)" wire:navigate subtle color="black" :text="__('jobs.tasks.from_template.back')" />
@else
<form wire:submit="add" class="card space-y-6" novalidate>
<x-radio.group
id="template"
wire:model.live="templateId"
:label="__('jobs.tasks.from_template.template')"
:options="$this->options"
required
/>
@if (($template = $this->selectedTemplate) !== null)
{{-- An input grid is a form, so a native table rather than <x-table>. --}}
<div class="space-y-2">
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.from_template.schedule_help') }}</p>
{{-- Below sm the rows stack (each cell on its own line, with its own visible label) so that the page reflows at 320 px. --}}
<table class="w-full border-collapse text-sm">
<caption class="mb-2 text-left font-medium">{{ __('jobs.tasks.from_template.table_caption', ['name' => $template->name]) }}</caption>
<thead class="max-sm:hidden">
<tr class="border-b-2 border-primary-200 text-primary-900 dark:border-primary-800 dark:text-primary-100">
<th scope="col" class="py-2 pr-2 text-left font-semibold">{{ __('jobs.tasks.from_template.task') }}</th>
<th scope="col" class="px-1 py-2 text-left font-semibold">{{ __('jobs.tasks.fields.due_on') }}</th>
<th scope="col" class="px-1 py-2 text-left font-semibold">{{ __('jobs.tasks.fields.estimated_hours') }}</th>
<th scope="col" class="py-2 pl-1 text-left font-semibold">{{ __('jobs.tasks.fields.assignee') }}</th>
</tr>
</thead>
<tbody class="max-sm:block">
@foreach ($template->steps as $index => $step)
<tr wire:key="row-{{ $template->id }}-{{ $index }}" class="border-b border-zinc-200 align-top max-sm:block max-sm:py-2 dark:border-zinc-700">
<th scope="row" class="py-2 pr-2 text-left font-normal break-words max-sm:block max-sm:py-0">
<span class="font-medium">{{ $step->title }}</span>
<span class="block text-xs text-zinc-600 dark:text-zinc-300">{{ $step->workTypeName }}</span>
</th>
<td class="px-1 py-2 max-sm:block max-sm:px-0 max-sm:py-1">
<span class="block text-xs font-medium sm:hidden" aria-hidden="true">{{ __('jobs.tasks.fields.due_on') }}</span>
<input
type="date"
id="row-{{ $index }}-due-on"
wire:model="rows.{{ $index }}.due_on"
aria-label="{{ __('jobs.tasks.from_template.due_on_of', ['title' => $step->title]) }}"
@error('rows.'.$index.'.due_on') aria-invalid="true" aria-describedby="row-{{ $index }}-due-on-error" @enderror
class="w-full min-w-0 rounded-md border border-zinc-500 bg-white px-1 py-1 text-sm dark:border-zinc-400 dark:bg-zinc-800 dark:[color-scheme:dark] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600 dark:focus-visible:outline-primary-300"
/>
@error('rows.'.$index.'.due_on')
<p id="row-{{ $index }}-due-on-error" class="mt-1 text-xs text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
</td>
<td class="px-1 py-2 max-sm:block max-sm:px-0 max-sm:py-1">
<span class="block text-xs font-medium sm:hidden" aria-hidden="true">{{ __('jobs.tasks.fields.estimated_hours') }}</span>
<input
type="text"
inputmode="decimal"
autocomplete="off"
id="row-{{ $index }}-hours"
wire:model="rows.{{ $index }}.estimated_hours"
aria-label="{{ __('jobs.tasks.from_template.hours_of', ['title' => $step->title]) }}"
@error('rows.'.$index.'.estimated_hours') aria-invalid="true" aria-describedby="row-{{ $index }}-hours-error" @enderror
class="w-16 rounded-md border max-sm:w-full border-zinc-500 bg-white px-2 py-1 text-sm dark:border-zinc-400 dark:bg-zinc-800 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600 dark:focus-visible:outline-primary-300"
/>
@error('rows.'.$index.'.estimated_hours')
<p id="row-{{ $index }}-hours-error" class="mt-1 text-xs text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
</td>
<td class="py-2 pl-1 max-sm:block max-sm:py-1 max-sm:pl-0">
<span class="block text-xs font-medium sm:hidden" aria-hidden="true">{{ __('jobs.tasks.fields.assignee') }}</span>
<select
id="row-{{ $index }}-assignee"
wire:model="rows.{{ $index }}.assignee_user_id"
aria-label="{{ __('jobs.tasks.from_template.assignee_of', ['title' => $step->title]) }}"
class="w-full min-w-0 rounded-md border border-zinc-500 bg-white px-1 py-1 text-sm dark:border-zinc-400 dark:bg-zinc-800 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600 dark:focus-visible:outline-primary-300"
>
<option value="">{{ __('jobs.calendar.no_assignee') }}</option>
@foreach ($this->userOptions as $user)
<option value="{{ $user['value'] }}">{{ $user['label'] }}</option>
@endforeach
</select>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="space-y-1 pt-2">
<x-checkbox
id="schedule-to-due-dates"
wire:model="scheduleToDueDates"
aria-describedby="schedule-to-due-dates-hint"
:label="__('jobs.tasks.from_template.schedule_to_due')"
/>
<p id="schedule-to-due-dates-hint" class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.from_template.schedule_to_due_hint') }}</p>
</div>
</div>
@endif
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('jobs.tasks.from_template.submit')" />
<x-button :href="route('jobs.show', $jobId)" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
</div>
</form>
@endif
</div>