Compare commits

...

2 commits

7 changed files with 23 additions and 26 deletions

View file

@ -18,7 +18,7 @@
</thead>
<tbody>
{% spaceless %}
{% for cat in categories.data %}
{% for cat in categories %}
<tr>
<th scope="row" class="l">
{% if cat.category %}
@ -34,25 +34,27 @@
</td>
<td class="value">{{ cat.sum_m|pmrvalue }}</td>
<td class="bar m">
<div style="width: {% widthratio cat.sum_m categories.max -100 %}%"></div>
<div style="width: {% widthratio cat.sum_m max -100 %}%"></div>
{% if cat.sum < 0 %}
<div class="tot"
style="width:{% widthratio cat.sum categories.max -100 %}%">
<div class="tot" style="width:{% widthratio cat.sum max -100 %}%">
<span>{{ cat.sum|pmrvalue }}</span>
</div>
{% endif %}
</td>
<td class="bar p">
<div style="width: {% widthratio cat.sum_p categories.max 100 %}%"></div>
<div style="width: {% widthratio cat.sum_p max 100 %}%"></div>
{% if cat.sum > 0 %}
<div class="tot"
style="width:{% widthratio cat.sum categories.max 100 %}%">
<div class="tot" style="width:{% widthratio cat.sum max 100 %}%">
<span>{{ cat.sum|pmrvalue }}</span>
</div>
{% endif %}
</td>
<td class="value">{{ cat.sum_p|pmrvalue }}</td>
</tr>
{% empty %}
<tr>
<td class="empty" colspan="6">{% translate "No transaction" %}</td>
</tr>
{% endfor %}
{% endspaceless %}
</tbody>

View file

View file

@ -1,9 +1,14 @@
from django import template
from django.db import models
register = template.Library()
def get_categories(transactions):
@register.inclusion_tag("category/category_plot.html")
def category_plot(transactions):
categories = (
transactions.values("category", "category__name", "category__icon")
transactions.filter(category__budget=True)
.values("category", "category__name", "category__icon")
.annotate(
sum=models.Sum("value"),
sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
@ -12,7 +17,7 @@ def get_categories(transactions):
.order_by("-sum")
)
return {
"data": categories,
"categories": categories,
"max": max(
categories.aggregate(
max=models.Max("sum_p", default=0),

View file

@ -1,6 +1,5 @@
{% extends "main/form/base.html" %}
{% load main_extras %}
{% load i18n %}
{% load i18n main_extras category %}
{% block title_new %}
{% translate "Create statement" %}
{% endblock %}
@ -19,11 +18,9 @@
{% endif %}
{% endblock %}
{% block tables %}
{% if categories %}
<h3>{% translate "Categories" %}</h3>
{% include "category/category_plot.html" %}
{% endif %}
{% if not form.instance|adding %}
<h3>{% translate "Categories" %}</h3>
{% category_plot transactions %}
<h3>{% translate "Transactions" %} ({{ form.instance.sum|pmvalue }} / {{ form.instance.diff|pmvalue }})</h3>
{% include "transaction/transaction_table.html" %}
{% endif %}

View file

@ -1,5 +1,4 @@
from account.models import Account
from category.utils import get_categories
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy
from main.views import NummiCreateView, NummiDeleteView, NummiListView, NummiUpdateView
@ -42,8 +41,6 @@ class StatementUpdateView(NummiUpdateView):
statement = data["form"].instance
_transactions = statement.transaction_set.all()
if _transactions:
data["categories"] = get_categories(_transactions)
return data | {
"account": statement.account,

View file

@ -1,6 +1,5 @@
{% extends "transaction/transaction_list.html" %}
{% load i18n %}
{% load static %}
{% load i18n static category %}
{% block link %}
{{ block.super }}
<link rel="stylesheet"
@ -11,8 +10,8 @@
{% block table %}
<h3>{% translate "Transactions" %}</h3>
{{ block.super }}
{% if categories %}
{% if not category %}
<h3>{% translate "Categories" %}</h3>
{% include "category/category_plot.html" %}
{% category_plot transactions %}
{% endif %}
{% endblock %}

View file

@ -1,6 +1,5 @@
from account.models import Account
from category.models import Category
from category.utils import get_categories
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy
from django.views.generic.dates import MonthArchiveView
@ -136,8 +135,6 @@ class TransactionMonthView(UserMixin, MonthArchiveView):
context_data = super().get_context_data(**kwargs)
if "category" in self.kwargs:
return context_data | {"category": self.category}
context_data["categories"] = get_categories(context_data["transactions"])
if "account" in self.kwargs:
return context_data | {"account": self.account}
return context_data