check in billing system models, admin, and sample data generator
[plstackapi.git] / planetstack / core / models / billing.py
1 import datetime
2 import os
3 import socket
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
10 class Account(PlCoreBase):
11     site = models.ForeignKey(Site, related_name="accounts", help_text="Site for this account")
12
13     @property
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"]
19         if (x==None):
20             return 0.0
21         return x
22
23     @property
24     def total_payments(self):
25         x=self.payments.all().aggregate(Sum('amount'))["amount__sum"]
26         if (x==None):
27             return 0.0
28         return x
29
30     @property
31     def balance_due(self):
32         return self.total_invoices - self.total_payments
33
34     def __unicode__(self):  return u'%s' % (self.site.name)
35
36 class Invoice(PlCoreBase):
37     date = models.DateTimeField()
38     account = models.ForeignKey(Account, related_name="invoices")
39
40     @property
41     def amount(self):
42         return str(self.charges.all().aggregate(Sum('amount'))["amount__sum"])
43
44     def __unicode__(self):  return u'%s-%s' % (self.account.site.name, str(self.date))
45
46 class UsableObject(PlCoreBase):
47     name = models.CharField(max_length=1024)
48
49     def __unicode__(self):  return u'%s' % (self.name)
50
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)
55
56     def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))
57
58 class Charge(PlCoreBase):
59     KIND_CHOICES = (('besteffort', 'besteffort'), ('reservation', 'reservation'), ('monthlyfee', 'monthlyfee'))
60     STATE_CHOICES = (('pending', 'pending'), ('invoiced', 'invoiced'))
61
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")
71
72     def __unicode__(self):  return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date))
73
74
75
76