mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-24 17:02:18 +00:00
Tidy up the querying and move into model
This should really be in the model not in the view
This commit is contained in:
@@ -31,15 +31,8 @@ class InvoiceIndex(generic.ListView):
|
|||||||
return context
|
return context
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
query = self.model.objects.annotate(
|
query = self.model.objects.outstanding().select_related('event', 'event__organisation', 'event__person',
|
||||||
_sum_total=Sum(F('event__items__cost') * F('event__items__quantity'), output_field=FloatField()),
|
'event__venue', 'event__mic')
|
||||||
_payment_count=Count('payment'),
|
|
||||||
_payment_total=Sum('payment__amount'),
|
|
||||||
).filter(
|
|
||||||
Q(_sum_total__gt=0.0, _payment_count=0) |
|
|
||||||
Q(_sum_total__lt=Value('_payment_total'), _sum_total__gt=Value('_payment_total'))
|
|
||||||
).select_related('event', 'event__organisation', 'event__person', 'event__venue', 'event__mic')
|
|
||||||
|
|
||||||
return query
|
return query
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -501,12 +501,30 @@ class EventCrew(models.Model):
|
|||||||
notes = models.TextField(blank=True, null=True)
|
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
|
@python_2_unicode_compatible
|
||||||
class Invoice(models.Model):
|
class Invoice(models.Model):
|
||||||
event = models.OneToOneField('Event')
|
event = models.OneToOneField('Event')
|
||||||
invoice_date = models.DateField(auto_now_add=True)
|
invoice_date = models.DateField(auto_now_add=True)
|
||||||
void = models.BooleanField(default=False)
|
void = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
objects = InvoiceManager()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def sum_total(self):
|
def sum_total(self):
|
||||||
if getattr(self, '_sum_total', None):
|
if getattr(self, '_sum_total', None):
|
||||||
|
|||||||
Reference in New Issue
Block a user