Files
Toiminnanohjaus/app/Modules/Jobs/Presentation/Livewire/⚡tasks-from-template.blade.php
petriandClaude Opus 5.5 18598cc08e Give tasks one assignee and members, and schedule template tasks to due dates
- A task now has one optional assignee (tasks.assignee_user_id), whose
  calendar row it is on and whose days its estimate books, and members
  (task_members) who see it among their own jobs and in the weekly sheet
  but get no bookings. A migration moves existing assignees over: the one
  with most booked time becomes the assignee, the others members.
- Adding tasks from a workflow template asks for each step's assignee and,
  by default, schedules tasks with a due date to end on it, booking the
  estimate backwards from the due date (not before today); what does not
  fit runs past the due date and is reported as late.
- The task calendar leaves quick sale tasks out of the unscheduled list,
  shows a bar's job details in a popover on hover and focus, and shows the
  day load on its own grid line.
- The template table stacks its cells on narrow screens to reflow at 320 px.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 19:01:11 +02:00

332 lines
16 KiB
PHP
Raw Permalink 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">
<h1 class="text-2xl font-semibold">{{ __('jobs.tasks.from_template.title') }}</h1>
<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
<p>{{ __('jobs.tasks.from_template.none') }}</p>
<x-button :href="route('jobs.show', $jobId)" wire:navigate subtle color="black" :text="__('jobs.tasks.from_template.back')" />
@else
<form wire:submit="add" class="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 border-zinc-200 dark:border-zinc-700">
<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]"
/>
@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"
/>
@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"
>
<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>