Superimpose products plot

This commit is contained in:
Gabriel Augendre 2022-04-26 20:11:36 +02:00
parent ca5e948477
commit 9f72505898
2 changed files with 20 additions and 27 deletions

View file

@ -28,8 +28,7 @@
<h2>{% translate "Products" %}</h2> <h2>{% translate "Products" %}</h2>
{% include "purchase/snippets/report_products.html" %} {% include "purchase/snippets/report_products.html" %}
{{ products_sold_plot|safe }} {{ products_plot|safe }}
{{ products_turnover_plot|safe }}
<h2>{% translate "Turnover by payment method" %}</h2> <h2>{% translate "Turnover by payment method" %}</h2>
{% include "purchase/snippets/report_payment_methods.html" %} {% include "purchase/snippets/report_payment_methods.html" %}

View file

@ -31,8 +31,7 @@ class ReportsView(ProtectedViewsMixin, TemplateView):
} }
products = Product.objects.with_turnover().with_sold() products = Product.objects.with_turnover().with_sold()
products_sold_plot = self.get_products_sold_plot(products) products_plot = self.get_products_plot(products)
products_turnover_plot = self.get_products_turnover_plot(products)
baskets = list(Basket.objects.priced().order_by("created_at")) baskets = list(Basket.objects.priced().order_by("created_at"))
by_hour_plot = self.by_hour_plot(baskets) by_hour_plot = self.by_hour_plot(baskets)
context.update( context.update(
@ -42,8 +41,7 @@ class ReportsView(ProtectedViewsMixin, TemplateView):
"average_basket": Basket.objects.average_basket(), "average_basket": Basket.objects.average_basket(),
"average_basket_by_day": average_basket_by_day, "average_basket_by_day": average_basket_by_day,
"products": products, "products": products,
"products_sold_plot": products_sold_plot, "products_plot": products_plot,
"products_turnover_plot": products_turnover_plot,
"by_hour_plot": by_hour_plot, "by_hour_plot": by_hour_plot,
"payment_methods": PaymentMethod.objects.with_turnover().with_sold(), "payment_methods": PaymentMethod.objects.with_turnover().with_sold(),
"no_payment_method": Basket.objects.no_payment_method().priced(), "no_payment_method": Basket.objects.no_payment_method().priced(),
@ -51,33 +49,29 @@ class ReportsView(ProtectedViewsMixin, TemplateView):
) )
return context return context
def get_products_sold_plot(self, products: ProductQuerySet): def get_products_plot(self, products: ProductQuerySet):
labels = [] labels = []
values = [] sold = []
turnover = []
for product in products: for product in products:
labels.append(product.name) labels.append(product.name)
values.append(product.sold) sold.append(product.sold)
fig = plt.figure() turnover.append(product.turnover / 100)
plt.bar(labels, values)
plt.xticks(rotation=15)
image_data = StringIO()
plt.title(_("# sold"))
fig.savefig(image_data, format="svg")
image_data.seek(0)
return image_data.getvalue()
def get_products_turnover_plot(self, products: ProductQuerySet): fig, ax1 = plt.subplots()
labels = [] color = "tab:orange"
values = [] ax1.bar(labels, sold, width=0.8, color=color)
for product in products: ax1.tick_params(axis="x", rotation=15)
labels.append(product.name) ax1.set_ylabel(_("# sold"), color=color)
values.append(product.turnover / 100)
fig = plt.figure() color = "tab:blue"
plt.bar(labels, values) ax2 = ax1.twinx()
plt.xticks(rotation=15) ax2.bar(labels, turnover, width=0.4, color=color)
ax2.set_ylabel(_("Turnover by product"), color=color)
plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter("%.2f")) plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter("%.2f"))
fig.tight_layout()
image_data = StringIO() image_data = StringIO()
plt.title(_("Turnover"))
fig.savefig(image_data, format="svg") fig.savefig(image_data, format="svg")
image_data.seek(0) image_data.seek(0)
return image_data.getvalue() return image_data.getvalue()