Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'total sales monthly' reporting feature for treasurer #235

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% extends "admin/base_site.html" %}
{% load static %}

{% block title %}Total sales monthly{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs"><a href="../..">Hjem</a></div>{% endblock %}

{% block content %}
{% load admin_static %}

<h1>Total sales monthly</h1>
<p>Table showing total registered sales during a given month in the past year. Useful for the treasurer for
bookkeeping.</p>
<table>
<thead>
<tr>
<th>Year</th>
<th>Month</th>
<th>DKK</th>
</tr>
</thead>
<tr></tr>
{% for entry in total_sales_monthly %}
<tr>
<td>
{{ entry.year }}
</td>
<td>
{{ entry.month }}
</td>
<td>
{{ entry.sales_amount }}
</td>
</tr>
{% endfor %}
</table>
{% endblock %}
5 changes: 5 additions & 0 deletions stregsystem/templates/shared_report_lists.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><a href = "/admin/totalsales/">Total sales monthly</a></th>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</div>

Expand Down
1 change: 1 addition & 0 deletions stregsystem/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
re_path(r'^$', views.roomindex, name="index"),
re_path(r'^admin/batch/$', views.batch_payment, name="batch"),
re_path(r'^admin/mobilepaytool/$', views.mobilepaytool, name="mobilepaytool"),
re_path(r'^admin/totalsales/$', views.total_sales_monthly, name="total_monthly_sales"),
re_path(r'^(?P<room_id>\d+)/$', views.index, name="menu_index"),
re_path(r'^(?P<room_id>\d+)/sale/$', views.sale, name="quickbuy"),
re_path(r'^(?P<room_id>\d+)/sale/(?P<member_id>\d+)/$', views.menu_sale, name="menu"),
Expand Down
15 changes: 15 additions & 0 deletions stregsystem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ def make_unprocessed_member_filled_mobilepayment_query():
Q(payment__isnull=True) & Q(status=MobilePayment.UNSET) & Q(member__isnull=False))


def total_sales_monthly_query(month_year_pair):
"""
Generates list of tuples (year, month, sum of sales for that month) given an input list of tuples (month, year)
"""
from django.db.models import Sum
from stregsystem.templatetags.stregsystem_extras import money
from stregsystem.models import Sale
import datetime

res = list()
for m, y in month_year_pair:
res.append((y, m, money(
Sale.objects.filter(timestamp__month=m, timestamp__year=y).aggregate(Sum('price'))['price__sum'])))
return res

def date_to_midnight(date):
"""
Converts a datetime.date to a datetime of the same date at midnight.
Expand Down
23 changes: 21 additions & 2 deletions stregsystem/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import calendar
import datetime

from django.core import management
Expand Down Expand Up @@ -32,7 +33,8 @@
qr_code,
make_room_specific_query,
make_unprocessed_mobilepayment_query,
parse_csv_and_create_mobile_payments
parse_csv_and_create_mobile_payments,
total_sales_monthly_query
)

from .booze import ballmer_peak
Expand Down Expand Up @@ -364,7 +366,7 @@ def mobilepaytool(request):

return render(request, "admin/stregsystem/mobilepaytool.html", data)


def qr_payment(request):
form = QRPaymentForm(request.GET)
if not form.is_valid():
Expand All @@ -381,3 +383,20 @@ def qr_payment(request):
data = 'mobilepay://send?{}'.format(urllib.parse.urlencode(query))

return qr_code(data)


@staff_member_required()
def total_sales_monthly(request):
data = dict()

# generate pairs (month, year) from current month, going a year back
last_year = datetime.datetime.today().year - 1
current_month = datetime.datetime.today().month
months = [((m + current_month) % 12 + 1, last_year + (m + current_month) // 12) for m in range(0, 12)]

# for each month, year pair, sum all sales in that period, and convert numeric month to text
data['total_sales_monthly'] = list()
for y, m, total in total_sales_monthly_query(months):
data['total_sales_monthly'].append({'year': y, 'month': calendar.month_name[m], 'sales_amount': total})

return render(request, "admin/stregsystem/report/total_sales_monthly.html", data)