Files
Toiminnanohjaus/app/Modules/Jobs/Presentation/Livewire/⚡task-form.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

238 lines
8.8 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\CatalogQueries;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\DTOs\NamedId;
use App\Modules\Jobs\Application\UseCases\AddTask;
use App\Modules\Jobs\Application\UseCases\UpdateTask;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
use App\Modules\Jobs\Presentation\Livewire\Forms\TaskForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Domain\Enums\Role;
use Carbon\CarbonImmutable;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Add page (no {task}) and edit page (with {task}) of a job's task.
*/
new class extends Component
{
#[Locked]
public int $jobId;
#[Locked]
public ?int $taskId = null;
#[Locked]
public string $jobTitle = '';
/** @var list<int> */
#[Locked]
public array $serviceUnitIds = [];
/**
* The task's work type when editing, offered even if taken out of use.
*/
#[Locked]
public ?int $storedWorkTypeId = null;
public TaskForm $form;
public function mount(CustomerJobQueries $jobQueries, TaskQueries $taskQueries, int $job, ?int $task = null): void
{
$details = $jobQueries->findDetails($job);
abort_if($details === null, 404);
$this->authorize('jobs.manage-tasks', $details->id);
$this->jobId = $details->id;
$this->jobTitle = $details->number.' '.$details->name;
$this->serviceUnitIds = array_map(fn (NamedId $unit): int => $unit->id, $details->serviceUnits);
if ($task === null) {
abort_unless($details->status->allowsTasks(), 404);
$this->form->assignee_user_ids = [(string) auth()->id()];
return;
}
$summary = $taskQueries->findTask($details->id, $task);
abort_if($summary === null, 404);
$this->taskId = $summary->id;
$this->storedWorkTypeId = $summary->workType->id;
$this->form->fillFromSummary($summary);
}
/**
* Work types of the job's service units and the shared ones, grouped
* as "Unit – Service" for <optgroup>.
*
* @return array<string, list<array{id: int, name: string}>>
*/
#[Computed]
public function workTypeGroups(): array
{
$catalog = app(CatalogQueries::class);
$groups = [];
$seen = [];
foreach ($catalog->activeWorkTypesFor($this->serviceUnitIds) as $node) {
if ($node->workTypes !== []) {
$groups[$node->serviceUnit->name] = array_map(fn ($workType): array => ['id' => $workType->id, 'name' => $workType->name], $node->workTypes);
}
foreach ($node->services as $serviceNode) {
if ($serviceNode->workTypes !== []) {
$groups[$node->serviceUnit->name.' – '.$serviceNode->service->name] = array_map(fn ($workType): array => ['id' => $workType->id, 'name' => $workType->name], $serviceNode->workTypes);
}
}
}
foreach ($groups as $group) {
foreach ($group as $workType) {
$seen[$workType['id']] = true;
}
}
if ($this->storedWorkTypeId !== null && ! isset($seen[$this->storedWorkTypeId]) && ($stored = $catalog->findWorkType($this->storedWorkTypeId)) !== null) {
$groups[__('catalog.inactive')] = [['id' => $stored->id, 'name' => $stored->name]];
}
return $groups;
}
/**
* @return list<array{value: int, label: string, note: ?string}>
*/
#[Computed]
public function userOptions(): array
{
return array_map(fn ($user): array => [
'value' => $user->id,
'label' => $user->name,
'note' => $user->role === Role::Subcontractor ? __('users.roles.subcontractor') : null,
], app(UserAccountQueries::class)->activeUserOptions());
}
public function save(AddTask $addTask, UpdateTask $updateTask): void
{
$this->authorize('jobs.manage-tasks', $this->jobId);
$data = $this->form->toData();
try {
if ($this->taskId === null) {
$taskId = $addTask->execute($this->jobId, $data, (int) auth()->id());
$message = __('jobs.tasks.created', ['title' => trim($data->title)]);
} else {
$taskId = $this->taskId;
$updateTask->execute($this->jobId, $taskId, $data);
$message = __('jobs.tasks.updated', ['title' => trim($data->title)]);
}
} catch (CustomerJobNotFound|TaskNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
$plannedTo = app(TaskQueries::class)->findTask($this->jobId, $taskId)?->plannedTo;
if ($data->plannedFrom !== null && $plannedTo !== null && $plannedTo !== ($data->plannedTo ?? $data->plannedFrom)) {
$message .= ' '.__('jobs.tasks.extended', ['to' => CarbonImmutable::parse($plannedTo)->format('j.n.Y')]);
}
session()->flash('status', $message);
$this->redirect(route('jobs.show', $this->jobId).'#tasks', navigate: true);
}
public function heading(): string
{
return __($this->taskId === null ? 'jobs.tasks.create_title' : 'jobs.tasks.edit_title');
}
public function render(): mixed
{
return $this->view()->title($this->heading().' – '.$this->jobTitle);
}
};
?>
<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">{{ $this->heading() }}</h1>
<p class="text-sm text-zinc-600 dark:text-zinc-300">
{{ __('jobs.tasks.job_label') }}:
<x-link :href="route('jobs.show', $jobId)" navigate>{{ $jobTitle }}</x-link>
</p>
</div>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.title" :label="__('jobs.tasks.fields.title')" 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="form.work_type_id" :label="__('jobs.tasks.fields.work_type')" :aria-label="__('jobs.tasks.fields.work_type')" :hint="__('jobs.tasks.fields.work_type_description')" required>
<option value="">{{ __('jobs.fields.none_selected') }}</option>
@foreach ($this->workTypeGroups as $group => $workTypes)
<optgroup label="{{ $group }}">
@foreach ($workTypes as $workType)
<option value="{{ $workType['id'] }}">{{ $workType['name'] }}</option>
@endforeach
</optgroup>
@endforeach
</x-select.native>
<x-checkbox-group
:legend="__('jobs.tasks.fields.assignees')"
model="form.assignee_user_ids"
:options="$this->userOptions"
/>
<div class="grid gap-6 sm:grid-cols-3">
<x-select.native wire:model="form.status" :label="__('jobs.tasks.fields.status')" :aria-label="__('jobs.tasks.fields.status')">
@foreach (TaskStatus::cases() as $status)
<option value="{{ $status->value }}">{{ __('jobs.tasks.statuses.'.$status->value) }}</option>
@endforeach
</x-select.native>
<x-input wire:model="form.due_on" type="date" :label="__('jobs.tasks.fields.due_on')" />
<x-input
wire:model="form.estimated_hours"
type="number"
step="0.25"
min="0"
inputmode="decimal"
:label="__('jobs.tasks.fields.estimated_hours')"
/>
</div>
<fieldset class="space-y-2">
<legend class="text-sm font-semibold">{{ __('jobs.tasks.schedule') }}</legend>
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tasks.fields.planned_hint') }}</p>
<div class="grid gap-6 sm:grid-cols-2">
<x-input wire:model="form.planned_from" type="date" :label="__('jobs.tasks.fields.planned_from')" />
<x-input wire:model="form.planned_to" type="date" :label="__('jobs.tasks.fields.planned_to')" />
</div>
</fieldset>
<x-textarea wire:model="form.description" :label="__('jobs.tasks.fields.description')" resize-auto />
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('jobs.save')" />
<x-button :href="route('jobs.show', $jobId).'#tasks'" wire:navigate subtle color="black" :text="__('jobs.cancel')" />
</div>
</form>
</div>