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]>
This commit is contained in:
2026-11-24 18:13:11 +02:00
co-authored by Claude Opus 5.5
parent dfe4941f02
commit b4f2f1cfaa
134 changed files with 6930 additions and 45 deletions
@@ -0,0 +1,103 @@
<?php
namespace App\Modules\Catalog\Presentation\Livewire\Forms;
use App\Modules\Catalog\Application\DTOs\WorkflowStepData;
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateData;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Domain\Exceptions\InvalidCatalogName;
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNameTaken;
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNeedsSteps;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Form;
/**
* Name, work type and steps of a workflow template. Each step has a `key`
* that stays the same while steps are moved, for wire:key and element ids.
*/
class WorkflowTemplateForm extends Form
{
public string $name = '';
public string $work_type_id = '';
/**
* @var list<array{key: int, title: string, work_type_id: string, description: string}>
*/
public array $steps = [];
public int $nextKey = 1;
public function addStep(string $title = '', string $workTypeId = '', string $description = ''): int
{
$key = $this->nextKey++;
$this->steps[] = ['key' => $key, 'title' => $title, 'work_type_id' => $workTypeId, 'description' => $description];
return $key;
}
public function toData(): WorkflowTemplateData
{
$this->validate();
return new WorkflowTemplateData(
$this->name,
(int) $this->work_type_id,
array_map(fn (array $step): WorkflowStepData => WorkflowStepData::fromArray($step), $this->steps),
);
}
/**
* Shows a broken business rule as an error on the field it concerns.
*/
public function showDomainError(DomainException $exception): void
{
[$field, $key] = match (true) {
$exception instanceof InvalidCatalogName => ['name', 'catalog.errors.invalid_name'],
$exception instanceof WorkflowTemplateNameTaken => ['name', 'catalog.workflows.errors.name_taken'],
$exception instanceof CatalogItemNotFound => ['work_type_id', 'catalog.errors.work_type_unavailable'],
$exception instanceof WorkflowTemplateNeedsSteps => ['steps', 'catalog.workflows.errors.steps_required'],
$exception instanceof InvalidWorkflowStepTitle => ['steps.'.($exception->stepIndex ?? 0).'.title', 'catalog.workflows.errors.invalid_step_title'],
$exception instanceof WorkflowStepWorkTypeNotAllowed => ['steps.'.$exception->stepIndex.'.work_type_id', 'catalog.workflows.errors.step_work_type_not_allowed'],
default => throw $exception,
};
$this->addError($field, __($key));
}
/**
* @return array<string, array<int, string>>
*/
protected function rules(): array
{
return [
'name' => ['required', 'string', 'max:100'],
'work_type_id' => ['required', 'integer'],
'steps' => ['required', 'array', 'min:1'],
'steps.*.title' => ['required', 'string', 'max:255'],
'steps.*.work_type_id' => ['nullable', 'integer'],
'steps.*.description' => ['nullable', 'string', 'max:5000'],
];
}
/**
* @return array<string, string>
*/
protected function messages(): array
{
return [
'name.required' => __('catalog.errors.name_required'),
'name.max' => __('catalog.errors.invalid_name'),
'work_type_id.required' => __('catalog.workflows.errors.work_type_required'),
'work_type_id.integer' => __('catalog.errors.work_type_unavailable'),
'steps.required' => __('catalog.workflows.errors.steps_required'),
'steps.min' => __('catalog.workflows.errors.steps_required'),
'steps.*.title.required' => __('catalog.workflows.errors.step_title_required'),
'steps.*.title.max' => __('catalog.workflows.errors.invalid_step_title'),
'steps.*.work_type_id.integer' => __('catalog.workflows.errors.step_work_type_not_allowed'),
'steps.*.description.max' => __('catalog.workflows.errors.description_too_long'),
];
}
}
@@ -122,6 +122,7 @@ new class extends Component
<div class="space-y-1 text-sm">
<p class="text-zinc-600 dark:text-zinc-300">{{ __('catalog.intro') }}</p>
<p><x-link :href="route('catalog.prices')" navigate>{{ __('catalog.prices_link') }}</x-link></p>
<p><x-link :href="route('catalog.workflow-templates')" navigate>{{ __('catalog.workflows_link') }}</x-link></p>
</div>
@if (session('status'))
@@ -0,0 +1,353 @@
<?php
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
use App\Modules\Catalog\Application\DTOs\WorkTypeSummary;
use App\Modules\Catalog\Application\UseCases\AddWorkflowTemplate;
use App\Modules\Catalog\Application\UseCases\UpdateWorkflowTemplate;
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
use App\Modules\Catalog\Presentation\Livewire\Forms\WorkflowTemplateForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Add page and edit page (with {template}) of a workflow template: its name,
* work type and steps. Steps are added, moved and removed with buttons, and
* everything is saved at once.
*/
new class extends Component
{
#[Locked]
public ?int $templateId = null;
/**
* Work types the stored template uses, offered even if out of use.
*
* @var list<int>
*/
#[Locked]
public array $storedWorkTypeIds = [];
public WorkflowTemplateForm $form;
public string $statusMessage = '';
public function mount(WorkflowTemplateQueries $templates, ?int $template = null): void
{
$this->authorize('catalog.manage');
if ($template === null) {
$this->form->addStep();
return;
}
$details = $templates->find($template);
abort_if($details === null, 404);
$this->templateId = $details->id;
$this->form->name = $details->name;
$this->form->work_type_id = (string) $details->workTypeId;
$this->storedWorkTypeIds = [$details->workTypeId];
foreach ($details->steps as $step) {
$this->form->addStep($step->title, $step->workTypeId === null ? '' : (string) $step->workTypeId, $step->description ?? '');
if ($step->workTypeId !== null) {
$this->storedWorkTypeIds[] = $step->workTypeId;
}
}
}
/**
* Work types for the template, as optgroups "Unit" or "Unit – Service".
*
* @return array<string, list<WorkTypeSummary>>
*/
#[Computed]
public function workTypeGroups(): array
{
return $this->groups(app(CatalogQueries::class)->tree(includeInactive: false));
}
/**
* Work types a step can have: those of the template work type's unit and the shared ones.
*
* @return array<string, list<WorkTypeSummary>>
*/
#[Computed]
public function stepWorkTypeGroups(): array
{
$catalog = app(CatalogQueries::class);
$workType = $this->form->work_type_id === '' ? null : $catalog->findWorkType((int) $this->form->work_type_id);
return $workType === null ? [] : $this->groups($catalog->activeWorkTypesFor([$workType->serviceUnitId]));
}
public function addStep(): void
{
$this->authorize('catalog.manage');
$key = $this->form->addStep();
$this->statusMessage = __('catalog.workflows.status.step_added', ['number' => count($this->form->steps)]);
$this->js("document.getElementById('step-{$key}-title')?.focus()");
}
public function moveStep(int $index, string $direction): void
{
$this->authorize('catalog.manage');
$steps = array_values($this->form->steps);
$target = $direction === 'up' ? $index - 1 : $index + 1;
if (! isset($steps[$index])) {
return;
}
$key = $steps[$index]['key'];
if (! isset($steps[$target])) {
$this->statusMessage = __($direction === 'up' ? 'catalog.workflows.status.already_first' : 'catalog.workflows.status.already_last', ['number' => $index + 1]);
return;
}
[$steps[$index], $steps[$target]] = [$steps[$target], $steps[$index]];
$this->form->steps = $steps;
$this->resetErrorBag();
$this->statusMessage = __('catalog.workflows.status.step_moved', ['from' => $index + 1, 'to' => $target + 1]);
$this->js("document.getElementById('step-{$key}-{$direction}')?.focus()");
}
public function removeStep(int $index): void
{
$this->authorize('catalog.manage');
$steps = array_values($this->form->steps);
if (! isset($steps[$index]) || count($steps) === 1) {
return;
}
array_splice($steps, $index, 1);
$this->form->steps = $steps;
$this->resetErrorBag();
$this->statusMessage = __('catalog.workflows.status.step_removed', ['number' => $index + 1]);
// Focus the step that took its place, the new last one, or the add button.
$next = $steps[$index] ?? $steps[count($steps) - 1];
$this->js(count($steps) > 1
? "document.getElementById('step-{$next['key']}-remove')?.focus()"
: "document.getElementById('add-step')?.focus()");
}
public function save(AddWorkflowTemplate $add, UpdateWorkflowTemplate $update): void
{
$this->authorize('catalog.manage');
$data = $this->form->toData();
try {
if ($this->templateId === null) {
$add->execute($data);
session()->flash('status', __('catalog.status.created', ['name' => trim($data->name)]));
} else {
$update->execute($this->templateId, $data);
session()->flash('status', __('catalog.status.updated', ['name' => trim($data->name)]));
}
} catch (WorkflowTemplateNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
$this->redirectRoute('catalog.workflow-templates', navigate: true);
}
public function heading(): string
{
return __($this->templateId === null ? 'catalog.workflows.create_title' : 'catalog.workflows.edit_title');
}
public function render(): mixed
{
return $this->view()->title($this->heading());
}
/**
* @param list<\App\Modules\Catalog\Application\DTOs\ServiceUnitNode> $tree
* @return array<string, list<WorkTypeSummary>>
*/
private function groups(array $tree): array
{
$groups = [];
$offered = [];
foreach ($tree as $node) {
foreach ($node->services as $serviceNode) {
if ($serviceNode->workTypes !== []) {
$groups[$node->serviceUnit->name.' – '.$serviceNode->service->name] = $serviceNode->workTypes;
}
}
if ($node->workTypes !== []) {
$groups[$node->serviceUnit->name] = $node->workTypes;
}
}
foreach ($groups as $workTypes) {
foreach ($workTypes as $workType) {
$offered[] = $workType->id;
}
}
// Keep the stored choices, even when they are out of use now.
$catalog = app(CatalogQueries::class);
$inactive = [];
foreach (array_unique($this->storedWorkTypeIds) as $id) {
if (! in_array($id, $offered, true) && ($workType = $catalog->findWorkType($id)) !== null) {
$inactive[] = $workType;
}
}
if ($inactive !== []) {
$groups[__('catalog.inactive')] = $inactive;
}
return $groups;
}
};
?>
<div class="mx-auto max-w-3xl space-y-6 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.name" :label="__('catalog.fields.name')" :hint="__('catalog.workflows.name_help')" autocomplete="off" required />
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native
wire:model.live="form.work_type_id"
:label="__('catalog.workflows.work_type')"
:aria-label="__('catalog.workflows.work_type')"
:hint="__('catalog.workflows.work_type_help')"
required
>
<option value="">{{ __('catalog.workflows.choose_work_type') }}</option>
@foreach ($this->workTypeGroups as $group => $workTypes)
<optgroup label="{{ $group }}">
@foreach ($workTypes as $workType)
<option wire:key="work-type-option-{{ $workType->id }}" value="{{ $workType->id }}">{{ $workType->name }}</option>
@endforeach
</optgroup>
@endforeach
</x-select.native>
<section aria-labelledby="steps-heading" class="space-y-4">
<div class="space-y-1">
<h2 id="steps-heading" class="text-xl font-semibold">{{ __('catalog.workflows.steps') }}</h2>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.workflows.steps_help') }}</p>
</div>
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
@error('form.steps')
<p role="alert" class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
<ol class="space-y-4">
@foreach ($form->steps as $index => $step)
{{-- The index is part of the key: a moved step is rendered anew, so its fields bind to their new position. --}}
<li wire:key="step-{{ $step['key'] }}-{{ $index }}">
<fieldset class="space-y-4 rounded-lg border border-zinc-200 p-4 dark:border-zinc-700">
<legend class="px-1 font-medium">{{ __('catalog.workflows.step_legend', ['number' => $index + 1]) }}</legend>
<x-input
id="step-{{ $step['key'] }}-title"
wire:model="form.steps.{{ $index }}.title"
:label="__('catalog.workflows.step_title')"
autocomplete="off"
required
/>
<x-select.native
id="step-{{ $step['key'] }}-work-type"
wire:model="form.steps.{{ $index }}.work_type_id"
:label="__('catalog.workflows.step_work_type')"
:aria-label="__('catalog.workflows.step_work_type_named', ['number' => $index + 1])"
:hint="__('catalog.workflows.step_work_type_help')"
>
<option value="">{{ __('catalog.workflows.template_work_type') }}</option>
@foreach ($this->stepWorkTypeGroups as $group => $workTypes)
<optgroup label="{{ $group }}">
@foreach ($workTypes as $workType)
<option value="{{ $workType->id }}">{{ $workType->name }}</option>
@endforeach
</optgroup>
@endforeach
</x-select.native>
<x-textarea
id="step-{{ $step['key'] }}-description"
wire:model="form.steps.{{ $index }}.description"
:label="__('catalog.workflows.step_description')"
resize-auto
/>
<div class="flex flex-wrap gap-2">
<x-button
id="step-{{ $step['key'] }}-up"
wire:click="moveStep({{ $index }}, 'up')"
subtle
color="black"
sm
icon="arrow-up"
:text="__('catalog.workflows.move_up')"
:aria-label="__('catalog.workflows.move_up_named', ['number' => $index + 1])"
/>
<x-button
id="step-{{ $step['key'] }}-down"
wire:click="moveStep({{ $index }}, 'down')"
subtle
color="black"
sm
icon="arrow-down"
:text="__('catalog.workflows.move_down')"
:aria-label="__('catalog.workflows.move_down_named', ['number' => $index + 1])"
/>
@if (count($form->steps) > 1)
<x-button
id="step-{{ $step['key'] }}-remove"
wire:click="removeStep({{ $index }})"
subtle
color="black"
sm
icon="trash"
:text="__('catalog.workflows.remove_step')"
:aria-label="__('catalog.workflows.remove_step_named', ['number' => $index + 1])"
/>
@endif
</div>
</fieldset>
</li>
@endforeach
</ol>
<x-button id="add-step" wire:click="addStep" subtle color="black" icon="plus" :text="__('catalog.workflows.add_step')" />
</section>
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('catalog.save')" />
<x-button :href="route('catalog.workflow-templates')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
</div>
</form>
</div>
@@ -0,0 +1,152 @@
<?php
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateSummary;
use App\Modules\Catalog\Application\UseCases\SetWorkflowTemplateActive;
use App\Modules\Catalog\Domain\Exceptions\WorkflowTemplateNotFound;
use Livewire\Attributes\Computed;
use Livewire\Component;
/**
* Workflow templates (työnkulkupohjat) grouped by service unit: adding,
* editing and taking out of use.
*/
new class extends Component
{
public string $statusMessage = '';
public function mount(): void
{
$this->authorize('catalog.manage');
}
/**
* @return array<string, list<WorkflowTemplateSummary>>
*/
#[Computed]
public function groups(): array
{
$groups = [];
foreach (app(WorkflowTemplateQueries::class)->all() as $template) {
$groups[$template->serviceUnitName][] = $template;
}
return $groups;
}
public function toggleActive(int $templateId, SetWorkflowTemplateActive $setActive): void
{
$this->authorize('catalog.manage');
$template = app(WorkflowTemplateQueries::class)->find($templateId);
if ($template === null) {
$this->statusMessage = __('catalog.status.not_found');
return;
}
try {
$setActive->execute($templateId, ! $template->isActive);
} catch (WorkflowTemplateNotFound) {
$this->statusMessage = __('catalog.status.not_found');
return;
}
unset($this->groups);
$this->statusMessage = __($template->isActive ? 'catalog.workflows.status.deactivated' : 'catalog.status.activated', ['name' => $template->name]);
}
/**
* @return list<array{index: string, label: string, sortable: bool}>
*/
public function headers(): array
{
return [
['index' => 'name', 'label' => __('catalog.fields.name'), 'sortable' => false],
['index' => 'work_type', 'label' => __('catalog.workflows.work_type'), 'sortable' => false],
['index' => 'steps', 'label' => __('catalog.workflows.step_count'), 'sortable' => false],
['index' => 'actions', 'label' => __('catalog.products.actions'), 'sortable' => false],
];
}
public function render(): mixed
{
return $this->view()->title(__('catalog.workflows.title'));
}
};
?>
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
<div class="flex flex-wrap items-center justify-between gap-4">
<h1 class="text-2xl font-semibold">{{ __('catalog.workflows.title') }}</h1>
<x-button :href="route('catalog.workflow-templates.create')" wire:navigate icon="plus" :text="__('catalog.workflows.add')" />
</div>
<div class="space-y-1 text-sm">
<p class="text-zinc-600 dark:text-zinc-300">{{ __('catalog.workflows.intro') }}</p>
<p><x-link :href="route('catalog.index')" navigate>{{ __('catalog.workflows.back_to_catalog') }}</x-link></p>
</div>
@if (session('status'))
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
@endif
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
@forelse ($this->groups as $unitName => $templates)
<section wire:key="unit-{{ $loop->index }}" aria-labelledby="unit-{{ $loop->index }}-heading" class="space-y-3">
<h2 id="unit-{{ $loop->index }}-heading" class="text-xl font-semibold break-words">{{ $unitName }}</h2>
<x-table :headers="$this->headers()" :rows="$templates">
@interact('column_name', $template)
<span class="font-medium break-words">{{ $template->name }}</span>
@unless ($template->isActive)
<span class="ml-1 text-sm">({{ __('catalog.inactive') }})</span>
@endunless
@endinteract
@interact('column_work_type', $template)
{{ $template->workTypeName }}
@endinteract
@interact('column_steps', $template)
{{ $template->stepCount }}
@endinteract
@interact('column_actions', $template)
<div class="flex flex-wrap items-center gap-2">
<x-button
:href="route('catalog.workflow-templates.edit', $template->id)"
wire:navigate
subtle
color="black"
sm
icon="pencil"
:text="__('catalog.actions.edit')"
:aria-label="__('catalog.actions.edit_named', ['name' => $template->name])"
/>
<label class="inline-flex min-h-6 cursor-pointer items-center gap-2 text-sm">
<input
type="checkbox"
id="active-template-{{ $template->id }}"
wire:click="toggleActive({{ $template->id }})"
@checked($template->isActive)
aria-label="{{ __('catalog.actions.active_named', ['name' => $template->name]) }}"
class="size-4 rounded border-zinc-400 text-primary-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700 dark:border-zinc-500 dark:bg-zinc-800"
/>
<span aria-hidden="true">{{ __('catalog.actions.active') }}</span>
</label>
</div>
@endinteract
</x-table>
</section>
@empty
<p>{{ __('catalog.workflows.none') }}</p>
@endforelse
</div>