Skip to content

Commit

Permalink
Some improvements to the sale overview
Browse files Browse the repository at this point in the history
- Add total row to each category grouping
- Add a separate table to the bottom with statistics for each category
- Add a column that display the ratio of sale that is to Crew (credit)
- Add functionality to get the sale overview as a CSV
  • Loading branch information
haavardlian committed Oct 11, 2023
1 parent 30b9260 commit cf8600e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 17 deletions.
2 changes: 2 additions & 0 deletions p0sx/p0sx/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
crew_report,
fetch_credit_from_ge,
sale_overview,
sale_overview_csv,
scan_user_card,
update_ge_user,
verify_add_credit,
Expand Down Expand Up @@ -59,6 +60,7 @@
url(r'edit_crew_credit/(?P<card>\w+)', credit_edit, name='edit_crew_credit'),
# url(r'sale/', sale_url, name='sale'),
url(r'sale/', sale_overview, name='sale'),
url(r'sale_csv/', sale_overview_csv, name='sale_csv'),
url(r'crew_report/', crew_report, name='crew_report'),
url(r'fetch_from_ge/', fetch_credit_from_ge, name='fetch_credit_from_ge'),
url(r'scan_user_card', scan_user_card, name='scan_user_card'),
Expand Down
71 changes: 70 additions & 1 deletion p0sx/pos/views/littleadmin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import csv
from datetime import datetime
from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -119,6 +120,53 @@ def credit_edit(request, card=None):
return render(request, 'pos/credit_edit.djhtml', {'form': form, 'target': user})


@login_required
def sale_overview_csv(request):
from_time = None
to_time = None

if request.POST:
form = TimeFilterForm(request.POST)
if form.is_valid():
from_time = form.cleaned_data['from_time']
to_time = form.cleaned_data['to_time']
else:
form = TimeFilterForm()

order_lines = OrderLine.objects.all().values('item__id', 'order__payment_method')
if from_time is not None and to_time is not None:
order_lines = order_lines.filter(order__date__gte=from_time).filter(order__date__lt=to_time)
order_lines = order_lines.annotate(total=Sum('price'),
first_sold=Min('order__date'),
last_sold=Max('order__date'),
sold=Sum(Case(When(price__gte=0, then=1), default=-1, output_field=IntegerField())))

items = Item.objects.all().values('name', 'category__name', 'id', 'price')

response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": 'attachment; filename="sale_overview.csv"'},
)

writer = csv.writer(response)
writer.writerow(["Name", "First sold", "Last sold", "Prepaid", "Credit", "Sold"])
for item in items:
per_payment_method = order_lines.filter(item_id=item['id'])
first_sold = per_payment_method.aggregate(Min('first_sold'))['first_sold__min']
last_sold = per_payment_method.aggregate(Max('last_sold'))['last_sold__max']
try:
credit = per_payment_method.filter(order__payment_method=1)[0]
except IndexError:
credit = {'sold': 0, 'total': 0, 'first_sold': None, 'last_sold': None}
try:
prepaid = per_payment_method.filter(order__payment_method=4)[0]
except IndexError:
prepaid = {'sold': 0, 'total': 0, 'first_sold': None, 'last_sold': None}

writer.writerow([item['name'], first_sold, last_sold, prepaid['total'], credit['total'], prepaid['sold'] + credit['sold']])

return response

@login_required
def sale_overview(request):
from_time = None
Expand Down Expand Up @@ -177,8 +225,29 @@ def sale_overview(request):
total['credit'] += item['credit']
total['total'] += item['total']

category_totals = []

for category_name, category_items in overview.items():
category_total = {}
category_total['name'] = "Total"
category_total['prepaid'] = 0
category_total['credit'] = 0
category_total['sold'] = 0
category_total['total'] = 0
for category_item in category_items:
print(category_item)
category_total['prepaid'] = category_total['prepaid'] + category_item['prepaid']
category_total['credit'] = category_total['credit'] + category_item['credit']
category_total['sold'] = category_total['sold'] + category_item['sold']
category_total['total'] = category_total['total'] + category_item['total']
category_items.append(category_total)

category_total_copy = category_total.copy();
category_total_copy['name'] = category_name
category_totals.append(category_total_copy)

shifts = ShiftSerializer(Shift.objects.all(), many=True)
return render(request, 'pos/sale_overview.djhtml', {'overview': overview, 'shifts': shifts.data, 'total': total, 'form': form})
return render(request, 'pos/sale_overview.djhtml', {'overview': overview, 'categories': category_totals, 'shifts': shifts.data, 'total': total, 'form': form})


@permission_required("pos.update_credit")
Expand Down
50 changes: 34 additions & 16 deletions p0sx/templates/pos/sale_overview.djhtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Sale overview
{% endblock title %}
{% block content %}

<h3>Export CSV</h3>
<form action="../sale_csv/" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="waves-effect waves-light btn cyan">CSV</button>
</form>

<h3>Filter this page</h3>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
Expand All @@ -27,6 +35,7 @@ Sale overview
<th>Prepaid</th>
<th>Credit</th>
<th>Total</th>
<th>Crew factor</th>
</tr>
</thead>
<tbody>
Expand All @@ -39,34 +48,41 @@ Sale overview
<td>{{ item.prepaid }}</td>
<td>{{ item.credit }}</td>
<td>{{ item.total }}</td>
<td>{% widthratio item.credit item.total 100 %}%</td>
</tr>
{% endfor %}
</tbody>
<tfoot>

</tfoot>
{% endfor %}
</table>

<h4>Shift/Total</h4>
<h4>Categories</h4>
<table>
<thead>
<tr>
<th>Category:</th>
<th>Sold</th>
<th>Prepaid</th>
<th>Credit</th>
<th>Total</th>
<th>Crew factor</th>
</tr>
</thead>
{% for category in categories %}
<tr>
<th colspan="4"><h5>Shifts</h5></th>
</tr>
<tr>
<th>Shift:</th>
<th>Prepaid</th>
<th>Credit</th>
<th>Total</th>
</tr>
{% for shift in shifts %}
<tr>
<td>{{ shift.shift_name }}</td>
<td>{{ shift.prepaid }}</td>
<td>{{ shift.credit }}</td>
<td>{{ shift.credit|add:shift.prepaid }}</td>
<td>{{ category.name }}</td>
<td>{{ category.sold }}</td>
<td>{{ category.prepaid }}</td>
<td>{{ category.credit }}</td>
<td>{{ category.total }}</td>
<td>{% widthratio category.credit category.total 100 %}%</td>
</tr>
{% endfor %}
</table>

<h4>Total</h4>
<table>
<tr>
<th colspan="4"><h5>Total</h5></th>
</tr>
Expand All @@ -75,12 +91,14 @@ Sale overview
<th>Prepaid</th>
<th>Credit</th>
<th>Total</th>
<th>Crew factor</th>
</tr>
<tr>
<th></th>
<td>{{ total.prepaid }}</td>
<td>{{ total.credit }}</td>
<td>{{ total.total }}</td>
<td>{% widthratio total.credit total.total 100 %}%</td>
</tr>
</table>
{% endblock content %}

0 comments on commit cf8600e

Please sign in to comment.