Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 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 |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 9 | from django.utils import timezone |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 10 | |
| 11 | class 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 | |
| 37 | class 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 | |
| 47 | class UsableObject(PlCoreBase): |
| 48 | name = models.CharField(max_length=1024) |
| 49 | |
| 50 | def __unicode__(self): return u'%s' % (self.name) |
| 51 | |
| 52 | class Payment(PlCoreBase): |
| 53 | account = models.ForeignKey(Account, related_name="payments") |
| 54 | amount = models.FloatField(default=0.0) |
Scott Baker | b24f2c3 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 55 | date = models.DateTimeField(default=timezone.now) |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 56 | |
| 57 | def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date)) |
| 58 | |
| 59 | class 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 | |