blob: 6e517b4101890c86a8574fa489ce085b8627b522 [file] [log] [blame]
Scott Baker43105042013-12-06 23:23:36 -08001import datetime
2import os
3import socket
4from django.db import models
Tony Mackd8515472015-08-19 11:58:18 -04005from core.models import PlCoreBase, Site, Slice, Instance, Deployment
Tony Mack50e12212015-03-09 13:03:56 -04006from core.models.plcorebase import StrippedCharField
Scott Baker43105042013-12-06 23:23:36 -08007from django.contrib.contenttypes.models import ContentType
8from django.contrib.contenttypes import generic
9from django.db.models import Sum
Scott Baker02b59522014-09-17 22:18:46 -070010from django.utils import timezone
Scott Baker43105042013-12-06 23:23:36 -080011
12class Account(PlCoreBase):
13 site = models.ForeignKey(Site, related_name="accounts", help_text="Site for this account")
14
15 @property
16 def total_invoices(self):
17 # Since the amount of an invoice is the sum of it's charges, we can
18 # compute the sum of the invoices by summing all charges where
19 # charge.invoice != Null.
20 x=self.charges.filter(invoice__isnull=False).aggregate(Sum('amount'))["amount__sum"]
21 if (x==None):
22 return 0.0
23 return x
24
25 @property
26 def total_payments(self):
27 x=self.payments.all().aggregate(Sum('amount'))["amount__sum"]
28 if (x==None):
29 return 0.0
30 return x
31
32 @property
33 def balance_due(self):
34 return self.total_invoices - self.total_payments
35
36 def __unicode__(self): return u'%s' % (self.site.name)
37
38class Invoice(PlCoreBase):
39 date = models.DateTimeField()
40 account = models.ForeignKey(Account, related_name="invoices")
41
42 @property
43 def amount(self):
44 return str(self.charges.all().aggregate(Sum('amount'))["amount__sum"])
45
46 def __unicode__(self): return u'%s-%s' % (self.account.site.name, str(self.date))
47
48class UsableObject(PlCoreBase):
Tony Mack50e12212015-03-09 13:03:56 -040049 name = StrippedCharField(max_length=1024)
Scott Baker43105042013-12-06 23:23:36 -080050
51 def __unicode__(self): return u'%s' % (self.name)
52
53class Payment(PlCoreBase):
54 account = models.ForeignKey(Account, related_name="payments")
55 amount = models.FloatField(default=0.0)
Scott Baker02b59522014-09-17 22:18:46 -070056 date = models.DateTimeField(default=timezone.now)
Scott Baker43105042013-12-06 23:23:36 -080057
58 def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))
59
60class Charge(PlCoreBase):
61 KIND_CHOICES = (('besteffort', 'besteffort'), ('reservation', 'reservation'), ('monthlyfee', 'monthlyfee'))
62 STATE_CHOICES = (('pending', 'pending'), ('invoiced', 'invoiced'))
63
64 account = models.ForeignKey(Account, related_name="charges")
65 slice = models.ForeignKey(Slice, related_name="charges", null=True, blank=True)
Tony Mack50e12212015-03-09 13:03:56 -040066 kind = StrippedCharField(max_length=30, choices=KIND_CHOICES, default="besteffort")
67 state = StrippedCharField(max_length=30, choices=STATE_CHOICES, default="pending")
Scott Baker43105042013-12-06 23:23:36 -080068 date = models.DateTimeField()
69 object = models.ForeignKey(UsableObject)
70 amount = models.FloatField(default=0.0)
71 coreHours = models.FloatField(default=0.0)
72 invoice = models.ForeignKey(Invoice, blank=True, null=True, related_name="charges")
73
74 def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))
75
76
77
78