Files
toiminnanohjaus-django/apps/customers/domain/entities.py
T
petriandClaude Opus 5.5 a81c073d6a Vaihe 1/2: asiakkaat, yhteyshenkilöt ja laskutusyksiköt
- customers-moduuli Clean Architecture -kerroksin: asiakas (yritys/kuluttaja),
  Y-tunnuksen tarkiste ja yksilöllisyys, yhteyshenkilöt, laskutusyksiköt
  (verkkolaskuosoite vaatii välittäjätunnuksen)
- henkilötunnus salattuna (Fernet) ja HMAC-tiiviste päällekkäisyyksiin;
  pääkäyttäjä ja johto näkevät ja poistavat, muut näkevät peitettynä
- sisäinen asiakas data-migraatiolla, ei poistettavissa
- yleinen poistonesto käänteisten relaatioiden perusteella (tulevat
  PROTECT-viiteavaimet estävät poiston automaattisesti)
- asiakkaan sivu: lisäys ja muokkaus dialogissa HTMX:llä, fokuksen hallinta
  ja ruudunlukijailmoitukset; HTMX näyttää nyt 422-validointivirheet
- testit: domain, application, integraatio ja selaintestit (axe);
  yhteiset fixturet apps/conftest.py:hin, FakeUnitOfWork shared-moduuliin
- import-linter: moduulien riippumattomuussäännöt
- dokumentit päivitetty

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 13:47:09 +02:00

124 lines
4.0 KiB
Python

from dataclasses import dataclass, replace
from typing import Self
from apps.customers.domain.enums import CustomerType
from apps.customers.domain.exceptions import (
ConsumerCannotHaveBusinessId,
EInvoiceOperatorRequired,
InternalCustomerMustBeCompany,
InvalidBillingUnitName,
InvalidContactName,
)
from apps.customers.domain.value_objects import BusinessId, CustomerName, EInvoiceAddress
from apps.shared.domain.value_objects import EmailAddress
@dataclass(frozen=True, slots=True)
class Customer:
"""Customer (specification 4).
Rules: a consumer has no business ID and a company has no personal identity code;
the internal customer is always a company. The personal identity code itself never
lives in the entity; ``has_personal_id`` tells whether one is stored.
"""
id: int | None
name: CustomerName
type: CustomerType
business_id: BusinessId | None
is_internal: bool = False
has_personal_id: bool = False
@classmethod
def register(
cls, *, name: CustomerName, type: CustomerType, business_id: BusinessId | None = None
) -> Self:
cls._check(type=type, business_id=business_id, is_internal=False)
return cls(id=None, name=name, type=type, business_id=business_id)
@classmethod
def reconstitute(
cls,
*,
id: int,
name: CustomerName,
type: CustomerType,
business_id: BusinessId | None,
is_internal: bool,
has_personal_id: bool,
) -> Self:
return cls(id, name, type, business_id, is_internal, has_personal_id)
def change_details(
self, *, name: CustomerName, type: CustomerType, business_id: BusinessId | None
) -> Self:
"""A company loses its personal identity code (the repository removes it)."""
self._check(type=type, business_id=business_id, is_internal=self.is_internal)
has_pic = self.has_personal_id and type is CustomerType.CONSUMER
return replace(self, name=name, type=type, business_id=business_id, has_personal_id=has_pic)
def with_id(self, id: int) -> Self:
return replace(self, id=id)
def with_personal_id(self, present: bool) -> Self:
return replace(self, has_personal_id=present)
@property
def can_have_personal_id(self) -> bool:
return self.type is CustomerType.CONSUMER
@staticmethod
def _check(*, type: CustomerType, business_id: BusinessId | None, is_internal: bool) -> None:
if is_internal and type is not CustomerType.COMPANY:
raise InternalCustomerMustBeCompany()
if type is CustomerType.CONSUMER and business_id is not None:
raise ConsumerCannotHaveBusinessId()
def _required_text(value: str, max_length: int, error: type[Exception]) -> str:
normalized = " ".join(value.split())
if not normalized or len(normalized) > max_length:
raise error()
return normalized
@dataclass(frozen=True, slots=True)
class Contact:
"""A customer's contact person."""
id: int | None
customer_id: int
name: str
title: str = ""
phone: str = ""
email: EmailAddress | None = None
note: str = ""
def __post_init__(self) -> None:
object.__setattr__(self, "name", _required_text(self.name, 150, InvalidContactName))
def with_id(self, id: int) -> Self:
return replace(self, id=id)
@dataclass(frozen=True, slots=True)
class BillingUnit:
"""Invoicing target of a customer; each unit gets its own invoices."""
id: int | None
customer_id: int
name: str
billing_address: str = ""
e_invoice_address: EInvoiceAddress | None = None
e_invoice_operator: str = ""
reference: str = ""
def __post_init__(self) -> None:
object.__setattr__(self, "name", _required_text(self.name, 200, InvalidBillingUnitName))
object.__setattr__(self, "e_invoice_operator", self.e_invoice_operator.strip())
if self.e_invoice_address is not None and not self.e_invoice_operator:
raise EInvoiceOperatorRequired()
def with_id(self, id: int) -> Self:
return replace(self, id=id)