diff --git a/CLAUDE.md b/CLAUDE.md index ed028ba..9ad644e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -4,7 +4,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ## Project -An ERP / work-management system (*toiminnanohjausjärjestelmä*) for a small (5–10 person) Finnish media/marketing company. Stack: Laravel 13, Livewire 4 (single-file components), TallStackUI 4 (Blade components, no prefix: ``, ``), Tailwind 4, Vite 8, Pest, Larastan. Domain code lives in modules under `app/Modules` (so far `Shared`, `Users`, `Customers`, `Catalog` (service units, services and work types, managed on the Pohjatiedot page; hourly rates, products and the mileage rate on the Hinnasto page, read through `PriceListQueries`, never `CatalogQueries`) `Jobs` (customer jobs: numbering, members, tags, links, history, tasks) `TimeTracking` (hours on tasks: weekly sheet and task booking page) `Expenses` (expenses on jobs with cost, markup and billed amount; reads jobs and prices through its own `ExpenseJobs`/`ExpensePricing` ports) `Billing` (the monthly billing material: a draft of invoices made of done jobs and monthly fee jobs (one line per unbilled month up to the draft's month, each such job an invoice of its own), management's billable hours, and on creation invoice lines with price snapshots, downloadable as a semicolon CSV with amounts in whole cents and the VAT rate in tenths of a percent (`billing.csv`, with consumers' identity codes for those who may see them); reads and marks hours, expenses and jobs through its `BillingJobs`/`BillingHours`/`BillingExpenses`/`BillingPrices` ports, which call those modules' queries and the use cases `MarkJobHoursBilled`, `MarkJobExpensesBilled` and `ChangeJobStatus`) and `Sales` (quick sales of consumer products at the counter, credits and cash days; its `QuickSaleHours` port books product durations through Jobs' `ProvideQuickSaleTask` (the yearly "Pikamyynnit" job) and TimeTracking's `AddTaskHours`)); `/` is still a temporary Livewire demo page behind login (`resources/views/pages/⚡home.blade.php`, layout `resources/views/layouts/app.blade.php`, which has the main menu and logout for signed-in users). +An ERP / work-management system (*toiminnanohjausjärjestelmä*) for a small (5–10 person) Finnish media/marketing company. Stack: Laravel 13, Livewire 4 (single-file components), TallStackUI 4 (Blade components, no prefix: ``, ``), Tailwind 4, Vite 8, Pest, Larastan. Domain code lives in modules under `app/Modules` (so far `Shared`, `Users`, `Customers`, `Catalog` (service units, services and work types, managed on the Pohjatiedot page; hourly rates, products and the mileage rate on the Hinnasto page, read through `PriceListQueries`, never `CatalogQueries`) `Jobs` (customer jobs: numbering, members, tags, links, history, tasks, task checklists) `TimeTracking` (hours on tasks: weekly sheet and task booking page) `Expenses` (expenses on jobs with cost, markup and billed amount; reads jobs and prices through its own `ExpenseJobs`/`ExpensePricing` ports) `Billing` (the monthly billing material: a draft of invoices made of done jobs and monthly fee jobs (one line per unbilled month up to the draft's month, each such job an invoice of its own), management's billable hours, and on creation invoice lines with price snapshots, downloadable as a semicolon CSV with amounts in whole cents and the VAT rate in tenths of a percent (`billing.csv`, with consumers' identity codes for those who may see them); reads and marks hours, expenses and jobs through its `BillingJobs`/`BillingHours`/`BillingExpenses`/`BillingPrices` ports, which call those modules' queries and the use cases `MarkJobHoursBilled`, `MarkJobExpensesBilled` and `ChangeJobStatus`) and `Sales` (quick sales of consumer products at the counter, credits and cash days; its `QuickSaleHours` port books product durations through Jobs' `ProvideQuickSaleTask` (the yearly "Pikamyynnit" job) and TimeTracking's `AddTaskHours`)); `/` is still a temporary Livewire demo page behind login (`resources/views/pages/⚡home.blade.php`, layout `resources/views/layouts/app.blade.php`, which has the main menu and logout for signed-in users). - **The specification is `docs/maarittely.md`** (Finnish). It defines roles and the permission table, money visibility, customers, customer jobs (*asiakastyö*) with fields and enum keys, Kanban, tasks, time entries, expenses, the service unit → service → work type hierarchy and pricing, Fennoa billing export, the data model, the phases with phase 1 in detail, and open questions. Read it before designing any domain feature. `määrittely.txt` is the original description with the customer's answers to questions 1–53, referenced as *(K12)* in the spec. - **Language rule:** code (identifiers, tables, columns, enum keys) is in English; the specification and the UI are in Finnish. Keep UI strings in translation files from the start. diff --git a/app/Modules/Jobs/Application/Contracts/ChecklistQueries.php b/app/Modules/Jobs/Application/Contracts/ChecklistQueries.php new file mode 100644 index 0000000..7d3a09e --- /dev/null +++ b/app/Modules/Jobs/Application/Contracts/ChecklistQueries.php @@ -0,0 +1,18 @@ + + */ + public function checklistOf(int $jobId, int $taskId): array; +} diff --git a/app/Modules/Jobs/Application/DTOs/ChecklistItemSummary.php b/app/Modules/Jobs/Application/DTOs/ChecklistItemSummary.php new file mode 100644 index 0000000..eba49da --- /dev/null +++ b/app/Modules/Jobs/Application/DTOs/ChecklistItemSummary.php @@ -0,0 +1,14 @@ +ensureTask($jobId, $taskId); + + return $this->items->findForTask($taskId, $itemId) ?? throw new ChecklistItemNotFound($taskId, $itemId); + } + + /** + * @throws TaskNotFound + */ + public function ensureTask(int $jobId, int $taskId): void + { + if ($this->tasks->findForJob($jobId, $taskId) === null) { + throw new TaskNotFound($jobId, $taskId); + } + } +} diff --git a/app/Modules/Jobs/Application/UseCases/AddChecklistItem.php b/app/Modules/Jobs/Application/UseCases/AddChecklistItem.php new file mode 100644 index 0000000..90f9f1d --- /dev/null +++ b/app/Modules/Jobs/Application/UseCases/AddChecklistItem.php @@ -0,0 +1,42 @@ +transactions->run(function () use ($jobId, $taskId, $text): int { + $this->finder->ensureTask($jobId, $taskId); + + return (int) $this->items->save(ChecklistItem::create($taskId, $text, $this->items->nextSortOrder($taskId)))->id(); + }); + } +} diff --git a/app/Modules/Jobs/Application/UseCases/CheckChecklistItem.php b/app/Modules/Jobs/Application/UseCases/CheckChecklistItem.php new file mode 100644 index 0000000..be1afba --- /dev/null +++ b/app/Modules/Jobs/Application/UseCases/CheckChecklistItem.php @@ -0,0 +1,44 @@ +transactions->run(function () use ($jobId, $taskId, $itemId, $done, $userId): void { + $item = $this->finder->find($jobId, $taskId, $itemId); + + if ($done) { + $item->check($userId, $this->clock->now()); + } else { + $item->uncheck(); + } + + $this->items->save($item); + }); + } +} diff --git a/app/Modules/Jobs/Application/UseCases/MoveChecklistItem.php b/app/Modules/Jobs/Application/UseCases/MoveChecklistItem.php new file mode 100644 index 0000000..528cbe8 --- /dev/null +++ b/app/Modules/Jobs/Application/UseCases/MoveChecklistItem.php @@ -0,0 +1,48 @@ +transactions->run(function () use ($jobId, $taskId, $itemId, $direction): bool { + $item = $this->finder->find($jobId, $taskId, $itemId); + $neighbour = $this->items->neighbour($item, $direction); + + if ($neighbour === null) { + return false; + } + + $item->swapOrderWith($neighbour); + $this->items->save($item); + $this->items->save($neighbour); + + return true; + }); + } +} diff --git a/app/Modules/Jobs/Application/UseCases/RemoveChecklistItem.php b/app/Modules/Jobs/Application/UseCases/RemoveChecklistItem.php new file mode 100644 index 0000000..52c9adc --- /dev/null +++ b/app/Modules/Jobs/Application/UseCases/RemoveChecklistItem.php @@ -0,0 +1,31 @@ +transactions->run(function () use ($jobId, $taskId, $itemId): void { + $this->items->delete($this->finder->find($jobId, $taskId, $itemId)); + }); + } +} diff --git a/app/Modules/Jobs/Application/UseCases/RenameChecklistItem.php b/app/Modules/Jobs/Application/UseCases/RenameChecklistItem.php new file mode 100644 index 0000000..7a487a5 --- /dev/null +++ b/app/Modules/Jobs/Application/UseCases/RenameChecklistItem.php @@ -0,0 +1,38 @@ +transactions->run(function () use ($jobId, $taskId, $itemId, $text): void { + $item = $this->finder->find($jobId, $taskId, $itemId); + $item->rename($text); + $this->items->save($item); + }); + } +} diff --git a/app/Modules/Jobs/Domain/Contracts/ChecklistItemRepository.php b/app/Modules/Jobs/Domain/Contracts/ChecklistItemRepository.php new file mode 100644 index 0000000..b557222 --- /dev/null +++ b/app/Modules/Jobs/Domain/Contracts/ChecklistItemRepository.php @@ -0,0 +1,27 @@ +text = $text; + } + + /** + * Checking an item that already is done keeps who did it and when. + */ + public function check(int $userId, DateTimeImmutable $at): void + { + if ($this->doneAt !== null) { + return; + } + + $this->doneAt = $at; + $this->doneBy = $userId; + } + + public function uncheck(): void + { + $this->doneAt = null; + $this->doneBy = null; + } + + public function swapOrderWith(self $other): void + { + [$this->sortOrder, $other->sortOrder] = [$other->sortOrder, $this->sortOrder]; + } + + public function id(): ?int + { + return $this->id; + } + + public function taskId(): int + { + return $this->taskId; + } + + public function text(): ChecklistItemText + { + return $this->text; + } + + public function sortOrder(): int + { + return $this->sortOrder; + } + + public function isDone(): bool + { + return $this->doneAt !== null; + } + + public function doneAt(): ?DateTimeImmutable + { + return $this->doneAt; + } + + public function doneBy(): ?int + { + return $this->doneBy; + } +} diff --git a/app/Modules/Jobs/Domain/Enums/MoveDirection.php b/app/Modules/Jobs/Domain/Enums/MoveDirection.php new file mode 100644 index 0000000..e06b315 --- /dev/null +++ b/app/Modules/Jobs/Domain/Enums/MoveDirection.php @@ -0,0 +1,11 @@ + self::MAX_LENGTH) { + throw new InvalidChecklistItemText; + } + + $this->value = $value; + } +} diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/ChecklistItemModel.php b/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/ChecklistItemModel.php new file mode 100644 index 0000000..10cc0b6 --- /dev/null +++ b/app/Modules/Jobs/Infrastructure/Persistence/Eloquent/ChecklistItemModel.php @@ -0,0 +1,37 @@ + + */ + protected function casts(): array + { + return [ + 'task_id' => 'integer', + 'sort_order' => 'integer', + 'done_at' => 'immutable_datetime', + 'done_by' => 'integer', + ]; + } +} diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentChecklistItemRepository.php b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentChecklistItemRepository.php new file mode 100644 index 0000000..e1f161b --- /dev/null +++ b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentChecklistItemRepository.php @@ -0,0 +1,73 @@ +where('task_id', $taskId)->find($itemId); + + return $model === null ? null : $this->toEntity($model); + } + + public function neighbour(ChecklistItem $item, MoveDirection $direction): ?ChecklistItem + { + $up = $direction === MoveDirection::Up; + $model = ChecklistItemModel::query() + ->where('task_id', $item->taskId()) + ->whereKeyNot($item->id()) + ->where(fn ($query) => $query + ->where('sort_order', $up ? '<' : '>', $item->sortOrder()) + ->orWhere(fn ($query) => $query->where('sort_order', $item->sortOrder())->where('id', $up ? '<' : '>', $item->id()))) + ->orderBy('sort_order', $up ? 'desc' : 'asc') + ->orderBy('id', $up ? 'desc' : 'asc') + ->first(); + + return $model === null ? null : $this->toEntity($model); + } + + public function nextSortOrder(int $taskId): int + { + return (int) ChecklistItemModel::query()->where('task_id', $taskId)->max('sort_order') + 1; + } + + public function save(ChecklistItem $item): ChecklistItem + { + $model = $item->id() === null ? new ChecklistItemModel : ChecklistItemModel::query()->findOrFail($item->id()); + + $model->fill([ + 'task_id' => $item->taskId(), + 'text' => $item->text()->value, + 'sort_order' => $item->sortOrder(), + 'done_at' => $item->doneAt()?->format('Y-m-d H:i:s'), + 'done_by' => $item->doneBy(), + ])->save(); + + return $this->toEntity($model->refresh()); + } + + public function delete(ChecklistItem $item): void + { + ChecklistItemModel::query()->whereKey($item->id())->delete(); + } + + private function toEntity(ChecklistItemModel $model): ChecklistItem + { + return ChecklistItem::reconstitute( + $model->id, + $model->task_id, + new ChecklistItemText($model->text), + $model->sort_order, + $model->done_at === null ? null : DateTimeImmutable::createFromInterface($model->done_at), + $model->done_by, + ); + } +} diff --git a/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentChecklistQueries.php b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentChecklistQueries.php new file mode 100644 index 0000000..e6f5211 --- /dev/null +++ b/app/Modules/Jobs/Infrastructure/Persistence/Repositories/EloquentChecklistQueries.php @@ -0,0 +1,25 @@ +where('task_id', $taskId) + ->whereExists(fn ($query) => $query->selectRaw('1')->from('tasks') + ->whereColumn('tasks.id', 'task_checklist_items.task_id') + ->where('tasks.customer_job_id', $jobId)) + ->orderBy('sort_order') + ->orderBy('id') + ->get() + ->map(fn (ChecklistItemModel $model): ChecklistItemSummary => new ChecklistItemSummary($model->id, $model->text, $model->done_at !== null)) + ->values() + ->all(); + } +} diff --git a/app/Modules/Jobs/Infrastructure/Providers/JobsServiceProvider.php b/app/Modules/Jobs/Infrastructure/Providers/JobsServiceProvider.php index d9b1103..7b1b837 100644 --- a/app/Modules/Jobs/Infrastructure/Providers/JobsServiceProvider.php +++ b/app/Modules/Jobs/Infrastructure/Providers/JobsServiceProvider.php @@ -4,15 +4,19 @@ namespace App\Modules\Jobs\Infrastructure\Providers; use App\Models\User; use App\Modules\Customers\Application\Contracts\CustomerRowUsage; +use App\Modules\Jobs\Application\Contracts\ChecklistQueries; use App\Modules\Jobs\Application\Contracts\CustomerJobQueries; use App\Modules\Jobs\Application\Contracts\TaskQueries; use App\Modules\Jobs\Application\Contracts\TaskUsage; +use App\Modules\Jobs\Domain\Contracts\ChecklistItemRepository; use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository; use App\Modules\Jobs\Domain\Contracts\JobNumberSequence; use App\Modules\Jobs\Domain\Contracts\TaskRepository; use App\Modules\Jobs\Infrastructure\CustomerRowUsageFromJobs; use App\Modules\Jobs\Infrastructure\NothingUsesTasks; use App\Modules\Jobs\Infrastructure\Persistence\DatabaseJobNumberSequence; +use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentChecklistItemRepository; +use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentChecklistQueries; use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentCustomerJobQueries; use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentCustomerJobRepository; use App\Modules\Jobs\Infrastructure\Persistence\Repositories\EloquentTaskQueries; @@ -38,6 +42,8 @@ class JobsServiceProvider extends ServiceProvider CustomerRowUsage::class => CustomerRowUsageFromJobs::class, TaskRepository::class => EloquentTaskRepository::class, TaskQueries::class => EloquentTaskQueries::class, + ChecklistItemRepository::class => EloquentChecklistItemRepository::class, + ChecklistQueries::class => EloquentChecklistQueries::class, // Replaced by the TimeTracking module, which knows which tasks have time entries. TaskUsage::class => NothingUsesTasks::class, ]; diff --git a/app/Modules/Jobs/Presentation/Livewire/⚡show.blade.php b/app/Modules/Jobs/Presentation/Livewire/⚡show.blade.php index d5af184..24f6a9e 100644 --- a/app/Modules/Jobs/Presentation/Livewire/⚡show.blade.php +++ b/app/Modules/Jobs/Presentation/Livewire/⚡show.blade.php @@ -356,6 +356,8 @@ new class extends Component

{{ $task->description }}

@endif + +
@if ($canChangeStatus)
diff --git a/app/Modules/Jobs/Presentation/Livewire/⚡task-checklist.blade.php b/app/Modules/Jobs/Presentation/Livewire/⚡task-checklist.blade.php new file mode 100644 index 0000000..0859ffe --- /dev/null +++ b/app/Modules/Jobs/Presentation/Livewire/⚡task-checklist.blade.php @@ -0,0 +1,364 @@ +authorize('jobs.view-job', $jobId); + + $this->jobId = $jobId; + $this->taskId = $taskId; + $this->taskTitle = $taskTitle; + } + + /** + * @return list + */ + #[Computed] + public function items(): array + { + return app(ChecklistQueries::class)->checklistOf($this->jobId, $this->taskId); + } + + public function check(int $itemId, bool $done, CheckChecklistItem $checkItem): void + { + $this->authorize('jobs.update-task-status', $this->jobId); + + $item = $this->item($itemId); + + if ($item === null) { + return; + } + + try { + $checkItem->execute($this->jobId, $this->taskId, $itemId, $done, (int) auth()->id()); + } catch (TaskNotFound|ChecklistItemNotFound) { + $this->notFound(); + + return; + } + + $this->refreshItems(); + $this->message = __($done ? 'jobs.tasks.checklist.checked' : 'jobs.tasks.checklist.unchecked', ['text' => $item->text]); + } + + public function add(AddChecklistItem $addItem): void + { + $this->authorize('jobs.manage-tasks', $this->jobId); + $this->resetErrorBag(); + + if (trim($this->newItem) === '') { + $this->addError('newItem', __('jobs.tasks.checklist.errors.text_required')); + + return; + } + + try { + $addItem->execute($this->jobId, $this->taskId, $this->newItem); + } catch (InvalidChecklistItemText) { + $this->addError('newItem', __('jobs.tasks.checklist.errors.invalid_text')); + + return; + } catch (TaskNotFound) { + $this->notFound(); + + return; + } + + $this->refreshItems(); + $this->message = __('jobs.tasks.checklist.added', ['text' => trim($this->newItem)]); + $this->reset('newItem'); + } + + public function startRename(int $itemId): void + { + $this->authorize('jobs.manage-tasks', $this->jobId); + + $item = $this->item($itemId); + + if ($item === null) { + return; + } + + $this->editingItemId = $item->id; + $this->editingText = $item->text; + $this->resetErrorBag(); + $this->js("document.getElementById('checklist-edit-{$item->id}')?.focus()"); + } + + public function saveRename(RenameChecklistItem $renameItem): void + { + $this->authorize('jobs.manage-tasks', $this->jobId); + $this->resetErrorBag(); + + if ($this->editingItemId === null) { + return; + } + + if (trim($this->editingText) === '') { + $this->addError('editingText', __('jobs.tasks.checklist.errors.text_required')); + + return; + } + + try { + $renameItem->execute($this->jobId, $this->taskId, $this->editingItemId, $this->editingText); + } catch (InvalidChecklistItemText) { + $this->addError('editingText', __('jobs.tasks.checklist.errors.invalid_text')); + + return; + } catch (TaskNotFound|ChecklistItemNotFound) { + $this->cancelRename(); + $this->notFound(); + + return; + } + + $this->refreshItems(); + $this->message = __('jobs.tasks.checklist.renamed', ['text' => trim($this->editingText)]); + $this->cancelRename(); + } + + public function cancelRename(): void + { + $itemId = $this->editingItemId; + + $this->reset('editingItemId', 'editingText'); + $this->resetErrorBag(); + + if ($itemId !== null) { + $this->js("document.getElementById('checklist-rename-{$itemId}')?.focus()"); + } + } + + public function move(int $itemId, string $direction, MoveChecklistItem $moveItem): void + { + $this->authorize('jobs.manage-tasks', $this->jobId); + + $item = $this->item($itemId); + $moveDirection = MoveDirection::tryFrom($direction); + + if ($item === null || $moveDirection === null) { + $this->notFound(); + + return; + } + + try { + $moved = $moveItem->execute($this->jobId, $this->taskId, $itemId, $moveDirection); + } catch (TaskNotFound|ChecklistItemNotFound) { + $this->notFound(); + + return; + } + + $this->refreshItems(); + $this->message = __(match (true) { + $moved && $moveDirection === MoveDirection::Up => 'jobs.tasks.checklist.moved_up', + $moved => 'jobs.tasks.checklist.moved_down', + $moveDirection === MoveDirection::Up => 'jobs.tasks.checklist.already_first', + default => 'jobs.tasks.checklist.already_last', + }, ['text' => $item->text]); + + // The item moved in the DOM: keep focus on the same button. + $this->js("document.getElementById('checklist-move-{$moveDirection->value}-{$itemId}')?.focus()"); + } + + public function remove(int $itemId, RemoveChecklistItem $removeItem): void + { + $this->authorize('jobs.manage-tasks', $this->jobId); + + $item = $this->item($itemId); + + if ($item === null) { + return; + } + + try { + $removeItem->execute($this->jobId, $this->taskId, $itemId); + } catch (TaskNotFound|ChecklistItemNotFound) { + $this->notFound(); + + return; + } + + if ($this->editingItemId === $itemId) { + $this->reset('editingItemId', 'editingText'); + } + + $this->refreshItems(); + $this->message = __('jobs.tasks.checklist.removed', ['text' => $item->text]); + + // The removed item's buttons are gone: move focus to the field for a new item. + $this->js("document.getElementById('checklist-new-{$this->taskId}')?.focus()"); + } + + private function item(int $itemId): ?ChecklistItemSummary + { + foreach ($this->items as $item) { + if ($item->id === $itemId) { + return $item; + } + } + + $this->notFound(); + + return null; + } + + private function notFound(): void + { + $this->refreshItems(); + $this->message = __('jobs.tasks.checklist.not_found'); + } + + private function refreshItems(): void + { + unset($this->items); + } +}; +?> + +
+ @php($canCheck = Gate::allows('jobs.update-task-status', $jobId)) + @php($canManage = Gate::allows('jobs.manage-tasks', $jobId)) + @php($doneCount = count(array_filter($this->items, fn ($item) => $item->done))) + +

+ {{ __('jobs.tasks.checklist.title') }} + @if ($this->items !== []) + {{ __('jobs.tasks.checklist.progress', ['done' => $doneCount, 'total' => count($this->items)]) }} + @endif +

+ +

{{ $message }}

+ + @if ($this->items === []) +

{{ __('jobs.tasks.checklist.empty') }}

+ @else +
    + @foreach ($this->items as $item) +
  • + @if ($editingItemId === $item->id) +
    +
    + +
    + + + + @else + @if ($canCheck) + + @else + + $item->done])>{{ $item->text }} + ({{ $item->done ? __('jobs.tasks.checklist.done') : __('jobs.tasks.checklist.not_done') }}) + + @endif + + @if ($canManage) +
    + + + + +
    + @endif + @endif +
  • + @endforeach +
+ @endif + + @if ($canManage) +
+
+ +
+ + + @endif +
diff --git a/database/migrations/2026_11_24_114405_create_task_checklist_items_table.php b/database/migrations/2026_11_24_114405_create_task_checklist_items_table.php new file mode 100644 index 0000000..d1c3a39 --- /dev/null +++ b/database/migrations/2026_11_24_114405_create_task_checklist_items_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignId('task_id')->constrained()->cascadeOnDelete(); + $table->string('text'); + $table->unsignedInteger('sort_order'); + $table->timestamp('done_at')->nullable(); + $table->foreignId('done_by')->nullable()->constrained('users')->nullOnDelete(); + $table->timestamps(); + + $table->index(['task_id', 'sort_order']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('task_checklist_items'); + } +}; diff --git a/docs/maarittely.md b/docs/maarittely.md index 5f8ba0f..88799b9 100644 --- a/docs/maarittely.md +++ b/docs/maarittely.md @@ -407,7 +407,7 @@ Tunnit kirjataan tehtäville, joten perustehtävät ja palveluyksiköiden työte Hinnasto ja tuotteet, rahakentät ja henkilökohtaiset näkyvyysoikeudet, kulut, tuntien hyväksyntä ja lukitus, laskuaineiston vienti Fennoaan (CSV/Excel), kuukausilaskutusrivit, pikamyynti. **Vaihe 3 – tehtävien hallinta** -Tehtävien Kanban-sarakkeet asetuksista palveluyksiköittäin, tarkistuslistat, kommentit, kuvat, työnkulkupohjat, kalenterinäkymä, kenttien yksinkertaistus palveluyksiköittäin. +Tehtävien Kanban-sarakkeet asetuksista palveluyksiköittäin, tarkistuslistat *(valmis)*, kommentit, kuvat, työnkulkupohjat, kalenterinäkymä, kenttien yksinkertaistus palveluyksiköittäin. **Vaihe 4 – laajennukset** Alihankkijoiden näkymät ja rajaukset, raportit, ilmoitukset, Fennoa-rajapinta, PWA. @@ -495,7 +495,14 @@ Jokainen sivu täyttää saavutettavuusvaatimukset ja saa selaintestin (vaalea/t - Luonnoksella on laskutuskuukausi (oletuksena kuluva kuukausi), johon asti kk-laskutuksen työt laskutetaan; kuukauden vaihto laskee esikatselun uudelleen (5.5). - Muodostetun aineiston sivulla ja Laskutus-listalla on CSV-lataus (luku 12). Tulossa: sarakkeiden sovitus Fennoan tuontimuotoon (avoin kysymys 4). -## 18. Avoimet kysymykset +## 18. Vaiheen 3 tarkka sisältö + +### 18.1 Tarkistuslistat *(valmis)* +- Työn sivulla jokaisen tehtävän kortissa on tarkistuslista edistymisineen ("1/3 tehty"). Kohdan kuittaa tehdyksi tai tekemättömäksi valintaruudulla (myös alihankkija liitettyjen töidensä tehtävissä); kuittaajan ja ajan tieto tallennetaan. +- Muut kuin alihankkijat lisäävät kohtia (1–255 merkkiä), muokkaavat niitä rivillä, järjestävät ne Siirrä ylös / Siirrä alas -painikkeilla ja poistavat ne. Kohdan poistoa ei vahvisteta dialogissa; muutokset ilmoitetaan ruudunlukijalle, ja fokus pysyy käytetyssä painikkeessa tai siirtyy lisäyskenttään. +- Kohdat poistuvat tehtävän mukana. + +## 19. Avoimet kysymykset 1. ~~**Lukitus**~~: ratkaistu – vain johto avaa valmiin työn lukituksen siirtämällä työn aiempaan tilaan, ja avaus näkyy muutoshistoriassa (5.3). 2. **Alihankkijayritys**: onko yrityksellä useita käyttäjätunnuksia, ja tarvitaanko yritys omana tietonaan (nimi, Y-tunnus)? diff --git a/lang/fi/jobs.php b/lang/fi/jobs.php index 79d41b7..22dc1b4 100644 --- a/lang/fi/jobs.php +++ b/lang/fi/jobs.php @@ -51,6 +51,38 @@ return [ 'in_review' => 'Tarkastettavana', 'done' => 'Valmis', ], + 'checklist' => [ + 'title' => 'Tarkistuslista', + 'progress' => '(:done/:total tehty)', + 'list_of' => 'Tarkistuslista: :title', + 'empty' => 'Ei kohtia.', + 'done' => 'tehty', + 'not_done' => 'ei tehty', + 'new_item' => 'Uusi kohta', + 'text' => 'Kohdan teksti', + 'add' => 'Lisää kohta', + 'add_named' => 'Lisää kohta tehtävälle :title', + 'save' => 'Tallenna', + 'cancel' => 'Peruuta', + 'rename_named' => 'Muokkaa kohtaa :text', + 'move_up_named' => 'Siirrä ylös: :text', + 'move_down_named' => 'Siirrä alas: :text', + 'remove_named' => 'Poista kohta :text', + 'added' => 'Kohta :text lisätty.', + 'renamed' => 'Kohta :text tallennettu.', + 'removed' => 'Kohta :text poistettu.', + 'checked' => 'Kohta :text merkitty tehdyksi.', + 'unchecked' => 'Kohta :text merkitty tekemättömäksi.', + 'moved_up' => 'Kohta :text siirretty ylöspäin.', + 'moved_down' => 'Kohta :text siirretty alaspäin.', + 'already_first' => 'Kohta :text on jo ensimmäisenä.', + 'already_last' => 'Kohta :text on jo viimeisenä.', + 'not_found' => 'Kohtaa ei löytynyt. Päivitä sivu.', + 'errors' => [ + 'text_required' => 'Anna kohdan teksti.', + 'invalid_text' => 'Kohdan teksti voi olla enintään 255 merkkiä.', + ], + ], 'errors' => [ 'title_required' => 'Anna tehtävän nimi.', 'invalid_title' => 'Nimen pituus voi olla enintään 255 merkkiä.', diff --git a/tests/Browser/JobsAccessibilityTest.php b/tests/Browser/JobsAccessibilityTest.php index 1bb3e30..188fc13 100644 --- a/tests/Browser/JobsAccessibilityTest.php +++ b/tests/Browser/JobsAccessibilityTest.php @@ -4,6 +4,7 @@ use App\Models\User; use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel; use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel; use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel; +use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\ChecklistItemModel; use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel; use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -155,6 +156,8 @@ it('has no accessibility issues on a job with tasks and on the task form', funct $task = TaskModel::factory() ->assignedTo([$this->user->id]) ->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Kuvauspäivä', 'due_on' => now()->subDay()->toDateString(), 'estimated_hours' => 6]); + ChecklistItemModel::query()->create(['task_id' => $task->id, 'text' => 'Valot', 'sort_order' => 1, 'done_at' => now(), 'done_by' => $this->user->id]); + ChecklistItemModel::query()->create(['task_id' => $task->id, 'text' => 'Mikit', 'sort_order' => 2]); $url = match ($page) { 'job page' => route('jobs.show', $this->job->id), @@ -192,3 +195,37 @@ it('changes a task status and removes a task with the keyboard', function () { ->assertSee('Tehtävä Kuvauspäivä poistettu.') ->assertScript('document.activeElement.id', 'tasks-heading'); }); + +it('adds, checks, renames, moves and removes checklist items with the keyboard', function () { + $this->job->update(['status' => 'order']); + $workType = WorkTypeModel::query()->where('name', 'videokuvaus')->sole(); + $task = TaskModel::factory() + ->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Kuvauspäivä']); + + $page = visit(route('jobs.show', $this->job->id)) + ->type('#checklist-new-'.$task->id, 'Valot') + ->keys('#checklist-new-'.$task->id, 'Enter') + ->assertSee('Kohta Valot lisätty.') + ->type('#checklist-new-'.$task->id, 'Mikit') + ->keys('#checklist-new-'.$task->id, 'Enter') + ->assertSee('Kohta Mikit lisätty.'); + + [$lights, $mics] = ChecklistItemModel::query()->orderBy('id')->pluck('id')->all(); + + $page->keys('#checklist-item-'.$lights, 'Space') + ->assertSee('Kohta Valot merkitty tehdyksi.') + ->assertSee('(1/2 tehty)') + ->keys('#checklist-rename-'.$lights, 'Enter') + ->assertScript('document.activeElement.id', 'checklist-edit-'.$lights) + ->type('#checklist-edit-'.$lights, 'Valot ja jalustat') + ->keys('#checklist-edit-'.$lights, 'Enter') + ->assertSee('Kohta Valot ja jalustat tallennettu.') + ->assertScript('document.activeElement.id', 'checklist-rename-'.$lights) + ->keys('#checklist-move-up-'.$mics, 'Enter') + ->assertSee('Kohta Mikit siirretty ylöspäin.') + ->assertScript('document.activeElement.id', 'checklist-move-up-'.$mics) + ->keys('[aria-label="Poista kohta Mikit"]', 'Enter') + ->assertSee('Kohta Mikit poistettu.') + ->assertScript('document.activeElement.id', 'checklist-new-'.$task->id) + ->assertNoAccessibilityIssues(level: 2); +}); diff --git a/tests/Fakes/InMemoryChecklistItemRepository.php b/tests/Fakes/InMemoryChecklistItemRepository.php new file mode 100644 index 0000000..95660bd --- /dev/null +++ b/tests/Fakes/InMemoryChecklistItemRepository.php @@ -0,0 +1,64 @@ + */ + public array $items = []; + + private int $nextId = 1; + + public function findForTask(int $taskId, int $itemId): ?ChecklistItem + { + $item = $this->items[$itemId] ?? null; + + return $item !== null && $item->taskId() === $taskId ? clone $item : null; + } + + public function neighbour(ChecklistItem $item, MoveDirection $direction): ?ChecklistItem + { + $siblings = array_values(array_filter($this->items, fn (ChecklistItem $other): bool => $other->taskId() === $item->taskId())); + usort($siblings, fn (ChecklistItem $a, ChecklistItem $b): int => [$a->sortOrder(), $a->id()] <=> [$b->sortOrder(), $b->id()]); + $ids = array_map(fn (ChecklistItem $other): ?int => $other->id(), $siblings); + $index = array_search($item->id(), $ids, true); + $neighbour = $siblings[$direction === MoveDirection::Up ? $index - 1 : $index + 1] ?? null; + + return $neighbour === null ? null : clone $neighbour; + } + + public function nextSortOrder(int $taskId): int + { + $orders = array_map(fn (ChecklistItem $item): int => $item->sortOrder(), array_filter($this->items, fn (ChecklistItem $item): bool => $item->taskId() === $taskId)); + + return ($orders === [] ? 0 : max($orders)) + 1; + } + + public function save(ChecklistItem $item): ChecklistItem + { + $id = $item->id() ?? $this->nextId++; + $this->items[$id] = ChecklistItem::reconstitute($id, $item->taskId(), $item->text(), $item->sortOrder(), $item->doneAt(), $item->doneBy()); + + return clone $this->items[$id]; + } + + public function delete(ChecklistItem $item): void + { + unset($this->items[$item->id()]); + } + + /** + * @return list + */ + public function textsOf(int $taskId): array + { + $items = array_values(array_filter($this->items, fn (ChecklistItem $item): bool => $item->taskId() === $taskId)); + usort($items, fn (ChecklistItem $a, ChecklistItem $b): int => [$a->sortOrder(), $a->id()] <=> [$b->sortOrder(), $b->id()]); + + return array_map(fn (ChecklistItem $item): string => $item->text()->value, $items); + } +} diff --git a/tests/Feature/Modules/Jobs/TaskChecklistTest.php b/tests/Feature/Modules/Jobs/TaskChecklistTest.php new file mode 100644 index 0000000..47240ca --- /dev/null +++ b/tests/Feature/Modules/Jobs/TaskChecklistTest.php @@ -0,0 +1,127 @@ +employee = User::factory()->create(); + $this->actingAs($this->employee); + $this->job = CustomerJobModel::factory()->create(['status' => 'order', 'responsible_user_id' => $this->employee->id]); + $workType = WorkTypeModel::query()->where('name', 'graafinen työ')->sole(); + $this->task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Mainoskuvat']); + $this->params = ['jobId' => $this->job->id, 'taskId' => $this->task->id, 'taskTitle' => 'Mainoskuvat']; +}); + +/** + * @return list + */ +function checklistTexts(int $taskId): array +{ + return ChecklistItemModel::query()->where('task_id', $taskId)->orderBy('sort_order')->orderBy('id')->pluck('text')->all(); +} + +it('shows the checklist with progress on the job page', function () { + ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1, 'done_at' => now(), 'done_by' => $this->employee->id]); + ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Mikit', 'sort_order' => 2]); + + $this->get(route('jobs.show', $this->job->id)) + ->assertOk() + ->assertSee('Tarkistuslista') + ->assertSee('(1/2 tehty)') + ->assertSeeInOrder(['Valot', 'Mikit']); +}); + +it('adds, checks, renames, moves and removes checklist items', function () { + $component = Livewire::test('jobs::task-checklist', $this->params) + ->set('newItem', ' Valot ') + ->call('add') + ->assertHasNoErrors() + ->assertSet('newItem', '') + ->assertSee('Kohta Valot lisätty.') + ->set('newItem', 'Mikit') + ->call('add'); + + [$lights, $mics] = ChecklistItemModel::query()->orderBy('id')->pluck('id')->all(); + + $component->call('check', $lights, true) + ->assertSee('Kohta Valot merkitty tehdyksi.') + ->assertSee('(1/2 tehty)'); + + expect(ChecklistItemModel::query()->find($lights)->done_by)->toBe($this->employee->id); + + $component->call('startRename', $lights) + ->assertSet('editingItemId', $lights) + ->set('editingText', 'Valot ja jalustat') + ->call('saveRename') + ->assertSee('Kohta Valot ja jalustat tallennettu.') + ->assertSet('editingItemId', null) + ->call('move', $mics, 'up') + ->assertSee('Kohta Mikit siirretty ylöspäin.') + ->call('move', $mics, 'up') + ->assertSee('Kohta Mikit on jo ensimmäisenä.'); + + expect(checklistTexts($this->task->id))->toBe(['Mikit', 'Valot ja jalustat']); + + $component->call('remove', $mics)->assertSee('Kohta Mikit poistettu.'); + + expect(checklistTexts($this->task->id))->toBe(['Valot ja jalustat']); +}); + +it('shows Finnish errors for empty and too long items', function () { + Livewire::test('jobs::task-checklist', $this->params) + ->call('add') + ->assertHasErrors('newItem') + ->assertSee('Anna kohdan teksti.') + ->set('newItem', str_repeat('a', 256)) + ->call('add') + ->assertSee('Kohdan teksti voi olla enintään 255 merkkiä.'); + + expect(ChecklistItemModel::query()->count())->toBe(0); +}); + +it('does not change items of another task through this one', function () { + $other = ChecklistItemModel::query()->create(['task_id' => TaskModel::factory()->create()->id, 'text' => 'Vieras', 'sort_order' => 1]); + + Livewire::test('jobs::task-checklist', $this->params) + ->call('remove', $other->id) + ->assertSee('Kohtaa ei löytynyt. Päivitä sivu.'); + + expect($other->fresh())->not->toBeNull(); +}); + +it('lets an attached subcontractor check items but not manage them', function () { + $item = ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1]); + $subcontractor = User::factory()->subcontractor()->create(); + TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->task->work_type_id]); + $this->actingAs($subcontractor); + + Livewire::test('jobs::task-checklist', $this->params) + ->assertDontSee('Lisää kohta') + ->call('check', $item->id, true) + ->assertSee('Kohta Valot merkitty tehdyksi.') + ->call('remove', $item->id) + ->assertForbidden(); + + expect($item->fresh()->done_by)->toBe($subcontractor->id); +}); + +it('forbids the checklist of a job the subcontractor is not attached to', function () { + $this->actingAs(User::factory()->subcontractor()->create()); + + Livewire::test('jobs::task-checklist', $this->params)->assertForbidden(); +}); + +it('removes the checklist with its task', function () { + ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1]); + + $this->task->delete(); + + expect(ChecklistItemModel::query()->count())->toBe(0); +}); diff --git a/tests/Unit/Modules/Jobs/Application/ChecklistUseCasesTest.php b/tests/Unit/Modules/Jobs/Application/ChecklistUseCasesTest.php new file mode 100644 index 0000000..189fdc7 --- /dev/null +++ b/tests/Unit/Modules/Jobs/Application/ChecklistUseCasesTest.php @@ -0,0 +1,90 @@ +items = new InMemoryChecklistItemRepository; + $transactions = new FakeTransactionManager; + $finder = new ChecklistItemFinder($tasks, $this->items); + + $this->jobId = 5; + $this->taskId = (int) $tasks->save(Task::reconstitute(1, $this->jobId, 11, new TaskTitle('Kuvaus'), null, TaskStatus::Waiting, null, null, [], 10))->id(); + + $this->add = new AddChecklistItem($finder, $this->items, $transactions); + $this->rename = new RenameChecklistItem($finder, $this->items, $transactions); + $this->check = new CheckChecklistItem($finder, $this->items, $transactions, new FakeClock(new DateTimeImmutable('2026-11-24'))); + $this->move = new MoveChecklistItem($finder, $this->items, $transactions); + $this->remove = new RemoveChecklistItem($finder, $this->items, $transactions); +}); + +it('normalises checklist item text and rejects empty and too long text', function () { + expect((new ChecklistItemText(" Tarkista valot \n"))->value)->toBe('Tarkista valot') + ->and(fn () => new ChecklistItemText(' '))->toThrow(InvalidChecklistItemText::class) + ->and(fn () => new ChecklistItemText(str_repeat('a', 256)))->toThrow(InvalidChecklistItemText::class); +}); + +it('adds items to the end of the task checklist', function () { + $this->add->execute($this->jobId, $this->taskId, 'Valot'); + $this->add->execute($this->jobId, $this->taskId, 'Mikit'); + + expect($this->items->textsOf($this->taskId))->toBe(['Valot', 'Mikit']); +}); + +it('does not touch the checklist of a task through another job', function () { + $itemId = $this->add->execute($this->jobId, $this->taskId, 'Valot'); + + expect(fn () => $this->add->execute(99, $this->taskId, 'Mikit'))->toThrow(TaskNotFound::class) + ->and(fn () => $this->remove->execute(99, $this->taskId, $itemId))->toThrow(TaskNotFound::class) + ->and(fn () => $this->remove->execute($this->jobId, $this->taskId, 404))->toThrow(ChecklistItemNotFound::class); +}); + +it('checks an item with who and when, keeps them on a second check and clears them on uncheck', function () { + $itemId = $this->add->execute($this->jobId, $this->taskId, 'Valot'); + + $this->check->execute($this->jobId, $this->taskId, $itemId, true, 10); + $this->check->execute($this->jobId, $this->taskId, $itemId, true, 12); + $item = $this->items->items[$itemId]; + + expect($item->isDone())->toBeTrue() + ->and($item->doneBy())->toBe(10) + ->and($item->doneAt()?->format('Y-m-d H:i'))->toBe('2026-11-24 12:00'); + + $this->check->execute($this->jobId, $this->taskId, $itemId, false, 10); + + expect($this->items->items[$itemId]->isDone())->toBeFalse() + ->and($this->items->items[$itemId]->doneBy())->toBeNull(); +}); + +it('renames, moves and removes items', function () { + $first = $this->add->execute($this->jobId, $this->taskId, 'Valot'); + $second = $this->add->execute($this->jobId, $this->taskId, 'Mikit'); + + $this->rename->execute($this->jobId, $this->taskId, $first, 'Valot ja jalustat'); + + expect($this->move->execute($this->jobId, $this->taskId, $first, MoveDirection::Up))->toBeFalse() + ->and($this->move->execute($this->jobId, $this->taskId, $second, MoveDirection::Down))->toBeFalse() + ->and($this->move->execute($this->jobId, $this->taskId, $first, MoveDirection::Down))->toBeTrue() + ->and($this->items->textsOf($this->taskId))->toBe(['Mikit', 'Valot ja jalustat']); + + $this->remove->execute($this->jobId, $this->taskId, $second); + + expect($this->items->textsOf($this->taskId))->toBe(['Valot ja jalustat']); +});