from django.db import models from apps.customers.domain.enums import CustomerType from apps.shared.infrastructure.models import TimestampedModel class CustomerModel(TimestampedModel): name = models.CharField(max_length=200) type = models.CharField(max_length=20, default=CustomerType.COMPANY.value) business_id = models.CharField(max_length=9, null=True, blank=True, unique=True) # Fernet ciphertext of the henkilötunnus; never stored in plain text. # NULL (not "") means no code, matching the nullable unique hash below. personal_identity_code = models.TextField(null=True, blank=True) # noqa: DJ001 # HMAC-SHA256 fingerprint for duplicate checks (the code itself is not searchable). personal_identity_code_hash = models.CharField( max_length=64, null=True, blank=True, unique=True ) is_internal = models.BooleanField(default=False) class Meta: db_table = "customers" ordering = ["name"] def __str__(self) -> str: return self.name class ContactModel(TimestampedModel): customer = models.ForeignKey(CustomerModel, on_delete=models.PROTECT, related_name="contacts") name = models.CharField(max_length=150) title = models.CharField(max_length=150, blank=True) phone = models.CharField(max_length=50, blank=True) email = models.EmailField(max_length=254, blank=True) note = models.TextField(blank=True) class Meta: db_table = "customer_contacts" ordering = ["name", "id"] def __str__(self) -> str: return self.name class BillingUnitModel(TimestampedModel): customer = models.ForeignKey( CustomerModel, on_delete=models.PROTECT, related_name="billing_units" ) name = models.CharField(max_length=200) billing_address = models.TextField(blank=True) e_invoice_address = models.CharField(max_length=35, blank=True) e_invoice_operator = models.CharField(max_length=35, blank=True) reference = models.CharField(max_length=100, blank=True) class Meta: db_table = "billing_units" ordering = ["name", "id"] def __str__(self) -> str: return self.name