42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
"""
|
|
Revenue models for tracking account revenue.
|
|
"""
|
|
import uuid
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
|
|
|
|
class Revenue(models.Model):
|
|
"""Revenue records for accounts"""
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
account = models.ForeignKey('Account', on_delete=models.CASCADE, related_name='revenues')
|
|
amount = models.DecimalField(max_digits=10, decimal_places=2)
|
|
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 = 'Revenue'
|
|
verbose_name_plural = 'Revenues'
|
|
ordering = ['-start_date']
|
|
indexes = [
|
|
models.Index(fields=['account']),
|
|
models.Index(fields=['start_date']),
|
|
]
|
|
|
|
def __str__(self):
|
|
return f"{self.account.name} - ${self.amount}"
|
|
|
|
@property
|
|
def is_active(self):
|
|
"""Check if revenue record is active based on end_date"""
|
|
return self.end_date is None or self.end_date > timezone.now().date()
|
|
|
|
@property
|
|
def duration_days(self):
|
|
"""Calculate the duration in days"""
|
|
if not self.end_date:
|
|
return (timezone.now().date() - self.start_date).days
|
|
return (self.end_date - self.start_date).days |