mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-26 09:52:16 +00:00
Changed the calculations of sum totals to be raw SQL queries for speed boost. Not ideal but the best we are going to get.
Changed outstanding invoice to a raw SQL query for a speed boost. Average load now ~3.5s with ~10 invoices
This commit is contained in:
@@ -13,13 +13,20 @@ class InvoiceIndex(generic.ListView):
|
|||||||
template_name = 'RIGS/invoice_list.html'
|
template_name = 'RIGS/invoice_list.html'
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
active = self.model.objects.filter(void=False).select_related('payment_set', 'event').prefetch_related(
|
# Manual query is the only way I have found to do this efficiently. Not ideal but needs must
|
||||||
'event__items').defer('event__person', 'event__organisation', 'event__venue', 'event__mic')
|
query = self.model.objects.raw(
|
||||||
set = []
|
"SELECT *, "
|
||||||
for invoice in active:
|
"(SELECT SUM(ei.cost * ei.quantity) FROM RIGS_eventitem AS ei WHERE ei.event_id = i.event_id) AS cost, "
|
||||||
if invoice.balance != 0:
|
"(SELECT SUM(p.amount) FROM RIGS_payment AS p WHERE p.invoice_id = i.id) AS payments FROM RIGS_invoice as i "
|
||||||
set.append(invoice)
|
"HAVING (cost - payments) > 0;"
|
||||||
return set
|
)
|
||||||
|
|
||||||
|
items = []
|
||||||
|
|
||||||
|
for invoice in query:
|
||||||
|
items.append(invoice)
|
||||||
|
|
||||||
|
return query
|
||||||
|
|
||||||
|
|
||||||
class InvoiceDetail(generic.DetailView):
|
class InvoiceDetail(generic.DetailView):
|
||||||
|
|||||||
20
RIGS/migrations/0019_auto_20150131_1919.py
Normal file
20
RIGS/migrations/0019_auto_20150131_1919.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import models, migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('RIGS', '0018_auto_20150130_0016'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='payment',
|
||||||
|
name='method',
|
||||||
|
field=models.CharField(blank=True, max_length=2, null=True, choices=[(b'C', b'Cash'), (b'I', b'Internal'), (b'E', b'External'), (b'SU', b'SU Core')]),
|
||||||
|
preserve_default=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
20
RIGS/migrations/0020_auto_20150303_0243.py
Normal file
20
RIGS/migrations/0020_auto_20150303_0243.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import models, migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('RIGS', '0019_auto_20150131_1919'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='payment',
|
||||||
|
name='method',
|
||||||
|
field=models.CharField(blank=True, max_length=2, null=True, choices=[(b'C', b'Cash'), (b'I', b'Internal'), (b'E', b'External'), (b'SU', b'SU Core'), (b'T', b'TEC Adjustment')]),
|
||||||
|
preserve_default=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -247,10 +247,11 @@ class Event(models.Model, RevisionMixin):
|
|||||||
"""
|
"""
|
||||||
@property
|
@property
|
||||||
def sum_total(self):
|
def sum_total(self):
|
||||||
total = 0
|
# Manual querying is required for efficiency whilst maintaining floating point arithmetic
|
||||||
for item in self.items.filter(cost__gt=0):
|
total = self.items.raw("SELECT id, SUM(quantity * cost) AS sum_total FROM RIGS_eventitem WHERE event_id=%i" % self.id)[0]
|
||||||
total += item.total_cost
|
if total.sum_total:
|
||||||
return total
|
return total.sum_total
|
||||||
|
return 0.0
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def vat_rate(self):
|
def vat_rate(self):
|
||||||
@@ -329,16 +330,15 @@ class Invoice(models.Model):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def payment_total(self):
|
def payment_total(self):
|
||||||
sum = self.payment_set.aggregate(models.Sum('amount'))['amount__sum']
|
# Manual querying is required for efficiency whilst maintaining floating point arithmetic
|
||||||
# for payment in self.payment_set.all():
|
total = self.payment_set.raw("SELECT id, SUM(amount) AS total FROM RIGS_payment WHERE invoice_id=%i" % self.id)[0]
|
||||||
# total += payment.amount
|
if total.total:
|
||||||
if sum:
|
return total.total
|
||||||
return sum
|
return 0.0
|
||||||
return 0
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def balance(self):
|
def balance(self):
|
||||||
return self.sum_total - self.payment_total
|
return float(self.sum_total) - float(self.payment_total)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%i: %s (%.2f)" % (self.pk, self.event, self.balance)
|
return "%i: %s (%.2f)" % (self.pk, self.event, self.balance)
|
||||||
|
|||||||
Reference in New Issue
Block a user