From f41bd9c65bec605e36e346c2d1a8a5e2a2f146f5 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Mon, 19 Sep 2016 15:03:21 +1000 Subject: [PATCH] Adds paid invoices by date report --- registrasion/reporting/views.py | 43 ++++++++++++++++++++++++++++++++- registrasion/urls.py | 5 ++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/registrasion/reporting/views.py b/registrasion/reporting/views.py index 7770d76e..1eea40f3 100644 --- a/registrasion/reporting/views.py +++ b/registrasion/reporting/views.py @@ -1,10 +1,13 @@ import forms +import collections +import datetime + from django.contrib.auth.decorators import user_passes_test from django.core.urlresolvers import reverse from django.db import models from django.db.models import F, Q -from django.db.models import Count, Sum +from django.db.models import Count, Max, Sum from django.db.models import Case, When, Value from django.shortcuts import render @@ -265,6 +268,44 @@ def product_status(request, form): return ListReport("Inventory", headings, data) +@report_view("Paid invoices by date", form_type=forms.ProductAndCategoryForm) +def paid_invoices_by_date(request, form): + ''' Shows the number of paid invoices containing given products or + categories per day. ''' + + products = form.cleaned_data["product"] + categories = form.cleaned_data["category"] + + invoices = commerce.Invoice.objects.filter( + Q(lineitem__product__in=products) | Q(lineitem__product__category__in=categories), + status=commerce.Invoice.STATUS_PAID, + ) + + payments = commerce.PaymentBase.objects.all() + payments = payments.filter( + invoice__in=invoices, + ) + payments = payments.order_by("invoice") + invoice_max_time = payments.values("invoice").annotate(max_time=Max("time")) + + by_date = collections.defaultdict(int) + + for line in invoice_max_time: + time = line["max_time"] + date = datetime.datetime( + year=time.year, month=time.month, day=time.day + ) + by_date[date] += 1 + + data = [(date, count) for date, count in sorted(by_date.items())] + data = [(date.strftime("%Y-%m-%d"), count) for date, count in data] + + return ListReport( + "Paid Invoices By Date", + ["date", "count"], + data, + ) + @report_view("Credit notes") def credit_notes(request, form): ''' Shows all of the credit notes in the system. ''' diff --git a/registrasion/urls.py b/registrasion/urls.py index 4d746388..64fecbe0 100644 --- a/registrasion/urls.py +++ b/registrasion/urls.py @@ -46,6 +46,11 @@ reports = [ url(r"^attendee/([0-9]*)$", rv.attendee, name="attendee"), url(r"^credit_notes/?$", rv.credit_notes, name="credit_notes"), url(r"^items_sold/?$", rv.items_sold, name="items_sold"), + url( + r"^paid_invoices_by_date/?$", + rv.paid_invoices_by_date, + name="paid_invoices_by_date" + ), url(r"^product_status/?$", rv.product_status, name="product_status"), url(r"^reconciliation/?$", rv.reconciliation, name="reconciliation"), ]