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]>
This commit is contained in:
2026-11-23 18:11:06 +02:00
co-authored by Claude Opus 5.5
parent 8dfc02c4e5
commit e0b8f4970f
93 changed files with 3777 additions and 58 deletions
@@ -0,0 +1,357 @@
<?php
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Application\DTOs\BillingUnitSummary;
use App\Modules\Customers\Application\DTOs\ContactSummary;
use App\Modules\Customers\Application\DTOs\CustomerSummary;
use App\Modules\Customers\Application\UseCases\RemoveBillingUnit;
use App\Modules\Customers\Application\UseCases\RemoveContact;
use App\Modules\Customers\Domain\Exceptions\BillingUnitNotFound;
use App\Modules\Customers\Domain\Exceptions\ContactNotFound;
use App\Modules\Customers\Domain\Enums\CustomerType;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* A customer's page: details, contacts and billing units.
*/
new class extends Component
{
public const string REMOVE_DIALOG = 'remove-dialog';
#[Locked]
public int $customerId;
/**
* The row the remove dialog asks about: 'contact' or 'billing_unit'.
*/
#[Locked]
public ?string $removalType = null;
#[Locked]
public ?int $removalId = null;
#[Locked]
public string $removalName = '';
public string $statusMessage = '';
protected CustomerQueries $customerQueries;
public function boot(CustomerQueries $customerQueries): void
{
$this->customerQueries = $customerQueries;
}
public function mount(int $customer): void
{
$this->authorize('customers.use');
abort_if($this->customerQueries->findSummary($customer) === null, 404);
$this->customerId = $customer;
}
#[Computed]
public function customer(): CustomerSummary
{
return $this->customerQueries->findSummary($this->customerId) ?? abort(404);
}
/**
* The personal identity code as this user may see it: in full for admins
* and management, masked for others. Computed, so the full code is never
* kept in the component state sent to the browser.
*/
#[Computed]
public function personalIdentityCode(): ?string
{
if (Gate::allows('customers.view-personal-identity-code')) {
return $this->customerQueries->personalIdentityCodeOf($this->customerId);
}
return $this->customer->personalIdentityCodeMasked;
}
/**
* @return list<ContactSummary>
*/
#[Computed]
public function contacts(): array
{
return $this->customerQueries->contactsOf($this->customerId);
}
/**
* @return list<BillingUnitSummary>
*/
#[Computed]
public function billingUnits(): array
{
return $this->customerQueries->billingUnitsOf($this->customerId);
}
/**
* Opens the remove dialog for a contact or a billing unit of this customer.
*/
public function confirmRemoval(string $type, int $id): void
{
$this->authorize('customers.use');
$name = match ($type) {
'contact' => $this->customerQueries->findContact($this->customerId, $id)?->name,
'billing_unit' => $this->customerQueries->findBillingUnit($this->customerId, $id)?->name,
default => null,
};
if ($name === null) {
$this->statusMessage = __('customers.removal.already_removed');
return;
}
$this->removalType = $type;
$this->removalId = $id;
$this->removalName = $name;
$this->statusMessage = '';
$this->js("document.getElementById('".self::REMOVE_DIALOG."').showModal()");
}
public function remove(RemoveContact $removeContact, RemoveBillingUnit $removeBillingUnit): void
{
$this->authorize('customers.use');
if ($this->removalType === null || $this->removalId === null) {
return;
}
try {
match ($this->removalType) {
'contact' => $removeContact->execute($this->customerId, $this->removalId),
'billing_unit' => $removeBillingUnit->execute($this->customerId, $this->removalId),
};
$this->statusMessage = __('customers.removal.removed_'.$this->removalType, ['name' => $this->removalName]);
} catch (ContactNotFound|BillingUnitNotFound) {
$this->statusMessage = __('customers.removal.already_removed');
}
// The removed row's button is gone, so focus moves to its section heading.
$heading = $this->removalType === 'contact' ? 'contacts-heading' : 'billing-units-heading';
$this->js("document.getElementById('".self::REMOVE_DIALOG."').close(); document.getElementById('{$heading}').focus()");
$this->reset('removalType', 'removalId', 'removalName');
}
public function render(): mixed
{
return $this->view()->title($this->customer->name);
}
};
?>
<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-center justify-between gap-4">
<h1 class="text-2xl font-semibold break-words">{{ $this->customer->name }}</h1>
<x-button
:href="route('customers.edit', $customerId)"
wire:navigate
subtle
color="black"
icon="pencil"
:text="__('customers.edit_details')"
/>
</div>
@if (session('status'))
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
@endif
<dl class="grid gap-x-6 gap-y-2 text-sm sm:grid-cols-[max-content_1fr]">
<dt class="font-medium">{{ __('customers.fields.type') }}</dt>
<dd>{{ __('customers.types.'.$this->customer->type->value) }}</dd>
@if ($this->customer->businessId !== null)
<dt class="font-medium">{{ __('customers.fields.business_id') }}</dt>
<dd>{{ $this->customer->businessId }}</dd>
@endif
@if ($this->customer->type === CustomerType::Consumer)
<dt class="font-medium">{{ __('customers.fields.personal_identity_code') }}</dt>
<dd>
@if ($this->personalIdentityCode !== null)
{{ $this->personalIdentityCode }}
@else
<span class="inline-flex items-center gap-1">
<x-icon name="exclamation-triangle" class="size-4 shrink-0" aria-hidden="true" />
{{ __('customers.fields.personal_identity_code_missing') }}
</span>
@endif
</dd>
@endif
</dl>
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
</div>
<section aria-labelledby="contacts-heading" class="space-y-4">
<div class="flex flex-wrap items-center justify-between gap-4">
<h2 id="contacts-heading" tabindex="-1" class="text-xl font-semibold focus:outline-none">
{{ __('customers.contacts.title') }}
</h2>
<x-button
:href="route('customers.contacts.create', $customerId)"
wire:navigate
icon="plus"
:text="__('customers.contacts.new')"
/>
</div>
@if ($this->contacts === [])
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('customers.contacts.empty') }}</p>
@else
<ul class="divide-y divide-zinc-200 rounded-lg border border-zinc-200 dark:divide-zinc-700 dark:border-zinc-700">
@foreach ($this->contacts as $contact)
<li wire:key="contact-{{ $contact->id }}" class="flex flex-wrap items-start justify-between gap-4 p-4">
<div class="min-w-0 space-y-1 text-sm">
<h3 class="text-base font-medium break-words">{{ $contact->name }}</h3>
@if ($contact->title !== null)
<p>{{ $contact->title }}</p>
@endif
@if ($contact->phone !== null)
<p>
<span class="sr-only">{{ __('customers.contacts.fields.phone') }}:</span>
<x-link :href="'tel:'.preg_replace('/[^\d+]/', '', $contact->phone)">{{ $contact->phone }}</x-link>
</p>
@endif
@if ($contact->email !== null)
<p class="break-all">
<span class="sr-only">{{ __('customers.contacts.fields.email') }}:</span>
<x-link :href="'mailto:'.$contact->email">{{ $contact->email }}</x-link>
</p>
@endif
@if ($contact->note !== null)
<p class="whitespace-pre-line text-zinc-600 dark:text-zinc-300">{{ $contact->note }}</p>
@endif
</div>
<div class="flex flex-wrap gap-2">
<x-button
:href="route('customers.contacts.edit', [$customerId, $contact->id])"
wire:navigate
subtle
color="black"
sm
icon="pencil"
:text="__('customers.edit')"
:aria-label="__('customers.contacts.edit_named', ['name' => $contact->name])"
/>
<x-button
wire:click="confirmRemoval('contact', {{ $contact->id }})"
subtle
color="black"
sm
icon="trash"
:text="__('customers.remove')"
:aria-label="__('customers.contacts.remove_named', ['name' => $contact->name])"
/>
</div>
</li>
@endforeach
</ul>
@endif
</section>
<section aria-labelledby="billing-units-heading" class="space-y-4">
<div class="flex flex-wrap items-center justify-between gap-4">
<h2 id="billing-units-heading" tabindex="-1" class="text-xl font-semibold focus:outline-none">
{{ __('customers.billing_units.title') }}
</h2>
<x-button
:href="route('customers.billing-units.create', $customerId)"
wire:navigate
icon="plus"
:text="__('customers.billing_units.new')"
/>
</div>
@if ($this->billingUnits === [])
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('customers.billing_units.empty') }}</p>
@else
<ul class="divide-y divide-zinc-200 rounded-lg border border-zinc-200 dark:divide-zinc-700 dark:border-zinc-700">
@foreach ($this->billingUnits as $billingUnit)
<li wire:key="billing-unit-{{ $billingUnit->id }}" class="flex flex-wrap items-start justify-between gap-4 p-4">
<div class="min-w-0 space-y-1 text-sm">
<h3 class="text-base font-medium break-words">{{ $billingUnit->name }}</h3>
<dl class="grid gap-x-4 gap-y-1 sm:grid-cols-[max-content_1fr]">
@if ($billingUnit->billingAddress !== null)
<dt class="font-medium">{{ __('customers.billing_units.fields.billing_address') }}</dt>
<dd class="whitespace-pre-line">{{ $billingUnit->billingAddress }}</dd>
@endif
@if ($billingUnit->eInvoiceAddress !== null)
<dt class="font-medium">{{ __('customers.billing_units.fields.e_invoice_address') }}</dt>
<dd class="break-all">{{ $billingUnit->eInvoiceAddress }}</dd>
<dt class="font-medium">{{ __('customers.billing_units.fields.e_invoice_operator') }}</dt>
<dd class="break-all">{{ $billingUnit->eInvoiceOperator }}</dd>
@endif
@if ($billingUnit->reference !== null)
<dt class="font-medium">{{ __('customers.billing_units.fields.reference') }}</dt>
<dd class="break-words">{{ $billingUnit->reference }}</dd>
@endif
</dl>
</div>
<div class="flex flex-wrap gap-2">
<x-button
:href="route('customers.billing-units.edit', [$customerId, $billingUnit->id])"
wire:navigate
subtle
color="black"
sm
icon="pencil"
:text="__('customers.edit')"
:aria-label="__('customers.billing_units.edit_named', ['name' => $billingUnit->name])"
/>
<x-button
wire:click="confirmRemoval('billing_unit', {{ $billingUnit->id }})"
subtle
color="black"
sm
icon="trash"
:text="__('customers.remove')"
:aria-label="__('customers.billing_units.remove_named', ['name' => $billingUnit->name])"
/>
</div>
</li>
@endforeach
</ul>
@endif
</section>
<x-confirm-dialog id="remove-dialog" :title="__('customers.removal.title')">
<p>
@if ($removalType !== null)
{{ __('customers.removal.question_'.$removalType, ['name' => $removalName]) }}
@endif
</p>
<x-slot:actions>
<x-button wire:click="remove" color="red" icon="trash" :text="__('customers.remove')" />
</x-slot:actions>
</x-confirm-dialog>
</div>