Files
Toiminnanohjaus/app/Modules/Jobs/Presentation/Livewire/⚡form.blade.php
T
petriandClaude Opus 5.5 b3d8e66477 Add quick job intake for phones
Phase 1, item 5:
- New page /jobs/quick (Pikavastaanotto): customer (search or quick
  add), name, short description, service unit, responsible person and
  starting status (Alustava or Prospekti) in one column. The job is
  created with CreateCustomerJob, so numbering, reference checks and
  history are the same as on the full form.
- After saving, the form clears for the next job (responsible person
  and status stay), and a status message with the job number and links
  to the job and its edit page takes focus.
- The customer search of the job form moves to a PicksCustomer trait and
  an <x-jobs.customer-picker> component shared by both pages.
- Menu link and a list button for users who may open jobs; nav items
  accept several route patterns.
- PHPStan ignores unused-trait only for Livewire Concerns, which are
  used from excluded single-file components.
- Spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-23 19:07:25 +02:00

518 lines
21 KiB
PHP

<?php
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\DTOs\BillingUnitSummary;
use App\Modules\Customers\Application\DTOs\ContactSummary;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\DTOs\CustomerJobDetails;
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
use App\Modules\Jobs\Application\UseCases\UpdateCustomerJob;
use App\Modules\Jobs\Domain\Enums\JobKind;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\PaymentBasis;
use App\Modules\Jobs\Domain\Enums\Priority;
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
use App\Modules\Jobs\Presentation\Livewire\Concerns\PicksCustomer;
use App\Modules\Jobs\Presentation\Livewire\Forms\JobForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Application\DTOs\UserOption;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Create page (no {job}) and edit page (with {job}) of a customer job.
*/
new class extends Component
{
use PicksCustomer;
#[Locked]
public ?int $jobId = null;
#[Locked]
public string $jobNumber = '';
/**
* The status stored before this edit: always offered, even when the
* user could not set it themselves.
*/
#[Locked]
public ?string $storedStatus = null;
public JobForm $form;
public string $newTag = '';
public function mount(CustomerJobQueries $jobQueries, ?int $job = null): void
{
$this->authorize('jobs.manage');
if ($job === null) {
$this->form->responsible_user_id = (string) auth()->id();
return;
}
$details = $jobQueries->findDetails($job);
abort_if($details === null, 404);
$this->jobId = $details->id;
$this->jobNumber = $details->number;
$this->customerName = $details->customer->name;
$this->storedStatus = $details->status->value;
$this->form->fillFromDetails($details);
}
/**
* @return list<array{value: int, label: string}>
*/
#[Computed]
public function serviceUnitOptions(): array
{
$catalog = app(CatalogQueries::class);
$options = [];
foreach ($catalog->activeServiceUnits() as $unit) {
$options[$unit->id] = ['value' => $unit->id, 'label' => $unit->name];
}
// Units already on the job stay visible after being taken out of use.
foreach ($this->form->service_unit_ids as $id) {
if (! isset($options[(int) $id]) && ($unit = $catalog->findServiceUnit((int) $id)) !== null) {
$options[$unit->id] = ['value' => $unit->id, 'label' => $unit->name, 'note' => __('catalog.inactive')];
}
}
return array_values($options);
}
/**
* @return list<UserOption>
*/
#[Computed]
public function users(): array
{
return app(UserAccountQueries::class)->activeUserOptions();
}
/**
* @return list<ContactSummary>
*/
#[Computed]
public function contacts(): array
{
return $this->form->customer_id === '' ? [] : app(CustomerQueries::class)->contactsOf((int) $this->form->customer_id);
}
/**
* @return list<BillingUnitSummary>
*/
#[Computed]
public function billingUnits(): array
{
return $this->form->customer_id === '' ? [] : app(CustomerQueries::class)->billingUnitsOf((int) $this->form->customer_id);
}
/**
* Statuses this user may choose: billing statuses for management and the
* job's biller, archived for management, and always the stored one.
*
* @return list<JobStatus>
*/
#[Computed]
public function statusOptions(): array
{
return array_values(array_filter(
JobStatus::cases(),
fn (JobStatus $status): bool => $status->value === $this->storedStatus || $this->maySetStatus($status),
));
}
/**
* @return list<string>
*/
#[Computed]
public function tagSuggestions(): array
{
return array_values(array_diff(app(CustomerJobQueries::class)->tagSuggestions($this->newTag), $this->form->tags));
}
public function addTag(): void
{
$tag = trim(preg_replace('/\s+/u', ' ', $this->newTag) ?? '');
if ($tag === '' || mb_strlen($tag) > 50) {
$this->addError('newTag', __('jobs.errors.invalid_tag'));
return;
}
$taken = array_map('mb_strtolower', $this->form->tags);
if (! in_array(mb_strtolower($tag), $taken, true)) {
$this->form->tags[] = $tag;
}
$this->newTag = '';
$this->resetErrorBag('newTag');
$this->statusMessage = __('jobs.tags.added', ['tag' => $tag]);
}
public function removeTag(int $index): void
{
$tag = $this->form->tags[$index] ?? null;
if ($tag === null) {
return;
}
unset($this->form->tags[$index]);
$this->form->tags = array_values($this->form->tags);
$this->statusMessage = __('jobs.tags.removed', ['tag' => $tag]);
$this->js("document.getElementById('new-tag')?.focus()");
}
public function addLink(): void
{
$this->form->links[] = ['title' => '', 'url' => ''];
$index = count($this->form->links) - 1;
$this->js("document.getElementById('link-title-{$index}')?.focus()");
}
public function removeLink(int $index): void
{
unset($this->form->links[$index]);
$this->form->links = array_values($this->form->links);
$this->js("document.getElementById('add-link')?.focus()");
}
public function save(CreateCustomerJob $createJob, UpdateCustomerJob $updateJob, CustomerJobQueries $jobQueries): void
{
$this->authorize('jobs.manage');
$status = JobStatus::tryFrom($this->form->status);
if ($status !== null && $status->value !== $this->storedStatus && ! $this->maySetStatus($status)) {
$this->addError('form.status', __('jobs.errors.status_not_allowed'));
return;
}
$data = $this->form->toData();
try {
if ($this->jobId === null) {
$jobId = $createJob->execute($data, (int) auth()->id());
$number = $jobQueries->findDetails($jobId)->number ?? '';
session()->flash('status', __('jobs.created', ['number' => $number, 'name' => trim($data->name)]));
} else {
$updateJob->execute($this->jobId, $data, (int) auth()->id());
$jobId = $this->jobId;
session()->flash('status', __('jobs.updated', ['number' => $this->jobNumber]));
}
} catch (CustomerJobNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
$this->redirectRoute('jobs.show', $jobId, navigate: true);
}
public function heading(): string
{
return $this->jobId === null ? __('jobs.create_title') : __('jobs.edit_title').' '.$this->jobNumber;
}
public function render(): mixed
{
return $this->view()->title($this->heading());
}
private function maySetStatus(JobStatus $status): bool
{
if ($status === JobStatus::Archived) {
return Gate::allows('jobs.archive');
}
if ($status->isBillingStatus()) {
$details = $this->jobId === null ? null : app(CustomerJobQueries::class)->findDetails($this->jobId);
return Gate::allows('jobs.set-billing-status', [$details]);
}
return true;
}
/**
* A contact or billing unit of another customer no longer fits.
*/
protected function customerChosen(?int $customerId): void
{
$this->form->customer_contact_id = '';
$this->form->billing_unit_id = '';
}
};
?>
@php
$userOptions = collect($this->users)->map(fn ($user) => [
'value' => $user->id,
'label' => $user->name,
'note' => $user->role === Role::Subcontractor ? __('users.roles.subcontractor') : null,
])->all();
$staffOptions = collect($this->users)->reject(fn ($user) => $user->role === Role::Subcontractor);
@endphp
<div class="mx-auto max-w-3xl space-y-6 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">{{ $this->heading() }}</h1>
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
<form wire:submit="save" class="space-y-8" novalidate>
<fieldset class="space-y-6">
<legend class="text-lg font-semibold">{{ __('jobs.sections.basics') }}</legend>
<x-input wire:model="form.name" :label="__('jobs.fields.name')" autocomplete="off" required />
<x-jobs.customer-picker
:customer-id="$form->customer_id"
:customer-name="$customerName"
:search="$customerSearch"
:results="$this->customerResults"
/>
<x-textarea wire:model="form.description" :label="__('jobs.fields.description')" resize-auto />
<x-checkbox-group
:legend="__('jobs.fields.service_units')"
model="form.service_unit_ids"
:options="$this->serviceUnitOptions"
/>
</fieldset>
<fieldset class="space-y-6">
<legend class="text-lg font-semibold">{{ __('jobs.sections.schedule') }}</legend>
<div class="grid gap-6 sm:grid-cols-2">
<x-input wire:model="form.starts_on" type="date" :label="__('jobs.fields.starts_on')" />
<x-input wire:model="form.due_on" type="date" :label="__('jobs.fields.due_on')" />
<x-input wire:model="form.planned_from" type="date" :label="__('jobs.fields.planned_from')" />
<x-input wire:model="form.planned_to" type="date" :label="__('jobs.fields.planned_to')" />
</div>
</fieldset>
<fieldset class="space-y-6">
<legend class="text-lg font-semibold">{{ __('jobs.sections.classification') }}</legend>
<div class="grid gap-6 sm:grid-cols-2">
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native wire:model="form.status" :label="__('jobs.fields.status')" :aria-label="__('jobs.fields.status')">
@foreach ($this->statusOptions as $status)
<option value="{{ $status->value }}">{{ __('jobs.statuses.'.$status->value) }}</option>
@endforeach
</x-select.native>
<x-select.native wire:model="form.priority" :label="__('jobs.fields.priority')" :aria-label="__('jobs.fields.priority')">
@foreach (Priority::cases() as $priority)
<option value="{{ $priority->value }}">{{ __('jobs.priorities.'.$priority->value) }}</option>
@endforeach
</x-select.native>
<x-select.native wire:model="form.kind" :label="__('jobs.fields.kind')" :aria-label="__('jobs.fields.kind')">
@foreach (JobKind::cases() as $kind)
<option value="{{ $kind->value }}">{{ __('jobs.kinds.'.$kind->value) }}</option>
@endforeach
</x-select.native>
<x-select.native wire:model="form.payment_basis" :label="__('jobs.fields.payment_basis')" :aria-label="__('jobs.fields.payment_basis')">
<option value="">{{ __('jobs.fields.no_payment_basis') }}</option>
@foreach (PaymentBasis::cases() as $basis)
<option value="{{ $basis->value }}">{{ __('jobs.payment_bases.'.$basis->value) }}</option>
@endforeach
</x-select.native>
<x-input
wire:model="form.estimated_hours"
type="number"
step="0.25"
min="0"
inputmode="decimal"
:label="__('jobs.fields.estimated_hours')"
:hint="__('jobs.fields.estimated_hours_description')"
/>
</div>
</fieldset>
<fieldset class="space-y-6">
<legend class="text-lg font-semibold">{{ __('jobs.sections.members') }}</legend>
<div class="grid gap-6 sm:grid-cols-2">
<x-select.native wire:model="form.responsible_user_id" :label="__('jobs.fields.responsible_user')" :aria-label="__('jobs.fields.responsible_user')" required>
<option value="">{{ __('jobs.fields.none_selected') }}</option>
@foreach ($staffOptions as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</x-select.native>
<x-select.native wire:model="form.contact_user_id" :label="__('jobs.fields.contact_user')" :aria-label="__('jobs.fields.contact_user')" :hint="__('jobs.fields.contact_user_description')">
<option value="">{{ __('jobs.fields.none_selected') }}</option>
@foreach ($staffOptions as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</x-select.native>
<x-select.native wire:model="form.biller_user_id" :label="__('jobs.fields.biller_user')" :aria-label="__('jobs.fields.biller_user')" :hint="__('jobs.fields.biller_user_description')">
<option value="">{{ __('jobs.fields.none_selected') }}</option>
@foreach ($staffOptions as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</x-select.native>
<x-select.native
wire:model="form.customer_contact_id"
:label="__('jobs.fields.customer_contact')"
:aria-label="__('jobs.fields.customer_contact')"
:hint="$form->customer_id === '' ? __('jobs.customer_picker.choose_customer_first') : null"
:disabled="$form->customer_id === ''"
>
<option value="">{{ __('jobs.fields.none_selected') }}</option>
@foreach ($this->contacts as $contact)
<option wire:key="contact-{{ $contact->id }}" value="{{ $contact->id }}">{{ $contact->name }}</option>
@endforeach
</x-select.native>
<x-select.native
wire:model="form.billing_unit_id"
:label="__('jobs.fields.billing_unit')"
:aria-label="__('jobs.fields.billing_unit')"
:hint="$form->customer_id === '' ? __('jobs.customer_picker.choose_customer_first') : null"
:disabled="$form->customer_id === ''"
>
<option value="">{{ __('jobs.fields.none_selected') }}</option>
@foreach ($this->billingUnits as $billingUnit)
<option wire:key="billing-unit-{{ $billingUnit->id }}" value="{{ $billingUnit->id }}">{{ $billingUnit->name }}</option>
@endforeach
</x-select.native>
</div>
<x-checkbox-group
:legend="__('jobs.fields.members')"
:hint="__('jobs.fields.members_description')"
model="form.member_user_ids"
:options="$userOptions"
/>
</fieldset>
<fieldset class="space-y-6">
<legend class="text-lg font-semibold">{{ __('jobs.sections.extra') }}</legend>
<x-input wire:model="form.customer_reference" :label="__('jobs.fields.customer_reference')" autocomplete="off" />
{{-- Tags: free text with suggestions from earlier tags, added one at a time. --}}
<div class="space-y-3">
<div class="flex flex-wrap items-end gap-3">
<div class="min-w-0 flex-1">
<x-input
id="new-tag"
wire:model.live.debounce.300ms="newTag"
wire:keydown.enter.prevent="addTag"
list="tag-suggestions"
:label="__('jobs.tags.new')"
:hint="__('jobs.tags.new_description')"
autocomplete="off"
/>
<datalist id="tag-suggestions">
@foreach ($this->tagSuggestions as $suggestion)
<option value="{{ $suggestion }}"></option>
@endforeach
</datalist>
</div>
<x-button wire:click="addTag" subtle color="black" icon="plus" :text="__('jobs.tags.add')" />
</div>
<p id="tags-label" class="text-sm font-semibold text-gray-700 dark:text-dark-300">{{ __('jobs.fields.tags') }}</p>
@if ($form->tags === [])
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.tags.none') }}</p>
@else
<ul aria-labelledby="tags-label" class="flex flex-wrap gap-2">
@foreach ($form->tags as $index => $tag)
<li wire:key="tag-{{ $tag }}" class="inline-flex items-center gap-1 rounded-full border border-zinc-300 py-1 pr-1 pl-3 text-sm dark:border-zinc-600">
{{ $tag }}
<x-button.circle
wire:click="removeTag({{ $index }})"
subtle
color="black"
xs
icon="x-mark"
:aria-label="__('jobs.tags.remove', ['tag' => $tag])"
/>
</li>
@endforeach
</ul>
@endif
@error('form.tags')
<p class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
</div>
{{-- Links: title and address per row. --}}
<div class="space-y-3">
<p class="text-sm font-semibold text-gray-700 dark:text-dark-300">{{ __('jobs.fields.links') }}</p>
@if ($form->links === [])
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('jobs.links.none') }}</p>
@endif
@foreach ($form->links as $index => $link)
<fieldset wire:key="link-{{ $index }}" class="space-y-3 rounded-lg border border-zinc-200 p-3 dark:border-zinc-700">
<legend class="px-1 text-sm font-medium">{{ __('jobs.links.link_number', ['number' => $index + 1]) }}</legend>
<div class="grid gap-3 sm:grid-cols-2">
<x-input id="link-title-{{ $index }}" wire:model="form.links.{{ $index }}.title" :label="__('jobs.links.title')" autocomplete="off" />
<x-input id="link-url-{{ $index }}" wire:model="form.links.{{ $index }}.url" type="url" :label="__('jobs.links.url')" :hint="__('jobs.links.url_description')" autocomplete="off" />
</div>
<x-button
wire:click="removeLink({{ $index }})"
subtle
color="black"
sm
icon="trash"
:text="__('jobs.links.remove', ['number' => $index + 1])"
/>
</fieldset>
@endforeach
<x-button id="add-link" wire:click="addLink" subtle color="black" sm icon="plus" :text="__('jobs.links.add')" />
@error('form.links')
<p class="text-sm text-red-700 dark:text-red-400">{{ $message }}</p>
@enderror
</div>
<x-textarea wire:model="form.instructions" :label="__('jobs.fields.instructions')" :hint="__('jobs.fields.instructions_description')" resize-auto />
</fieldset>
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('jobs.save')" />
<x-button
:href="$jobId === null ? route('jobs.index') : route('jobs.show', $jobId)"
wire:navigate
subtle
color="black"
:text="__('jobs.cancel')"
/>
</div>
</form>
</div>