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 |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 5 | from core.models import PlCoreBase, Site, Slice, Instance, Deployment |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 6 | from core.models.plcorebase import StrippedCharField |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 7 | from django.contrib.contenttypes.models import ContentType |
| 8 | from django.contrib.contenttypes import generic |
| 9 | from django.db.models import Sum |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 10 | from django.utils import timezone |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 11 | |
| 12 | class 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 | |
| 38 | class 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 | |
| 48 | class UsableObject(PlCoreBase): |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 49 | name = StrippedCharField(max_length=1024) |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 50 | |
| 51 | def __unicode__(self): return u'%s' % (self.name) |
| 52 | |
| 53 | class Payment(PlCoreBase): |
| 54 | account = models.ForeignKey(Account, related_name="payments") |
| 55 | amount = models.FloatField(default=0.0) |
Scott Baker | 02b5952 | 2014-09-17 22:18:46 -0700 | [diff] [blame] | 56 | date = models.DateTimeField(default=timezone.now) |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 57 | |
| 58 | def __unicode__(self): return u'%s-%0.2f-%s' % (self.account.site.name, self.amount, str(self.date)) |
| 59 | |
| 60 | class 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 Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 66 | kind = StrippedCharField(max_length=30, choices=KIND_CHOICES, default="besteffort") |
| 67 | state = StrippedCharField(max_length=30, choices=STATE_CHOICES, default="pending") |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 68 | 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 | |