blob: 8c614184876327412e118e50d7f9ca1d4b45ab54 [file] [log] [blame]
Scott Baker43105042013-12-06 23:23:36 -08001import datetime
2import os
3import socket
4from django.db import models
5from core.models import PlCoreBase, Site, Slice, Sliver, Deployment
6from django.contrib.contenttypes.models import ContentType
7from django.contrib.contenttypes import generic
8from django.db.models import Sum
Scott Baker02b59522014-09-17 22:18:46 -07009from django.utils import timezone
Scott Baker43105042013-12-06 23:23:36 -080010
11class Account(PlCoreBase):
12 site = models.ForeignKey(Site, related_name="accounts", help_text="Site for this account")
13
14 @property
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"]
20 if (x==None):
21 return 0.0
22 return x
23
24 @property
25 def total_payments(self):
26 x=self.payments.all().aggregate(Sum('amount'))["amount__sum"]
27 if (x==None):
28 return 0.0
29 return x
30
31 @property
32 def balance_due(self):
33 return self.total_invoices - self.total_payments
34
35 def __unicode__(self): return u'%s' % (self.site.name)
36
37class Invoice(PlCoreBase):
38 date = models.DateTimeField()
39 account = models.ForeignKey(Account, related_name="invoices")
40
41 @property
42 def amount(self):
43 return str(self.charges.all().aggregate(Sum('amount'))["amount__sum"])
44
45 def __unicode__(self): return u'%s-%s' % (self.account.site.name, str(self.date))
46
47class UsableObject(PlCoreBase):
48 name = models.CharField(max_length=1024)
49
50 def __unicode__(self): return u'%s' % (self.name)
51
52class Payment(PlCoreBase):
53 account = models.ForeignKey(Account, related_name="payments")
54 amount = models.FloatField(default=0.0)
Scott Baker02b59522014-09-17 22:18:46 -070055 date = models.DateTimeField(default=timezone.now)
Scott Baker43105042013-12-06 23:23:36 -080056
57 def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))
58
59class Charge(PlCoreBase):
60 KIND_CHOICES = (('besteffort', 'besteffort'), ('reservation', 'reservation'), ('monthlyfee', 'monthlyfee'))
61 STATE_CHOICES = (('pending', 'pending'), ('invoiced', 'invoiced'))
62
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")
72
73 def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))
74
75
76
77