Add customer jobs with numbering, members, tags, links and history

Phase 1, item 4. New Jobs module:
- Customer jobs with the spec's fields except money (phase 2): yearly
  running numbers (2026-0001) from a locked counter row, service units,
  schedule, kind, status, priority, payment basis, estimated hours,
  PO reference, instructions, members (responsible, contact person,
  customer contact, biller, billing unit, other members), tags shared
  across jobs, and links.
- Every change is recorded field by field and shown on the job page with
  names and Finnish dates.
- References to other modules are checked through their query
  contracts: contact and billing unit must be the customer's own;
  service units and users must be in use unless already on the job.
- Job list with search, filters and "my jobs" on by default; job form
  with an accessible customer search and quick add; job page.
- Access: subcontractors see only jobs they are members of, read only;
  billing statuses for management and the job's biller; archiving for
  management.

Related changes:
- Customers: one internal customer (created by a migration) for internal
  work; contacts and billing units used by a job cannot be removed.
- Users and Catalog: batch name lookups and active user options with
  roles for other modules.
- <x-checkbox-group> component; spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-23 18:57:18 +02:00
co-authored by Claude Opus 5.5
parent 51b2d4a252
commit 70e107f6e2
95 changed files with 4631 additions and 26 deletions
@@ -0,0 +1,240 @@
<?php
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\DTOs\CustomerJobDetails;
use App\Modules\Jobs\Application\DTOs\JobChangeEntry;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use Carbon\CarbonImmutable;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* A customer job's page: details, members, links, instructions and history.
*/
new class extends Component
{
#[Locked]
public int $jobId;
public function mount(CustomerJobQueries $jobQueries, int $job): void
{
$this->authorize('jobs.view');
abort_if($jobQueries->findDetails($job) === null, 404);
$this->authorize('jobs.view-job', $job);
$this->jobId = $job;
}
#[Computed]
public function job(): CustomerJobDetails
{
return app(CustomerJobQueries::class)->findDetails($this->jobId) ?? abort(404);
}
/**
* @return list<JobChangeEntry>
*/
#[Computed]
public function changes(): array
{
return app(CustomerJobQueries::class)->changes($this->jobId);
}
public function formatDate(?string $date): string
{
return $date === null ? __('jobs.not_set') : CarbonImmutable::parse($date)->format('j.n.Y');
}
/**
* A history value as text: names for ids, labels for enum keys,
* Finnish dates.
*/
public function historyValue(string $field, ?string $value): string
{
if ($value === null || $value === '') {
return __('jobs.not_set');
}
$ids = array_map('intval', explode(',', $value));
$named = fn (array $names): string => implode(', ', array_map(
fn (int $id): string => $names[$id] ?? __('jobs.history.unknown', ['id' => $id]),
$ids,
));
return match ($field) {
'starts_on', 'due_on', 'planned_from', 'planned_to' => $this->formatDate($value),
'kind' => __('jobs.kinds.'.$value),
'status' => __('jobs.statuses.'.$value),
'priority' => __('jobs.priorities.'.$value),
'payment_basis' => __('jobs.payment_bases.'.$value),
'customer' => $named(app(CustomerQueries::class)->namesByIds($ids)),
'service_units' => $named(app(CatalogQueries::class)->serviceUnitNames($ids)),
'responsible_user', 'contact_user', 'biller_user', 'members' => $named(app(UserAccountQueries::class)->namesByIds($ids)),
'customer_contact' => app(CustomerQueries::class)->findContact($this->job->customer->id, $ids[0])->name ?? __('jobs.history.unknown', ['id' => $ids[0]]),
'billing_unit' => app(CustomerQueries::class)->findBillingUnit($this->job->customer->id, $ids[0])->name ?? __('jobs.history.unknown', ['id' => $ids[0]]),
default => $value,
};
}
public function render(): mixed
{
return $this->view()->title($this->job->number.' '.$this->job->name);
}
};
?>
@php($job = $this->job)
<div class="mx-auto max-w-5xl space-y-8 p-4 sm:p-6">
<div class="space-y-4">
<div class="flex flex-wrap items-start justify-between gap-4">
<h1 class="text-2xl font-semibold break-words">
<span class="whitespace-nowrap">{{ $job->number }}</span> {{ $job->name }}
</h1>
@can('jobs.manage')
<x-button :href="route('jobs.edit', $job->id)" wire:navigate subtle color="black" icon="pencil" :text="__('jobs.edit')" />
@endcan
</div>
@if (session('status'))
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
@endif
<dl class="flex flex-wrap gap-x-8 gap-y-2 text-sm">
<div class="flex gap-2"><dt class="font-medium">{{ __('jobs.fields.status') }}:</dt><dd>{{ __('jobs.statuses.'.$job->status->value) }}</dd></div>
<div class="flex gap-2"><dt class="font-medium">{{ __('jobs.fields.priority') }}:</dt><dd><x-jobs.priority :priority="$job->priority" /></dd></div>
<div class="flex gap-2"><dt class="font-medium">{{ __('jobs.fields.due_on') }}:</dt><dd><x-jobs.due-date :date="$job->dueOn" :status="$job->status" /></dd></div>
</dl>
</div>
<section aria-labelledby="basics-heading" class="space-y-3">
<h2 id="basics-heading" class="text-xl font-semibold">{{ __('jobs.sections.basics') }}</h2>
<dl class="grid gap-x-6 gap-y-2 text-sm sm:grid-cols-[max-content_1fr]">
<dt class="font-medium">{{ __('jobs.fields.customer') }}</dt>
<dd>
@can('customers.use')
<x-link :href="route('customers.show', $job->customer->id)" navigate>{{ $job->customer->name }}</x-link>
@else
{{ $job->customer->name }}
@endcan
</dd>
<dt class="font-medium">{{ __('jobs.fields.service_units') }}</dt>
<dd>{{ collect($job->serviceUnits)->pluck('name')->join(', ') }}</dd>
<dt class="font-medium">{{ __('jobs.fields.kind') }}</dt>
<dd>{{ __('jobs.kinds.'.$job->kind->value) }}</dd>
<dt class="font-medium">{{ __('jobs.fields.payment_basis') }}</dt>
<dd>{{ $job->paymentBasis === null ? __('jobs.not_set') : __('jobs.payment_bases.'.$job->paymentBasis->value) }}</dd>
<dt class="font-medium">{{ __('jobs.fields.estimated_hours') }}</dt>
<dd>{{ $job->estimatedHours === null ? __('jobs.not_set') : str_replace('.', ',', $job->estimatedHours).' h' }}</dd>
<dt class="font-medium">{{ __('jobs.fields.customer_reference') }}</dt>
<dd class="break-words">{{ $job->customerReference ?? __('jobs.not_set') }}</dd>
<dt class="font-medium">{{ __('jobs.fields.tags') }}</dt>
<dd>{{ $job->tags === [] ? __('jobs.tags.none') : implode(', ', $job->tags) }}</dd>
<dt class="font-medium">{{ __('jobs.fields.created_at') }}</dt>
<dd>{{ CarbonImmutable::parse($job->createdAt)->format('j.n.Y H.i') }}, {{ $job->createdByName }}</dd>
</dl>
@if ($job->description !== null)
<h3 class="text-base font-semibold">{{ __('jobs.fields.description') }}</h3>
<p class="whitespace-pre-line text-sm">{{ $job->description }}</p>
@endif
</section>
<section aria-labelledby="schedule-heading" class="space-y-3">
<h2 id="schedule-heading" class="text-xl font-semibold">{{ __('jobs.sections.schedule') }}</h2>
<dl class="grid gap-x-6 gap-y-2 text-sm sm:grid-cols-[max-content_1fr]">
<dt class="font-medium">{{ __('jobs.fields.starts_on') }}</dt>
<dd>{{ $this->formatDate($job->startsOn) }}</dd>
<dt class="font-medium">{{ __('jobs.fields.due_on') }}</dt>
<dd><x-jobs.due-date :date="$job->dueOn" :status="$job->status" /></dd>
<dt class="font-medium">{{ __('jobs.fields.planned_from') }}</dt>
<dd>{{ $this->formatDate($job->plannedFrom) }}</dd>
<dt class="font-medium">{{ __('jobs.fields.planned_to') }}</dt>
<dd>{{ $this->formatDate($job->plannedTo) }}</dd>
</dl>
</section>
<section aria-labelledby="members-heading" class="space-y-3">
<h2 id="members-heading" class="text-xl font-semibold">{{ __('jobs.sections.members') }}</h2>
<dl class="grid gap-x-6 gap-y-2 text-sm sm:grid-cols-[max-content_1fr]">
<dt class="font-medium">{{ __('jobs.fields.responsible_user') }}</dt>
<dd>{{ $job->responsible->name }}</dd>
<dt class="font-medium">{{ __('jobs.fields.contact_user') }}</dt>
<dd>{{ $job->contactUser->name ?? __('jobs.not_set') }}</dd>
<dt class="font-medium">{{ __('jobs.fields.customer_contact') }}</dt>
<dd>{{ $job->customerContact->name ?? __('jobs.not_set') }}</dd>
<dt class="font-medium">{{ __('jobs.fields.biller_user') }}</dt>
<dd>{{ $job->biller->name ?? __('jobs.not_set') }}</dd>
<dt class="font-medium">{{ __('jobs.fields.billing_unit') }}</dt>
<dd>{{ $job->billingUnit->name ?? __('jobs.not_set') }}</dd>
<dt class="font-medium">{{ __('jobs.fields.members') }}</dt>
<dd>{{ $job->members === [] ? __('jobs.not_set') : collect($job->members)->pluck('name')->join(', ') }}</dd>
</dl>
</section>
<section aria-labelledby="links-heading" class="space-y-3">
<h2 id="links-heading" class="text-xl font-semibold">{{ __('jobs.sections.links') }}</h2>
@if ($job->links === [])
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.links.none') }}</p>
@else
<ul class="list-disc space-y-1 pl-5 text-sm">
@foreach ($job->links as $link)
<li class="break-all">
<x-link :href="$link['url']" target="_blank" rel="noopener noreferrer">
{{ $link['title'] }}<span class="sr-only"> ({{ __('jobs.opens_in_new_tab') }})</span>
</x-link>
</li>
@endforeach
</ul>
@endif
</section>
<section aria-labelledby="instructions-heading" class="space-y-3">
<h2 id="instructions-heading" class="text-xl font-semibold">{{ __('jobs.sections.instructions') }}</h2>
<p class="whitespace-pre-line text-sm">{{ $job->instructions ?? __('jobs.not_set') }}</p>
</section>
<section aria-labelledby="history-heading" class="space-y-3">
<h2 id="history-heading" class="text-xl font-semibold">{{ __('jobs.sections.history') }}</h2>
@if ($this->changes === [])
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.history.empty') }}</p>
@else
<ol class="space-y-2 text-sm">
@foreach ($this->changes as $change)
<li class="border-b border-zinc-200 pb-2 dark:border-zinc-700">
@if ($change->field === 'created')
{{ __('jobs.history.created', ['user' => $change->userName, 'number' => $change->newValue]) }}
@else
<span class="break-words">{{ __('jobs.history.changed', [
'field' => __('jobs.fields.'.$change->field),
'old' => $this->historyValue($change->field, $change->oldValue),
'new' => $this->historyValue($change->field, $change->newValue),
]) }}</span>
@endif
<span class="block text-zinc-600 dark:text-zinc-300">
{{ __('jobs.history.by', ['user' => $change->userName, 'time' => CarbonImmutable::parse($change->changedAt)->format('j.n.Y H.i')]) }}
</span>
</li>
@endforeach
</ol>
@endif
</section>
</div>