2026-01-26 10:30:49 -05:00

80 lines
3.0 KiB
Python

"""
Customer models for managing customer information.
"""
import uuid
from django.db import models
from django.utils import timezone
class Customer(models.Model):
"""Customer model with contact information"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=200)
# Primary contact
primary_contact_first_name = models.CharField(max_length=100)
primary_contact_last_name = models.CharField(max_length=100)
primary_contact_phone = models.CharField(max_length=20)
primary_contact_email = models.EmailField()
# Secondary contact (optional)
secondary_contact_first_name = models.CharField(max_length=100, blank=True, null=True)
secondary_contact_last_name = models.CharField(max_length=100, blank=True, null=True)
secondary_contact_phone = models.CharField(max_length=20, blank=True, null=True)
secondary_contact_email = models.EmailField(blank=True, null=True)
# Billing information
billing_contact_first_name = models.CharField(max_length=100)
billing_contact_last_name = models.CharField(max_length=100)
billing_street_address = models.CharField(max_length=255)
billing_city = models.CharField(max_length=100)
billing_state = models.CharField(max_length=100)
billing_zip_code = models.CharField(max_length=20)
billing_email = models.EmailField()
billing_terms = models.TextField()
# Dates
start_date = models.DateField()
end_date = models.DateField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
app_label = 'core'
verbose_name = 'Customer'
verbose_name_plural = 'Customers'
ordering = ['name']
indexes = [
models.Index(fields=['name']),
models.Index(fields=['primary_contact_email']),
]
def __str__(self):
return self.name
@property
def is_active(self):
"""Check if customer is active based on end_date"""
return self.end_date is None or self.end_date > timezone.now().date()
@property
def primary_contact_full_name(self):
"""Get primary contact's full name"""
return f"{self.primary_contact_first_name} {self.primary_contact_last_name}"
@property
def secondary_contact_full_name(self):
"""Get secondary contact's full name"""
if self.secondary_contact_first_name and self.secondary_contact_last_name:
return f"{self.secondary_contact_first_name} {self.secondary_contact_last_name}"
return None
@property
def billing_contact_full_name(self):
"""Get billing contact's full name"""
return f"{self.billing_contact_first_name} {self.billing_contact_last_name}"
@property
def billing_address(self):
"""Get formatted billing address"""
return f"{self.billing_street_address}, {self.billing_city}, {self.billing_state} {self.billing_zip_code}"