Files
Toiminnanohjaus/app/Modules/Customers/Presentation/Livewire/⚡form.blade.php
T
petriandClaude Opus 5.5 e0b8f4970f Add customer contacts, billing units and consumer personal identity codes
Customer page and contacts/billing units (phase 1, item 2):
- New customer page (customers.show) with details, contacts and billing
  units; add and edit on their own pages, delete after confirmation.
  Creating or editing a customer now leads to this page.
- Contacts: name, title, phone, email, note. Billing units: name,
  billing address, e-invoice address (12-35 letters/digits), operator
  (required with an e-invoice address) and reference.
- Use cases check that the row belongs to the customer, so ids in the
  URL can't reach another customer's rows.
- EmailAddress moves from Users to Shared so both modules can use it.
- <x-confirm-dialog> (native <dialog>) replaces <x-modal>, whose title,
  close button and focus handling are not accessible; solid red buttons
  use red-700 via published NormalButtonColors for 4.5:1 contrast.

Personal identity code (henkilötunnus) for consumers who are invoiced:
- Validated (date, century sign, check character), stored encrypted
  with an HMAC blind index for duplicate checks; never in
  CustomerSummary, search, logs or Livewire state.
- Admins and management see it in full and may remove it; others enter
  it and see only the masked form. The form never shows the stored code.

Spec, architecture doc and CLAUDE.md updated.

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

146 lines
5.1 KiB
PHP

<?php
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\UseCases\RegisterCustomer;
use App\Modules\Customers\Application\UseCases\UpdateCustomer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Presentation\Livewire\Forms\CustomerForm;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Create page (no route parameter) and edit page (with {customer}).
*/
new class extends Component
{
#[Locked]
public ?int $customerId = null;
public CustomerForm $form;
/**
* The stored personal identity code, masked (e.g. ••••••-308T). The
* full code never reaches the form.
*/
#[Locked]
public ?string $storedPersonalIdentityCode = null;
public function mount(CustomerQueries $customerQueries, ?int $customer = null): void
{
$this->authorize('customers.use');
if ($customer === null) {
return;
}
$summary = $customerQueries->findSummary($customer);
abort_if($summary === null, 404);
$this->customerId = $summary->id;
$this->storedPersonalIdentityCode = $summary->personalIdentityCodeMasked;
$this->form->fillFromSummary($summary);
}
public function save(RegisterCustomer $registerCustomer, UpdateCustomer $updateCustomer): void
{
$this->authorize('customers.use');
if ($this->form->remove_personal_identity_code) {
$this->authorize('customers.view-personal-identity-code');
}
$data = $this->form->toData();
try {
if ($this->customerId === null) {
$customerId = $registerCustomer->execute($data);
session()->flash('status', __('customers.created', ['name' => $data->name]));
} else {
$updateCustomer->execute($this->customerId, $data);
$customerId = $this->customerId;
session()->flash('status', __('customers.updated', ['name' => $data->name]));
}
} catch (CustomerNotFound) {
abort(404);
} catch (DomainException $exception) {
$this->form->showDomainError($exception);
return;
}
// The customer's page, where contacts and billing units are added.
$this->redirectRoute('customers.show', $customerId, navigate: true);
}
public function render(): mixed
{
return $this->view()->title(__($this->customerId === null ? 'customers.create_title' : 'customers.edit_title'));
}
};
?>
<div class="mx-auto max-w-2xl space-y-6 p-4 sm:p-6">
<h1 class="text-2xl font-semibold">
{{ __($customerId === null ? 'customers.create_title' : 'customers.edit_title') }}
</h1>
<form wire:submit="save" class="space-y-6" novalidate>
<x-input
wire:model="form.name"
:label="__('customers.fields.name')"
autocomplete="organization"
required
/>
<x-radio.group
wire:model.live="form.type"
:label="__('customers.fields.type')"
:options="collect(CustomerType::cases())->map(fn (CustomerType $type) => ['label' => __('customers.types.'.$type->value), 'value' => $type->value])->all()"
/>
@if ($form->type === CustomerType::Company->value)
{{-- No inputmode="numeric": the iOS number pad has no hyphen for 1234567-8. --}}
<x-input
wire:model="form.business_id"
:label="__('customers.fields.business_id')"
:hint="__('customers.fields.business_id_description')"
autocomplete="off"
/>
@else
{{-- Never filled from the stored code; type="text" because the code has letters. --}}
<x-input
wire:model="form.personal_identity_code"
:label="__('customers.fields.personal_identity_code')"
:hint="$storedPersonalIdentityCode === null
? __('customers.fields.personal_identity_code_description')
: __('customers.fields.personal_identity_code_stored', ['code' => $storedPersonalIdentityCode])"
autocomplete="off"
spellcheck="false"
/>
@if ($storedPersonalIdentityCode !== null)
@can('customers.view-personal-identity-code')
<x-checkbox
wire:model="form.remove_personal_identity_code"
:label="__('customers.fields.remove_personal_identity_code')"
/>
@endcan
@endif
@endif
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('customers.save')" />
<x-button
:href="$customerId === null ? route('customers.index') : route('customers.show', $customerId)"
wire:navigate
subtle
color="black"
:text="__('customers.cancel')"
/>
</div>
</form>
</div>