Reverted to using Django ORM for the calculation of event totals and payment totals on an invoice.

Need to pay closer attention to the docs which state that the SUM(arg[0], fields) arg[0] is the returned type.
This commit is contained in:
root
2015-03-03 17:29:38 +00:00
parent a3b30ceed8
commit ed660b9106

View File

@@ -7,6 +7,7 @@ from django.conf import settings
from django.utils.functional import cached_property
import reversion
from decimal import Decimal
# Create your models here.
class Profile(AbstractUser):
@@ -248,14 +249,20 @@ class Event(models.Model, RevisionMixin):
@property
def sum_total(self):
# Manual querying is required for efficiency whilst maintaining floating point arithmetic
if connection.vendor == 'postgresql':
sql = "SELECT SUM(quantity * cost) AS sum_total FROM \"RIGS_eventitem\" WHERE event_id=%i" % self.id
else:
sql = "SELECT id, SUM(quantity * cost) AS sum_total FROM RIGS_eventitem WHERE event_id=%i" % self.id
total = self.items.raw(sql)[0]
if total.sum_total:
return total.sum_total
return 0.0
# if connection.vendor == 'postgresql':
# sql = "SELECT SUM(quantity * cost) AS sum_total FROM \"RIGS_eventitem\" WHERE event_id=%i" % self.id
# else:
# sql = "SELECT id, SUM(quantity * cost) AS sum_total FROM RIGS_eventitem WHERE event_id=%i" % self.id
#total = self.items.raw(sql)[0]
#if total.sum_total:
# return total.sum_total
#total = 0.0
#for item in self.items.filter(cost__gt=0).extra(select="SUM(cost * quantity) AS sum"):
# total += item.sum
total = EventItem.objects.filter(event=self, cost__gt=0).aggregate(sum_total=models.Sum('cost',field="quantity * cost"))['sum_total']
if total:
return total
return Decimal("0.00")
@cached_property
def vat_rate(self):
@@ -335,18 +342,22 @@ class Invoice(models.Model):
@property
def payment_total(self):
# Manual querying is required for efficiency whilst maintaining floating point arithmetic
if connection.vendor == 'postgresql':
sql = "SELECT SUM(amount) AS total FROM \"RIGS_payment\" WHERE invoice_id=%i" % self.id
else:
sql = "SELECT id, SUM(amount) AS total FROM RIGS_payment WHERE invoice_id=%i" % self.id
total = self.payment_set.raw(sql)[0]
if total.total:
return total.total
return 0.0
#if connection.vendor == 'postgresql':
# sql = "SELECT SUM(amount) AS total FROM \"RIGS_payment\" WHERE invoice_id=%i" % self.id
#else:
# sql = "SELECT id, SUM(amount) AS total FROM RIGS_payment WHERE invoice_id=%i" % self.id
#total = self.payment_set.raw(sql)[0]
#if total.total:
# return total.total
#return 0.0
total = self.payment_set.aggregate(total=models.Sum('amount'))['total']
if total:
return total
return Decimal("0.00")
@property
def balance(self):
return float(self.sum_total) - float(self.payment_total)
return self.sum_total - self.payment_total
def __str__(self):
return "%i: %s (%.2f)" % (self.pk, self.event, self.balance)
@@ -374,4 +385,4 @@ class Payment(models.Model):
invoice = models.ForeignKey('Invoice')
date = models.DateField()
amount = models.DecimalField(max_digits=10, decimal_places=2, help_text='Please use ex. VAT')
method = models.CharField(max_length=2, choices=METHODS, null=True, blank=True)
method = models.CharField(max_length=2, choices=METHODS, null=True, blank=True)