4 from django.db import models
5 from core.models import PlCoreBase, Site, Slice, Sliver, Deployment
6 from django.contrib.contenttypes.models import ContentType
7 from django.contrib.contenttypes import generic
8 from django.db.models import Sum
9 from django.utils import timezone
11 class Account(PlCoreBase):
12 site = models.ForeignKey(Site, related_name="accounts", help_text="Site for this account")
15 def total_invoices(self):
16 # Since the amount of an invoice is the sum of it's charges, we can
17 # compute the sum of the invoices by summing all charges where
18 # charge.invoice != Null.
19 x=self.charges.filter(invoice__isnull=False).aggregate(Sum('amount'))["amount__sum"]
25 def total_payments(self):
26 x=self.payments.all().aggregate(Sum('amount'))["amount__sum"]
32 def balance_due(self):
33 return self.total_invoices - self.total_payments
35 def __unicode__(self): return u'%s' % (self.site.name)
37 class Invoice(PlCoreBase):
38 date = models.DateTimeField()
39 account = models.ForeignKey(Account, related_name="invoices")
43 return str(self.charges.all().aggregate(Sum('amount'))["amount__sum"])
45 def __unicode__(self): return u'%s-%s' % (self.account.site.name, str(self.date))
47 class UsableObject(PlCoreBase):
48 name = models.CharField(max_length=1024)
50 def __unicode__(self): return u'%s' % (self.name)
52 class Payment(PlCoreBase):
53 account = models.ForeignKey(Account, related_name="payments")
54 amount = models.FloatField(default=0.0)
55 date = models.DateTimeField(default=timezone.now)
57 def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))
59 class Charge(PlCoreBase):
60 KIND_CHOICES = (('besteffort', 'besteffort'), ('reservation', 'reservation'), ('monthlyfee', 'monthlyfee'))
61 STATE_CHOICES = (('pending', 'pending'), ('invoiced', 'invoiced'))
63 account = models.ForeignKey(Account, related_name="charges")
64 slice = models.ForeignKey(Slice, related_name="charges", null=True, blank=True)
65 kind = models.CharField(max_length=30, choices=KIND_CHOICES, default="besteffort")
66 state = models.CharField(max_length=30, choices=STATE_CHOICES, default="pending")
67 date = models.DateTimeField()
68 object = models.ForeignKey(UsableObject)
69 amount = models.FloatField(default=0.0)
70 coreHours = models.FloatField(default=0.0)
71 invoice = models.ForeignKey(Invoice, blank=True, null=True, related_name="charges")
73 def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))