89 lines
3.1 KiB
Python
89 lines
3.1 KiB
Python
"""
|
|
Account models for managing customer accounts.
|
|
"""
|
|
import uuid
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
|
|
from backend.core.models.revenues.revenues import Revenue
|
|
from backend.core.models.labor.labor import Labor
|
|
|
|
|
|
class Account(models.Model):
|
|
"""Account model belonging to a customer"""
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
customer = models.ForeignKey('Customer', on_delete=models.CASCADE, related_name='accounts')
|
|
name = models.CharField(max_length=200)
|
|
|
|
# Address
|
|
street_address = models.CharField(max_length=255)
|
|
city = models.CharField(max_length=100)
|
|
state = models.CharField(max_length=100)
|
|
zip_code = models.CharField(max_length=20)
|
|
|
|
# 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)
|
|
|
|
# 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 = 'Account'
|
|
verbose_name_plural = 'Accounts'
|
|
ordering = ['name']
|
|
indexes = [
|
|
models.Index(fields=['customer']),
|
|
models.Index(fields=['name']),
|
|
]
|
|
|
|
def __str__(self):
|
|
return f"{self.name} ({self.customer.name})"
|
|
|
|
@property
|
|
def is_active(self):
|
|
"""Check if account 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 contact's full name"""
|
|
return f"{self.primary_contact_first_name} {self.primary_contact_last_name}"
|
|
|
|
@property
|
|
def secondary_contact_full_name(self):
|
|
"""Get contact's full name"""
|
|
return f"{self.secondary_contact_first_name} {self.secondary_contact_last_name}"
|
|
|
|
@property
|
|
def address(self):
|
|
"""Get formatted address"""
|
|
return f"{self.street_address}, {self.city}, {self.state} {self.zip_code}"
|
|
|
|
def current_revenue(self):
|
|
"""Get the current active revenue for this account"""
|
|
return Revenue.objects.filter(
|
|
account=self,
|
|
start_date__lte=timezone.now().date(),
|
|
end_date__isnull=True
|
|
).first()
|
|
|
|
def current_labor(self):
|
|
"""Get the current active labor for this account"""
|
|
return Labor.objects.filter(
|
|
account=self,
|
|
start_date__lte=timezone.now().date(),
|
|
end_date__isnull=True
|
|
).first() |