Files
Toiminnanohjaus/app/Modules/Jobs/Presentation/Livewire/⚡tasks-from-template.blade.php
T
petriandClaude Opus 5.5 b4f2f1cfaa Add the task calendar, holidays, workflow templates and customer removal
Task calendar (/jobs/calendar) replaces per-unit task Kanban columns
(K34): a two-week Gantt of tasks per person. Bars move, resize and
change person by dragging or through a keyboard dialog. Each task's
estimate is booked evenly over working days into task_bookings, and
what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves
to the next working day. Tasks get planned_from/planned_to on the form.

Holidays (/settings/holidays): Finnish public holidays calculated per
year, which the admin can take out of use, plus company days off.

Also includes the earlier uncommitted work: workflow templates with
adding tasks from a template, and removing customers no module refers to.

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

262 lines
12 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 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 and estimate for each.
*/
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}>
*/
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<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);
}
#[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]);
}
};
?>
<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>
<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>
<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="py-2 pl-1 text-left font-semibold">{{ __('jobs.tasks.fields.estimated_hours') }}</th>
</tr>
</thead>
<tbody>
@foreach ($template->steps as $index => $step)
<tr wire:key="row-{{ $template->id }}-{{ $index }}" class="border-b border-zinc-200 align-top dark:border-zinc-700">
<th scope="row" class="py-2 pr-2 text-left font-normal break-words">
<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">
<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="py-2 pl-1">
<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 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>
</tr>
@endforeach
</tbody>
</table>
</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>