Files
customerTicket/apps/customers/models.py
T
2026-09-10 11:34:19 +03:30

55 lines
2.5 KiB
Python

from django.conf import settings
from django.db import models
from apps.core.models import PersianTextNormalizationMixin, TimeStampedModel
class ContactGroup(PersianTextNormalizationMixin, TimeStampedModel):
"""A unit-owned address-book group; contacts may belong to many groups."""
persian_text_fields = {"name": False, "description": True}
name = models.CharField(max_length=150)
unit = models.ForeignKey(
"staff.OrganizationalUnit",
on_delete=models.CASCADE,
related_name="contact_groups",
)
description = models.TextField(blank=True)
is_active = models.BooleanField(default=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=["unit", "name"], name="unique_contact_group_name_per_unit")
]
ordering = ["unit__name", "name"]
verbose_name = "گروه مخاطبان"
verbose_name_plural = "گروه‌های مخاطبان"
def __str__(self):
return f"{self.unit}: {self.name}"
class CustomerProfile(PersianTextNormalizationMixin, TimeStampedModel):
persian_text_fields = {"first_name": False, "last_name": False, "organization_name": False, "province": False, "city": False}
class Type(models.TextChoices):
PERSON="PERSON","شخص"; CONSUMER="CONSUMER","مصرف‌کننده نهایی"; SHOP="SHOP","فروشگاه"; REPRESENTATIVE="REPRESENTATIVE","نمایندگی"; COMPANY="COMPANY","شرکت"; OTHER="OTHER","سایر"
class StakeholderType(models.TextChoices):
CUSTOMER = "CUSTOMER", "مشتری"
SUPPLIER = "SUPPLIER", "تامین‌کننده"
CARRIER = "CARRIER", "باربری"
OTHER = "OTHER", "سایر"
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="customer_profile")
first_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
customer_type = models.CharField(max_length=20, choices=Type.choices, blank=True)
stakeholder_type = models.CharField(
max_length=20,
choices=StakeholderType.choices,
default=StakeholderType.CUSTOMER,
db_index=True,
)
organization_name = models.CharField(max_length=180, blank=True)
groups = models.ManyToManyField(ContactGroup, blank=True, related_name="contacts")
province = models.CharField(max_length=100, blank=True)
city = models.CharField(max_length=100, blank=True)
def __str__(self): return (f"{self.first_name} {self.last_name}".strip() or self.user.mobile or self.user.username)