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
10 class Account(PlCoreBase):
11 site = models.ForeignKey(Site, related_name="accounts", help_text="Site for this account")
14 def total_invoices(self):
15 # Since the amount of an invoice is the sum of it's charges, we can
16 # compute the sum of the invoices by summing all charges where
17 # charge.invoice != Null.
18 x=self.charges.filter(invoice__isnull=False).aggregate(Sum('amount'))["amount__sum"]
24 def total_payments(self):
25 x=self.payments.all().aggregate(Sum('amount'))["amount__sum"]
31 def balance_due(self):
32 return self.total_invoices - self.total_payments
34 def __unicode__(self): return u'%s' % (self.site.name)
36 class Invoice(PlCoreBase):
37 date = models.DateTimeField()
38 account = models.ForeignKey(Account, related_name="invoices")
42 return str(self.charges.all().aggregate(Sum('amount'))["amount__sum"])
44 def __unicode__(self): return u'%s-%s' % (self.account.site.name, str(self.date))
46 class UsableObject(PlCoreBase):
47 name = models.CharField(max_length=1024)
49 def __unicode__(self): return u'%s' % (self.name)
51 class Payment(PlCoreBase):
52 account = models.ForeignKey(Account, related_name="payments")
53 amount = models.FloatField(default=0.0)
54 date = models.DateTimeField(default=datetime.datetime.now)
56 def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))
58 class Charge(PlCoreBase):
59 KIND_CHOICES = (('besteffort', 'besteffort'), ('reservation', 'reservation'), ('monthlyfee', 'monthlyfee'))
60 STATE_CHOICES = (('pending', 'pending'), ('invoiced', 'invoiced'))
62 account = models.ForeignKey(Account, related_name="charges")
63 slice = models.ForeignKey(Slice, related_name="charges", null=True, blank=True)
64 kind = models.CharField(max_length=30, choices=KIND_CHOICES, default="besteffort")
65 state = models.CharField(max_length=30, choices=STATE_CHOICES, default="pending")
66 date = models.DateTimeField()
67 object = models.ForeignKey(UsableObject)
68 amount = models.FloatField(default=0.0)
69 coreHours = models.FloatField(default=0.0)
70 invoice = models.ForeignKey(Invoice, blank=True, null=True, related_name="charges")
72 def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))