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,23 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Contracts;
use App\Modules\Customers\Domain\Entities\BillingUnit;
interface BillingUnitRepository
{
/**
* Null when the billing unit does not exist or belongs to another customer.
*/
public function findForCustomer(int $customerId, int $billingUnitId): ?BillingUnit;
/**
* Inserts a new billing unit or updates a stored one, and returns the
* stored billing unit (a new one gets its id here).
*/
public function save(BillingUnit $billingUnit): BillingUnit;
public function delete(BillingUnit $billingUnit): void;
}
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Contracts;
use App\Modules\Customers\Domain\Entities\CustomerContact;
interface CustomerContactRepository
{
/**
* Null when the contact does not exist or belongs to another customer.
*/
public function findForCustomer(int $customerId, int $contactId): ?CustomerContact;
/**
* Inserts a new contact or updates a stored one, and returns the stored
* contact (a new one gets its id here).
*/
public function save(CustomerContact $contact): CustomerContact;
public function delete(CustomerContact $contact): void;
}
@@ -6,6 +6,7 @@ namespace App\Modules\Customers\Domain\Contracts;
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
use App\Modules\Customers\Domain\ValueObjects\PersonalIdentityCode;
interface CustomerRepository
{
@@ -18,4 +19,6 @@ interface CustomerRepository
public function save(Customer $customer): Customer;
public function isBusinessIdTaken(BusinessId $businessId, ?int $exceptCustomerId = null): bool;
public function isPersonalIdentityCodeTaken(PersonalIdentityCode $code, ?int $exceptCustomerId = null): bool;
}
@@ -0,0 +1,139 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Entities;
use App\Modules\Customers\Domain\Exceptions\EInvoiceOperatorRequired;
use App\Modules\Customers\Domain\ValueObjects\BillingUnitName;
use App\Modules\Customers\Domain\ValueObjects\EInvoiceAddress;
/**
* A billing unit (laskutusyksikkö) of a customer. Each billing unit gets
* its own invoices, and a customer job picks one.
*/
final class BillingUnit
{
private function __construct(
private ?int $id,
private int $customerId,
private BillingUnitName $name,
private ?string $billingAddress,
private ?EInvoiceAddress $eInvoiceAddress,
private ?string $eInvoiceOperator,
private ?string $reference,
) {
self::ensureOperatorForEInvoice($eInvoiceAddress, $eInvoiceOperator);
}
/**
* A new billing unit that is not stored yet, so it has no id.
*
* @throws EInvoiceOperatorRequired
*/
public static function create(
int $customerId,
BillingUnitName $name,
?string $billingAddress = null,
?EInvoiceAddress $eInvoiceAddress = null,
?string $eInvoiceOperator = null,
?string $reference = null,
): self {
return new self(
null,
$customerId,
$name,
OptionalText::from($billingAddress),
$eInvoiceAddress,
self::normaliseOperator($eInvoiceOperator),
OptionalText::from($reference),
);
}
/**
* Rebuilds a stored billing unit. Used only by repositories.
*/
public static function reconstitute(
int $id,
int $customerId,
BillingUnitName $name,
?string $billingAddress,
?EInvoiceAddress $eInvoiceAddress,
?string $eInvoiceOperator,
?string $reference,
): self {
return new self($id, $customerId, $name, $billingAddress, $eInvoiceAddress, $eInvoiceOperator, $reference);
}
/**
* @throws EInvoiceOperatorRequired
*/
public function changeDetails(
BillingUnitName $name,
?string $billingAddress,
?EInvoiceAddress $eInvoiceAddress,
?string $eInvoiceOperator,
?string $reference,
): void {
$eInvoiceOperator = self::normaliseOperator($eInvoiceOperator);
self::ensureOperatorForEInvoice($eInvoiceAddress, $eInvoiceOperator);
$this->name = $name;
$this->billingAddress = OptionalText::from($billingAddress);
$this->eInvoiceAddress = $eInvoiceAddress;
$this->eInvoiceOperator = $eInvoiceOperator;
$this->reference = OptionalText::from($reference);
}
public function id(): ?int
{
return $this->id;
}
public function customerId(): int
{
return $this->customerId;
}
public function name(): BillingUnitName
{
return $this->name;
}
public function billingAddress(): ?string
{
return $this->billingAddress;
}
public function eInvoiceAddress(): ?EInvoiceAddress
{
return $this->eInvoiceAddress;
}
public function eInvoiceOperator(): ?string
{
return $this->eInvoiceOperator;
}
public function reference(): ?string
{
return $this->reference;
}
/**
* Operator codes such as 003721291126 or NDEAFIHH: no spaces, upper case.
*/
private static function normaliseOperator(?string $operator): ?string
{
$operator = OptionalText::from($operator);
return $operator === null ? null : strtoupper(preg_replace('/\s+/', '', $operator) ?? $operator);
}
private static function ensureOperatorForEInvoice(?EInvoiceAddress $address, ?string $operator): void
{
if ($address !== null && $operator === null) {
throw new EInvoiceOperatorRequired;
}
}
}
@@ -5,14 +5,17 @@ declare(strict_types=1);
namespace App\Modules\Customers\Domain\Entities;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\CompanyCannotHavePersonalIdentityCode;
use App\Modules\Customers\Domain\Exceptions\ConsumerCannotHaveBusinessId;
use App\Modules\Customers\Domain\ValueObjects\BusinessId;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Customers\Domain\ValueObjects\PersonalIdentityCode;
/**
* A customer of the company: a business or a private consumer.
*
* Only the name is needed at job intake; other details are added later.
* A consumer who is invoiced needs a personal identity code.
*/
final class Customer
{
@@ -21,33 +24,61 @@ final class Customer
private CustomerName $name,
private CustomerType $type,
private ?BusinessId $businessId,
private ?PersonalIdentityCode $personalIdentityCode,
) {
self::ensureBusinessIdFitsType($type, $businessId);
self::ensureIdentifiersFitType($type, $businessId, $personalIdentityCode);
}
/**
* A new customer that is not stored yet, so it has no id.
*/
public static function register(CustomerName $name, CustomerType $type, ?BusinessId $businessId = null): self
{
return new self(null, $name, $type, $businessId);
public static function register(
CustomerName $name,
CustomerType $type,
?BusinessId $businessId = null,
?PersonalIdentityCode $personalIdentityCode = null,
): self {
return new self(null, $name, $type, $businessId, $personalIdentityCode);
}
/**
* Rebuilds a stored customer. Used only by repositories.
*/
public static function reconstitute(int $id, CustomerName $name, CustomerType $type, ?BusinessId $businessId): self
{
return new self($id, $name, $type, $businessId);
public static function reconstitute(
int $id,
CustomerName $name,
CustomerType $type,
?BusinessId $businessId,
?PersonalIdentityCode $personalIdentityCode = null,
): self {
return new self($id, $name, $type, $businessId, $personalIdentityCode);
}
public function changeDetails(CustomerName $name, CustomerType $type, ?BusinessId $businessId): void
{
self::ensureBusinessIdFitsType($type, $businessId);
/**
* Changes the details. A null personal identity code keeps the stored
* one, except that a company never keeps one.
*/
public function changeDetails(
CustomerName $name,
CustomerType $type,
?BusinessId $businessId,
?PersonalIdentityCode $personalIdentityCode = null,
): void {
if ($personalIdentityCode === null && $type === CustomerType::Consumer) {
$personalIdentityCode = $this->personalIdentityCode;
}
self::ensureIdentifiersFitType($type, $businessId, $personalIdentityCode);
$this->name = $name;
$this->type = $type;
$this->businessId = $businessId;
$this->personalIdentityCode = $personalIdentityCode;
}
public function removePersonalIdentityCode(): void
{
$this->personalIdentityCode = null;
}
public function id(): ?int
@@ -70,10 +101,22 @@ final class Customer
return $this->businessId;
}
private static function ensureBusinessIdFitsType(CustomerType $type, ?BusinessId $businessId): void
public function personalIdentityCode(): ?PersonalIdentityCode
{
return $this->personalIdentityCode;
}
private static function ensureIdentifiersFitType(
CustomerType $type,
?BusinessId $businessId,
?PersonalIdentityCode $personalIdentityCode,
): void {
if ($type === CustomerType::Consumer && $businessId !== null) {
throw new ConsumerCannotHaveBusinessId;
}
if ($type === CustomerType::Company && $personalIdentityCode !== null) {
throw new CompanyCannotHavePersonalIdentityCode;
}
}
}
@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Entities;
use App\Modules\Customers\Domain\ValueObjects\ContactName;
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
/**
* A contact person of a customer. A customer job later picks one of the
* customer's contacts as its customer contact.
*/
final class CustomerContact
{
private function __construct(
private ?int $id,
private int $customerId,
private ContactName $name,
private ?string $title,
private ?string $phone,
private ?EmailAddress $email,
private ?string $note,
) {}
/**
* A new contact that is not stored yet, so it has no id.
*/
public static function create(
int $customerId,
ContactName $name,
?string $title = null,
?string $phone = null,
?EmailAddress $email = null,
?string $note = null,
): self {
return new self(null, $customerId, $name, OptionalText::from($title), OptionalText::from($phone), $email, OptionalText::from($note));
}
/**
* Rebuilds a stored contact. Used only by repositories.
*/
public static function reconstitute(
int $id,
int $customerId,
ContactName $name,
?string $title,
?string $phone,
?EmailAddress $email,
?string $note,
): self {
return new self($id, $customerId, $name, $title, $phone, $email, $note);
}
public function changeDetails(ContactName $name, ?string $title, ?string $phone, ?EmailAddress $email, ?string $note): void
{
$this->name = $name;
$this->title = OptionalText::from($title);
$this->phone = OptionalText::from($phone);
$this->email = $email;
$this->note = OptionalText::from($note);
}
public function id(): ?int
{
return $this->id;
}
public function customerId(): int
{
return $this->customerId;
}
public function name(): ContactName
{
return $this->name;
}
public function title(): ?string
{
return $this->title;
}
public function phone(): ?string
{
return $this->phone;
}
public function email(): ?EmailAddress
{
return $this->email;
}
public function note(): ?string
{
return $this->note;
}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Entities;
/**
* Normalises optional free-text fields: trimmed, and empty means none.
*
* @internal
*/
final class OptionalText
{
public static function from(?string $value): ?string
{
$value = $value === null ? '' : trim($value);
return $value === '' ? null : $value;
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* The row does not exist or belongs to another customer.
*/
final class BillingUnitNotFound extends DomainException
{
public function __construct(public readonly int $customerId, public readonly int $id)
{
parent::__construct("Billing unit {$id} of customer {$customerId} was not found.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class CompanyCannotHavePersonalIdentityCode extends DomainException
{
public function __construct()
{
parent::__construct('A company customer cannot have a personal identity code.');
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* The row does not exist or belongs to another customer.
*/
final class ContactNotFound extends DomainException
{
public function __construct(public readonly int $customerId, public readonly int $id)
{
parent::__construct("Contact {$id} of customer {$customerId} was not found.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class EInvoiceOperatorRequired extends DomainException
{
public function __construct()
{
parent::__construct('An e-invoice address needs an e-invoice operator.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidBillingUnitName extends DomainException
{
public function __construct()
{
parent::__construct('The billing unit name must be 1–255 characters.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidContactName extends DomainException
{
public function __construct()
{
parent::__construct('The contact name must be 1–255 characters.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidEInvoiceAddress extends DomainException
{
public function __construct()
{
parent::__construct('The e-invoice address must be 12–35 letters or digits.');
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* The message never contains the code: it is sensitive personal data.
*/
final class InvalidPersonalIdentityCode extends DomainException
{
public function __construct()
{
parent::__construct('The personal identity code is not a valid Finnish personal identity code.');
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* The message never contains the code: it is sensitive personal data.
*/
final class PersonalIdentityCodeAlreadyInUse extends DomainException
{
public function __construct()
{
parent::__construct('The personal identity code already belongs to another customer.');
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\ValueObjects;
use App\Modules\Customers\Domain\Exceptions\InvalidBillingUnitName;
/**
* Name of a billing unit, e.g. "Mainos Oy, markkinointi".
*/
final readonly class BillingUnitName
{
public const int MAX_LENGTH = 255;
public string $value;
public function __construct(string $value)
{
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
throw new InvalidBillingUnitName;
}
$this->value = $value;
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\ValueObjects;
use App\Modules\Customers\Domain\Exceptions\InvalidContactName;
/**
* Name of a customer's contact person.
*/
final readonly class ContactName
{
public const int MAX_LENGTH = 255;
public string $value;
public function __construct(string $value)
{
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
throw new InvalidContactName;
}
$this->value = $value;
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\ValueObjects;
use App\Modules\Customers\Domain\Exceptions\InvalidEInvoiceAddress;
/**
* E-invoice address (verkkolaskuosoite), e.g. an OVT code 003712345678 or
* an IBAN. Spaces are removed and letters upper-cased.
*/
final readonly class EInvoiceAddress
{
public string $value;
public function __construct(string $value)
{
$value = strtoupper(preg_replace('/\s+/', '', $value) ?? $value);
if (preg_match('/^[A-Z0-9]{12,35}$/', $value) !== 1) {
throw new InvalidEInvoiceAddress;
}
$this->value = $value;
}
}
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace App\Modules\Customers\Domain\ValueObjects;
use App\Modules\Customers\Domain\Exceptions\InvalidPersonalIdentityCode;
/**
* Finnish personal identity code (henkilötunnus), e.g. 131052-308T.
*
* Sensitive personal data: never put the value in exception messages or
* logs, and show it only to users allowed to see it (masked() otherwise).
*/
final readonly class PersonalIdentityCode
{
private const string CHECK_CHARACTERS = '0123456789ABCDEFHJKLMNPRSTUVWXY';
/**
* Century sign => the century the two-digit year belongs to.
*/
private const array CENTURIES = [
'+' => 1800,
'-' => 1900, 'Y' => 1900, 'X' => 1900, 'W' => 1900, 'V' => 1900, 'U' => 1900,
'A' => 2000, 'B' => 2000, 'C' => 2000, 'D' => 2000, 'E' => 2000, 'F' => 2000,
];
public string $value;
public function __construct(string $value)
{
$value = strtoupper(preg_replace('/\s+/', '', $value) ?? $value);
if (preg_match('/^(\d{2})(\d{2})(\d{2})([-+A-FU-Y])(\d{3})([0-9A-Y])$/', $value, $parts) !== 1) {
throw new InvalidPersonalIdentityCode;
}
[, $day, $month, $year, $centurySign, $individualNumber, $checkCharacter] = $parts;
if (! checkdate((int) $month, (int) $day, self::CENTURIES[$centurySign] + (int) $year)) {
throw new InvalidPersonalIdentityCode;
}
$expected = self::CHECK_CHARACTERS[(int) ($day.$month.$year.$individualNumber) % 31];
if ($checkCharacter !== $expected) {
throw new InvalidPersonalIdentityCode;
}
$this->value = $value;
}
/**
* The code with the date of birth hidden, e.g. ••••••-308T.
*/
public function masked(): string
{
return '••••••'.substr($this->value, 6);
}
public function equals(self $other): bool
{
return $this->value === $other->value;
}
}