Files
Toiminnanohjaus/app/Modules/Customers/Domain/Entities/Customer.php
T
petriandClaude Opus 5.5 70e107f6e2 Add customer jobs with numbering, members, tags, links and history
Phase 1, item 4. New Jobs module:
- Customer jobs with the spec's fields except money (phase 2): yearly
  running numbers (2026-0001) from a locked counter row, service units,
  schedule, kind, status, priority, payment basis, estimated hours,
  PO reference, instructions, members (responsible, contact person,
  customer contact, biller, billing unit, other members), tags shared
  across jobs, and links.
- Every change is recorded field by field and shown on the job page with
  names and Finnish dates.
- References to other modules are checked through their query
  contracts: contact and billing unit must be the customer's own;
  service units and users must be in use unless already on the job.
- Job list with search, filters and "my jobs" on by default; job form
  with an accessible customer search and quick add; job page.
- Access: subcontractors see only jobs they are members of, read only;
  billing statuses for management and the job's biller; archiving for
  management.

Related changes:
- Customers: one internal customer (created by a migration) for internal
  work; contacts and billing units used by a job cannot be removed.
- Users and Catalog: batch name lookups and active user options with
  roles for other modules.
- <x-checkbox-group> component; spec and CLAUDE.md updated.

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

144 lines
4.2 KiB
PHP

<?php
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\Exceptions\InternalCustomerMustBeCompany;
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
{
private function __construct(
private ?int $id,
private CustomerName $name,
private CustomerType $type,
private ?BusinessId $businessId,
private ?PersonalIdentityCode $personalIdentityCode,
private bool $internal = false,
) {
self::ensureIdentifiersFitType($type, $businessId, $personalIdentityCode);
self::ensureInternalIsCompany($internal, $type);
}
/**
* A new customer that is not stored yet, so it has no id.
*/
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,
?PersonalIdentityCode $personalIdentityCode = null,
bool $internal = false,
): self {
return new self($id, $name, $type, $businessId, $personalIdentityCode, $internal);
}
/**
* 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);
self::ensureInternalIsCompany($this->internal, $type);
$this->name = $name;
$this->type = $type;
$this->businessId = $businessId;
$this->personalIdentityCode = $personalIdentityCode;
}
public function removePersonalIdentityCode(): void
{
$this->personalIdentityCode = null;
}
public function id(): ?int
{
return $this->id;
}
public function name(): CustomerName
{
return $this->name;
}
public function type(): CustomerType
{
return $this->type;
}
public function businessId(): ?BusinessId
{
return $this->businessId;
}
public function personalIdentityCode(): ?PersonalIdentityCode
{
return $this->personalIdentityCode;
}
/**
* The company itself, whose jobs hold internal work (administration,
* sales, training). Created by a migration, never through the UI.
*/
public function isInternal(): bool
{
return $this->internal;
}
private static function ensureInternalIsCompany(bool $internal, CustomerType $type): void
{
if ($internal && $type !== CustomerType::Company) {
throw new InternalCustomerMustBeCompany;
}
}
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;
}
}
}