Compare commits

..

3 Commits

Author SHA1 Message Date
Tom Price
a3cdef8570 Tidy up the querying and move into model
This should really be in the model not in the view
2017-05-08 23:49:33 +01:00
Tom Price
7ec1c726a6 Optimise down to just the one SQL query for waiting invoices.
Cuts down on all the unnecessary queries and selects everything in one go.
Down from ~6 queries per invoice
2017-03-01 02:43:53 +00:00
Tom Price
4773b43081 Refactor outstanding invoice query to use django annotations.
Considerably improves performance and also removes the explict PSQL code.

Can be further improved by reducing the number of hits to the db in the template.
2017-03-01 02:05:06 +00:00
3 changed files with 36 additions and 21 deletions

View File

@@ -1,16 +1,17 @@
import cStringIO as StringIO
import datetime
import re
from decimal import Decimal
from django.contrib import messages
from django.core.urlresolvers import reverse_lazy
from django.db.models import Count, Sum, F, FloatField, Q, Value
from django.http import Http404, HttpResponseRedirect
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.template import RequestContext
from django.template.loader import get_template
from django.views import generic
from django.db.models import Q
from z3c.rml import rml2pdf
from RIGS import models
@@ -30,19 +31,8 @@ class InvoiceIndex(generic.ListView):
return context
def get_queryset(self):
# Manual query is the only way I have found to do this efficiently. Not ideal but needs must
sql = "SELECT * FROM " \
"(SELECT " \
"(SELECT COUNT(p.amount) FROM \"RIGS_payment\" AS p WHERE p.invoice_id=\"RIGS_invoice\".id) AS \"payment_count\", " \
"(SELECT SUM(ei.cost * ei.quantity) FROM \"RIGS_eventitem\" AS ei WHERE ei.event_id=\"RIGS_invoice\".event_id) AS \"cost\", " \
"(SELECT SUM(p.amount) FROM \"RIGS_payment\" AS p WHERE p.invoice_id=\"RIGS_invoice\".id) AS \"payments\", " \
"\"RIGS_invoice\".\"id\", \"RIGS_invoice\".\"event_id\", \"RIGS_invoice\".\"invoice_date\", \"RIGS_invoice\".\"void\" FROM \"RIGS_invoice\") " \
"AS sub " \
"WHERE (((cost > 0.0) AND (payment_count=0)) OR (cost - payments) <> 0.0) AND void = '0'" \
"ORDER BY invoice_date"
query = self.model.objects.raw(sql)
query = self.model.objects.outstanding().select_related('event', 'event__organisation', 'event__person',
'event__venue', 'event__mic')
return query
@@ -94,6 +84,7 @@ class InvoiceVoid(generic.View):
return HttpResponseRedirect(reverse_lazy('invoice_list'))
return HttpResponseRedirect(reverse_lazy('invoice_detail', kwargs={'pk': object.pk}))
class InvoiceDelete(generic.DeleteView):
model = models.Invoice
@@ -114,6 +105,7 @@ class InvoiceDelete(generic.DeleteView):
def get_success_url(self):
return self.request.POST.get('next')
class InvoiceArchive(generic.ListView):
model = models.Invoice
template_name = 'RIGS/invoice_list_archive.html'
@@ -142,11 +134,11 @@ class InvoiceWaiting(generic.ListView):
events = self.model.objects.filter(
(
Q(start_date__lte=datetime.date.today(), end_date__isnull=True) | # Starts before with no end
Q(end_date__lte=datetime.date.today()) # Has end date, finishes before
) & Q(invoice__isnull=True) # Has not already been invoiced
& Q(is_rig=True) # Is a rig (not non-rig)
).order_by('start_date') \
Q(end_date__lte=datetime.date.today()) # Has end date, finishes before
) & Q(invoice__isnull=True) # Has not already been invoiced
& Q(is_rig=True) # Is a rig (not non-rig)
).order_by('start_date') \
.select_related('person',
'organisation',
'venue', 'mic') \

View File

@@ -1,11 +1,11 @@
import datetime
import hashlib
import pytz
import random
import string
from collections import Counter
from decimal import Decimal
import pytz
import reversion
from django.conf import settings
from django.contrib.auth.models import AbstractUser
@@ -501,14 +501,34 @@ class EventCrew(models.Model):
notes = models.TextField(blank=True, null=True)
class InvoiceManager(models.Manager):
def outstanding(self):
return self.annotate(
_payment_total=models.Sum(models.F('payment__amount'))
).annotate(
_sum_total=models.Sum(models.F('event__items__cost') * models.F('event__items__quantity'),
output_field=models.DecimalField(decimal_places=2))
# ).annotate(
# _balance=models.ExpressionWrapper(models.F('_sum_total') - models.F('_payment_total'),
# models.DecimalField(decimal_places=2))
# ).filter(
# models.Q(_balance__isnull=True) |
# ~models.Q(_sum_total=models.F('_payment_total'))
)
@python_2_unicode_compatible
class Invoice(models.Model):
event = models.OneToOneField('Event')
invoice_date = models.DateField(auto_now_add=True)
void = models.BooleanField(default=False)
objects = InvoiceManager()
@property
def sum_total(self):
if getattr(self, '_sum_total', None):
return Decimal(getattr(self, '_sum_total'))
return self.event.sum_total
@property
@@ -517,6 +537,10 @@ class Invoice(models.Model):
@property
def payment_total(self):
if hasattr(self, '_payment_total') and hasattr(self, '_payment_count'):
if getattr(self, '_payment_count') == 0:
return Decimal(0.00)
return Decimal(getattr(self, '_payment_total', 0.00))
total = self.payment_set.aggregate(total=models.Sum('amount'))['total']
if total:
return total

View File

@@ -61,7 +61,6 @@ class EventOembed(generic.View):
'html': '<iframe src="{0}" frameborder="0" width="100%" height="250"></iframe>'.format(full_url),
'version': '1.0',
'type': 'rich',
'height': '250'
}
json = simplejson.JSONEncoderForHTML().encode(data)