Files
Toiminnanohjaus/app/Modules/Jobs/Presentation/Livewire/Concerns/PicksCustomer.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

103 lines
3.1 KiB
PHP

<?php
namespace App\Modules\Jobs\Presentation\Livewire\Concerns;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\DTOs\CustomerDetailsData;
use App\Modules\Customers\Application\DTOs\CustomerSummary;
use App\Modules\Customers\Application\UseCases\RegisterCustomer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
/**
* Customer choice for job pages, rendered with <x-jobs.customer-picker>:
* a search field whose results are buttons, and a quick add by name. The
* page has a form object with a `customer_id` string and may override
* customerChosen() to clear fields that depend on the customer.
*
* @property-read list<CustomerSummary> $customerResults
*/
trait PicksCustomer
{
public string $customerSearch = '';
#[Locked]
public string $customerName = '';
public string $statusMessage = '';
/**
* @return list<CustomerSummary>
*/
#[Computed]
public function customerResults(): array
{
$term = trim($this->customerSearch);
return mb_strlen($term) < 2 ? [] : app(CustomerQueries::class)->search($term, 8);
}
public function selectCustomer(int $customerId): void
{
$this->authorize('jobs.manage');
$customer = app(CustomerQueries::class)->findSummary($customerId);
if ($customer !== null) {
$this->useCustomer($customer->id, $customer->name);
}
}
public function createCustomer(RegisterCustomer $registerCustomer): void
{
$this->authorize('jobs.manage');
$this->authorize('customers.use');
$name = trim($this->customerSearch);
try {
$customerId = $registerCustomer->execute(new CustomerDetailsData($name, CustomerType::Company));
} catch (DomainException) {
$this->addError('customerSearch', __('customers.errors.invalid_name'));
return;
}
$this->useCustomer($customerId, $name);
$this->statusMessage = __('jobs.customer_picker.created', ['name' => $name]);
}
public function clearCustomer(): void
{
$this->form->customer_id = '';
$this->customerName = '';
$this->statusMessage = '';
$this->customerChosen(null);
$this->js("document.getElementById('customer-search')?.focus()");
}
/**
* Called when the customer changes; null when it was cleared.
*/
protected function customerChosen(?int $customerId): void {}
private function useCustomer(int $customerId, string $name): void
{
$changed = (string) $customerId !== $this->form->customer_id;
$this->form->customer_id = (string) $customerId;
$this->customerName = $name;
$this->customerSearch = '';
$this->resetErrorBag(['customerSearch', 'form.customer_id']);
if ($changed) {
$this->customerChosen($customerId);
}
$this->js("document.getElementById('change-customer')?.focus()");
}
}