Improve reports

This commit is contained in:
Gabriel Augendre 2022-04-25 18:59:32 +02:00
parent 150632e9c5
commit 13fd6a27a8
6 changed files with 58 additions and 12 deletions

View file

@ -21,6 +21,9 @@ class PaymentMethodQuerySet(models.QuerySet):
) )
) )
def with_sold(self):
return self.annotate(sold=Count("baskets", distinct=True))
class PaymentMethod(Model): class PaymentMethod(Model):
name = models.CharField(max_length=50, unique=True) name = models.CharField(max_length=50, unique=True)
@ -99,6 +102,9 @@ class BasketQuerySet(models.QuerySet):
price=Sum(F("items__quantity") * F("items__product__unit_price_cents")) price=Sum(F("items__quantity") * F("items__product__unit_price_cents"))
) )
def no_payment_method(self):
return self.filter(payment_method=None)
class Basket(Model): class Basket(Model):
payment_method = models.ForeignKey( payment_method = models.ForeignKey(
@ -112,7 +118,7 @@ class Basket(Model):
objects = BasketQuerySet.as_manager() objects = BasketQuerySet.as_manager()
def __str__(self): def __str__(self):
return f"Panier #{self.id}" return f"Basket #{self.id}"
def get_absolute_url(self): def get_absolute_url(self):
return reverse("purchase:update", args=(self.pk,)) return reverse("purchase:update", args=(self.pk,))

View file

@ -3,6 +3,7 @@
{% block content %} {% block content %}
{% if object %} {% if object %}
<h1>{{ object }}</h1> <h1>{{ object }}</h1>
<p>{{ object.created_at }}</p>
{% else %} {% else %}
<h1>New basket</h1> <h1>New basket</h1>
{% endif %} {% endif %}

View file

@ -4,16 +4,15 @@
{% block content %} {% block content %}
<h1>Reports</h1> <h1>Reports</h1>
<h2>Total turnover</h2> <h2>Total turnover</h2>
<ul> <ul><li>{{ total|currency }}</li></ul>
<li>
{{ total|currency }}
</li>
</ul>
<h2>Turnover by day</h2>
<h2>Products</h2> <h2>Products</h2>
{% include "purchase/snippets/report_products.html" %} {% include "purchase/snippets/report_products.html" %}
<h2>Turnover by product by day</h2>
<h2>Turnover by payment method</h2> <h2>Turnover by payment method</h2>
<h2>Turnover by payment method by day</h2> {% include "purchase/snippets/report_payment_methods.html" %}
<h2>Baskets without payment method</h2>
{% include "purchase/snippets/report_no_payment_method.html" %}
{% endblock %} {% endblock %}

View file

@ -0,0 +1,20 @@
{% load purchase %}
<table class="table table-hover table-sm">
<thead><tr>
<th scope="col">Basket ID</th>
<th scope="col">Price</th>
</tr></thead>
<tbody>
{% for basket in no_payment_method %}
<tr>
<th scope="row">
<a href="{% url "purchase:update" basket.id %}">
{{ basket }}
</a>
</th>
<td>{{ basket.price|currency }}</td>
</tr>
{% endfor %}
</tbody>
</table>

View file

@ -0,0 +1,18 @@
{% load purchase %}
<table class="table table-hover table-sm">
<thead><tr>
<th scope="col">Payment method</th>
<th scope="col"># baskets</th>
<th scope="col">Turnover</th>
</tr></thead>
<tbody>
{% for payment_method in payment_methods %}
<tr>
<th scope="row">{{ payment_method }}</th>
<td>{{ payment_method.sold }}</td>
<td>{{ payment_method.turnover|currency }}</td>
</tr>
{% endfor %}
</tbody>
</table>

View file

@ -2,9 +2,11 @@ from django.db.models import Sum
from django.views.generic import TemplateView from django.views.generic import TemplateView
from purchase.models import Basket, PaymentMethod, Product from purchase.models import Basket, PaymentMethod, Product
from purchase.views.utils import ProtectedViewsMixin
class ReportsView(TemplateView): class ReportsView(ProtectedViewsMixin, TemplateView):
permission_required = ["purchase.view_basket"]
template_name = "purchase/reports.html" template_name = "purchase/reports.html"
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
@ -12,9 +14,9 @@ class ReportsView(TemplateView):
context.update( context.update(
{ {
"total": Basket.objects.priced().aggregate(total=Sum("price"))["total"], "total": Basket.objects.priced().aggregate(total=Sum("price"))["total"],
"by_day": {},
"products": Product.objects.with_turnover().with_sold(), "products": Product.objects.with_turnover().with_sold(),
"payment_methods": PaymentMethod.objects.with_turnover(), "payment_methods": PaymentMethod.objects.with_turnover().with_sold(),
"no_payment_method": Basket.objects.no_payment_method().priced(),
} }
) )
return context return context