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,93 @@
<?php
namespace App\Modules\Customers\Presentation\Livewire\Forms;
use App\Modules\Customers\Application\DTOs\BillingUnitData;
use App\Modules\Customers\Application\DTOs\BillingUnitSummary;
use App\Modules\Customers\Domain\Exceptions\EInvoiceOperatorRequired;
use App\Modules\Customers\Domain\Exceptions\InvalidBillingUnitName;
use App\Modules\Customers\Domain\Exceptions\InvalidEInvoiceAddress;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Livewire\Form;
/**
* Billing unit form shared by the add and edit pages.
*/
class BillingUnitForm extends Form
{
public string $name = '';
public string $billing_address = '';
public string $e_invoice_address = '';
public string $e_invoice_operator = '';
public string $reference = '';
public function fillFromSummary(BillingUnitSummary $billingUnit): void
{
$this->name = $billingUnit->name;
$this->billing_address = $billingUnit->billingAddress ?? '';
$this->e_invoice_address = $billingUnit->eInvoiceAddress ?? '';
$this->e_invoice_operator = $billingUnit->eInvoiceOperator ?? '';
$this->reference = $billingUnit->reference ?? '';
}
public function toData(): BillingUnitData
{
$this->validate();
return BillingUnitData::fromArray([
'name' => $this->name,
'billing_address' => $this->billing_address,
'e_invoice_address' => $this->e_invoice_address,
'e_invoice_operator' => $this->e_invoice_operator,
'reference' => $this->reference,
]);
}
/**
* Shows a broken business rule as an error on the field it concerns.
*/
public function showDomainError(DomainException $exception): void
{
[$field, $key] = match ($exception::class) {
InvalidBillingUnitName::class => ['name', 'customers.errors.invalid_billing_unit_name'],
InvalidEInvoiceAddress::class => ['e_invoice_address', 'customers.errors.invalid_e_invoice_address'],
EInvoiceOperatorRequired::class => ['e_invoice_operator', 'customers.errors.e_invoice_operator_required'],
default => throw $exception,
};
$this->addError($field, __($key));
}
/**
* @return array<string, array<int, mixed>>
*/
protected function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'billing_address' => ['nullable', 'string', 'max:1000'],
'e_invoice_address' => ['nullable', 'string', 'max:50'],
'e_invoice_operator' => ['nullable', 'string', 'max:35'],
'reference' => ['nullable', 'string', 'max:255'],
];
}
/**
* @return array<string, string>
*/
protected function messages(): array
{
return [
'name.required' => __('customers.errors.billing_unit_name_required'),
'name.max' => __('customers.errors.invalid_billing_unit_name'),
'billing_address.max' => __('customers.errors.too_long', ['max' => 1000]),
'e_invoice_address.max' => __('customers.errors.invalid_e_invoice_address'),
'e_invoice_operator.max' => __('customers.errors.too_long', ['max' => 35]),
'reference.max' => __('customers.errors.too_long', ['max' => 255]),
];
}
}
@@ -0,0 +1,92 @@
<?php
namespace App\Modules\Customers\Presentation\Livewire\Forms;
use App\Modules\Customers\Application\DTOs\ContactData;
use App\Modules\Customers\Application\DTOs\ContactSummary;
use App\Modules\Customers\Domain\Exceptions\InvalidContactName;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use Livewire\Form;
/**
* Contact person form shared by the add and edit pages.
*/
class ContactForm extends Form
{
public string $name = '';
public string $title = '';
public string $phone = '';
public string $email = '';
public string $note = '';
public function fillFromSummary(ContactSummary $contact): void
{
$this->name = $contact->name;
$this->title = $contact->title ?? '';
$this->phone = $contact->phone ?? '';
$this->email = $contact->email ?? '';
$this->note = $contact->note ?? '';
}
public function toData(): ContactData
{
$this->validate();
return ContactData::fromArray([
'name' => $this->name,
'title' => $this->title,
'phone' => $this->phone,
'email' => $this->email,
'note' => $this->note,
]);
}
/**
* Shows a broken business rule as an error on the field it concerns.
*/
public function showDomainError(DomainException $exception): void
{
[$field, $key] = match ($exception::class) {
InvalidContactName::class => ['name', 'customers.errors.invalid_contact_name'],
InvalidEmailAddress::class => ['email', 'customers.errors.invalid_email'],
default => throw $exception,
};
$this->addError($field, __($key));
}
/**
* @return array<string, array<int, mixed>>
*/
protected function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'title' => ['nullable', 'string', 'max:255'],
'phone' => ['nullable', 'string', 'max:50'],
'email' => ['nullable', 'string', 'email', 'max:255'],
'note' => ['nullable', 'string', 'max:2000'],
];
}
/**
* @return array<string, string>
*/
protected function messages(): array
{
return [
'name.required' => __('customers.errors.contact_name_required'),
'name.max' => __('customers.errors.invalid_contact_name'),
'title.max' => __('customers.errors.too_long', ['max' => 255]),
'phone.max' => __('customers.errors.too_long', ['max' => 50]),
'email.email' => __('customers.errors.invalid_email'),
'email.max' => __('customers.errors.invalid_email'),
'note.max' => __('customers.errors.too_long', ['max' => 2000]),
];
}
}
@@ -6,9 +6,12 @@ use App\Modules\Customers\Application\DTOs\CustomerDetailsData;
use App\Modules\Customers\Application\DTOs\CustomerSummary;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\BusinessIdAlreadyInUse;
use App\Modules\Customers\Domain\Exceptions\CompanyCannotHavePersonalIdentityCode;
use App\Modules\Customers\Domain\Exceptions\ConsumerCannotHaveBusinessId;
use App\Modules\Customers\Domain\Exceptions\InvalidBusinessId;
use App\Modules\Customers\Domain\Exceptions\InvalidCustomerName;
use App\Modules\Customers\Domain\Exceptions\InvalidPersonalIdentityCode;
use App\Modules\Customers\Domain\Exceptions\PersonalIdentityCodeAlreadyInUse;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use Illuminate\Validation\Rule;
use Livewire\Form;
@@ -28,6 +31,14 @@ class CustomerForm extends Form
public string $business_id = '';
/**
* A new personal identity code. Never filled from the stored code: empty
* keeps the stored one.
*/
public string $personal_identity_code = '';
public bool $remove_personal_identity_code = false;
public function fillFromSummary(CustomerSummary $customer): void
{
$this->name = $customer->name;
@@ -43,6 +54,8 @@ class CustomerForm extends Form
'name' => $this->name,
'type' => $this->type,
'business_id' => $this->type === CustomerType::Consumer->value ? null : $this->business_id,
'personal_identity_code' => $this->type === CustomerType::Consumer->value ? $this->personal_identity_code : null,
'remove_personal_identity_code' => $this->remove_personal_identity_code,
]);
}
@@ -56,6 +69,9 @@ class CustomerForm extends Form
InvalidBusinessId::class => ['business_id', 'customers.errors.invalid_business_id'],
ConsumerCannotHaveBusinessId::class => ['business_id', 'customers.errors.consumer_business_id'],
BusinessIdAlreadyInUse::class => ['business_id', 'customers.errors.business_id_taken'],
InvalidPersonalIdentityCode::class => ['personal_identity_code', 'customers.errors.invalid_personal_identity_code'],
CompanyCannotHavePersonalIdentityCode::class => ['personal_identity_code', 'customers.errors.company_personal_identity_code'],
PersonalIdentityCodeAlreadyInUse::class => ['personal_identity_code', 'customers.errors.personal_identity_code_taken'],
default => throw $exception,
};
@@ -71,6 +87,8 @@ class CustomerForm extends Form
'name' => ['required', 'string', 'max:255'],
'type' => ['required', Rule::enum(CustomerType::class)],
'business_id' => ['nullable', 'string', 'max:20'],
'personal_identity_code' => ['nullable', 'string', 'max:20'],
'remove_personal_identity_code' => ['boolean'],
];
}
@@ -85,6 +103,7 @@ class CustomerForm extends Form
'type.required' => __('customers.errors.type_required'),
'type.enum' => __('customers.errors.type_required'),
'business_id.max' => __('customers.errors.invalid_business_id'),
'personal_identity_code.max' => __('customers.errors.invalid_personal_identity_code'),
];
}
}