From 9d50dc715449120cfa7a69a9c7833c96a8011977 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Thu, 4 Jan 2024 16:51:48 +0100
Subject: [PATCH] Separate detail and edit view for category, update statement
 page

---
 .../locale/fr_FR/LC_MESSAGES/django.po        |  10 ++--
 .../templates/account/account_detail.html     |   1 -
 nummi/account/views.py                        |  25 ---------
 .../locale/fr_FR/LC_MESSAGES/django.mo        | Bin 899 -> 968 bytes
 .../locale/fr_FR/LC_MESSAGES/django.po        |  22 ++++----
 .../templates/category/category_detail.html   |  24 +++++++++
 .../templates/category/category_form.html     |  12 -----
 .../templates/category/category_plot.html     |  48 +++++++++++++-----
 nummi/category/templatetags/category.py       |  18 +++++--
 nummi/category/urls.py                        |   3 +-
 nummi/category/views.py                       |  14 ++++-
 nummi/main/locale/fr_FR/LC_MESSAGES/django.po |  14 ++---
 nummi/main/static/main/css/plot.css           |   2 +-
 nummi/main/templatetags/main_extras.py        |  14 ++---
 .../templates/statement/statement_form.html   |   9 ++--
 15 files changed, 120 insertions(+), 96 deletions(-)
 create mode 100644 nummi/category/templates/category/category_detail.html

diff --git a/nummi/account/locale/fr_FR/LC_MESSAGES/django.po b/nummi/account/locale/fr_FR/LC_MESSAGES/django.po
index e0baa15..14f7e1a 100644
--- a/nummi/account/locale/fr_FR/LC_MESSAGES/django.po
+++ b/nummi/account/locale/fr_FR/LC_MESSAGES/django.po
@@ -7,7 +7,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: \n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2024-01-04 16:04+0100\n"
+"POT-Creation-Date: 2024-01-04 16:18+0100\n"
 "PO-Revision-Date: 2023-04-22 15:17+0200\n"
 "Last-Translator: Edgar P. Burkhart <traduction@edgarpierre.fr>\n"
 "Language-Team: \n"
@@ -37,19 +37,19 @@ msgstr "Défaut"
 msgid "Accounts"
 msgstr "Comptes"
 
-#: .\account\templates\account\account_detail.html:15
+#: .\account\templates\account\account_detail.html:13
 msgid "Edit account"
 msgstr "Modifier le compte"
 
-#: .\account\templates\account\account_detail.html:17
+#: .\account\templates\account\account_detail.html:16
 msgid "Statements"
 msgstr "Relevés"
 
-#: .\account\templates\account\account_detail.html:21
+#: .\account\templates\account\account_detail.html:20
 msgid "Transactions"
 msgstr "Transactions"
 
-#: .\account\templates\account\account_detail.html:26
+#: .\account\templates\account\account_detail.html:25
 msgid "History"
 msgstr "Historique"
 
diff --git a/nummi/account/templates/account/account_detail.html b/nummi/account/templates/account/account_detail.html
index 419669e..92c3c7f 100644
--- a/nummi/account/templates/account/account_detail.html
+++ b/nummi/account/templates/account/account_detail.html
@@ -4,7 +4,6 @@
 {% block title %}{{ object }} – {{ block.super }}{% endblock %}
 {% block link %}
     {{ block.super }}
-    {% css "main/css/form.css" %}
     {% css "main/css/table.css" %}
     {% css "main/css/plot.css" %}
 {% endblock %}
diff --git a/nummi/account/views.py b/nummi/account/views.py
index a07bd8b..902db10 100644
--- a/nummi/account/views.py
+++ b/nummi/account/views.py
@@ -24,31 +24,6 @@ class AccountUpdateView(NummiUpdateView):
     form_class = AccountForm
     pk_url_kwarg = "account"
 
-    def get_context_data(self, **kwargs):
-        _max = 8
-        data = super().get_context_data(**kwargs)
-        account = data["form"].instance
-
-        _transactions = account.transaction_set.all()
-        if _transactions.count() > _max:
-            data["transactions_url"] = reverse_lazy(
-                "account_transactions", args=(account.pk,)
-            )
-        _statements = account.statement_set.all()
-        if _statements.count() > _max:
-            data["statements_url"] = reverse_lazy(
-                "account_statements", args=(account.pk,)
-            )
-
-        return data | {
-            "transactions": _transactions[:8],
-            "new_statement_url": reverse_lazy(
-                "new_statement", kwargs={"account": account.pk}
-            ),
-            "statements": _statements[:8],
-            "history": history(account.transaction_set),
-        }
-
 
 class AccountDeleteView(NummiDeleteView):
     model = Account
diff --git a/nummi/category/locale/fr_FR/LC_MESSAGES/django.mo b/nummi/category/locale/fr_FR/LC_MESSAGES/django.mo
index 32338f9c0ed8d8e5f6168003298a21f3684d5ae9..53d58b5e6341c6055989605e6b469824c309374e 100644
GIT binary patch
diff --git a/nummi/category/locale/fr_FR/LC_MESSAGES/django.po b/nummi/category/locale/fr_FR/LC_MESSAGES/django.po
index d59e917..d66c030 100644
--- a/nummi/category/locale/fr_FR/LC_MESSAGES/django.po
+++ b/nummi/category/locale/fr_FR/LC_MESSAGES/django.po
@@ -7,7 +7,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: \n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2024-01-02 15:52+0100\n"
+"POT-Creation-Date: 2024-01-04 16:18+0100\n"
 "PO-Revision-Date: 2023-04-22 15:18+0200\n"
 "Last-Translator: Edgar P. Burkhart <traduction@edgarpierre.fr>\n"
 "Language-Team: \n"
@@ -38,6 +38,18 @@ msgstr "Budget"
 msgid "Categories"
 msgstr "Catégories"
 
+#: .\category\templates\category\category_detail.html:12
+msgid "Edit category"
+msgstr "Modifier la catégorie"
+
+#: .\category\templates\category\category_detail.html:15
+msgid "Transactions"
+msgstr "Transactions"
+
+#: .\category\templates\category\category_detail.html:20
+msgid "History"
+msgstr "Historique"
+
 #: .\category\templates\category\category_form.html:5
 msgid "Create category"
 msgstr "Créer une catégorie"
@@ -46,14 +58,6 @@ msgstr "Créer une catégorie"
 msgid "New category"
 msgstr "Nouvelle catégorie"
 
-#: .\category\templates\category\category_form.html:13
-msgid "Transactions"
-msgstr "Transactions"
-
-#: .\category\templates\category\category_form.html:18
-msgid "History"
-msgstr "Historique"
-
 #: .\category\templates\category\category_plot.html:15
 msgid "Expenses"
 msgstr "Dépenses"
diff --git a/nummi/category/templates/category/category_detail.html b/nummi/category/templates/category/category_detail.html
new file mode 100644
index 0000000..b0ea18f
--- /dev/null
+++ b/nummi/category/templates/category/category_detail.html
@@ -0,0 +1,24 @@
+{% extends "main/base.html" %}
+{% load main_extras i18n %}
+{% block title %}{{ object }} – {{ block.super }}{% endblock %}
+{% block link %}
+    {{ block.super }}
+    {% css "main/css/table.css" %}
+    {% css "main/css/plot.css" %}
+{% endblock %}
+{% block body %}
+    <h2>{{ object.icon|remix }}{{ object }}</h2>
+    <p>
+        <a href="{% url "edit_category" object.pk %}">{% translate "Edit category" %}</a>
+    </p>
+    <section>
+        <h3>{% translate "Transactions" %}</h3>
+        {% include "transaction/transaction_table.html" %}
+    </section>
+    {% if history %}
+        <section>
+            <h3>{% translate "History" %}</h3>
+            {% include "history/plot.html" %}
+        </section>
+    {% endif %}
+{% endblock %}
diff --git a/nummi/category/templates/category/category_form.html b/nummi/category/templates/category/category_form.html
index f4aaa6d..7e2748e 100644
--- a/nummi/category/templates/category/category_form.html
+++ b/nummi/category/templates/category/category_form.html
@@ -8,15 +8,3 @@
     {% translate "New category" %}
 {% endblock %}
 {% block h2 %}{{ form.instance.icon|remix }}{{ form.instance }}{% endblock %}
-{% block tables %}
-    <section>
-        <h3>{% translate "Transactions" %}</h3>
-        {% include "transaction/transaction_table.html" %}
-    </section>
-    {% if history %}
-        <section>
-            <h3>{% translate "History" %}</h3>
-            {% include "history/plot.html" %}
-        </section>
-    {% endif %}
-{% endblock %}
diff --git a/nummi/category/templates/category/category_plot.html b/nummi/category/templates/category/category_plot.html
index 63577af..c152c10 100644
--- a/nummi/category/templates/category/category_plot.html
+++ b/nummi/category/templates/category/category_plot.html
@@ -1,4 +1,4 @@
-{% load main_extras %}
+{% load main_extras category %}
 {% load i18n %}
 <div class="plot">
     <table class="full-width">
@@ -34,14 +34,14 @@
                         <td class="c">
                             {% if cat.category %}{{ cat.category__icon|remix }}{% endif %}
                         </td>
-                        <td class="value">{{ cat.sum_m|pmrvalue }}</td>
+                        <td class="value">{{ cat.sum_m|pmvalue }}</td>
                         <td class="bar m">
                             {% if cat.sum_m %}
                                 <div style="width: {% widthratio cat.sum_m max -100 %}%"></div>
                             {% endif %}
                             {% if cat.sum < 0 %}
                                 <div class="tot" style="width:{% widthratio cat.sum max -100 %}%">
-                                    <span>{{ cat.sum|pmrvalue }}</span>
+                                    <span>{{ cat.sum|pmvalue }}</span>
                                 </div>
                             {% endif %}
                         </td>
@@ -51,11 +51,11 @@
                             {% endif %}
                             {% if cat.sum > 0 %}
                                 <div class="tot" style="width:{% widthratio cat.sum max 100 %}%">
-                                    <span>{{ cat.sum|pmrvalue }}</span>
+                                    <span>{{ cat.sum|pmvalue }}</span>
                                 </div>
                             {% endif %}
                         </td>
-                        <td class="value">{{ cat.sum_p|pmrvalue }}</td>
+                        <td class="value">{{ cat.sum_p|pmvalue }}</td>
                     </tr>
                 {% empty %}
                     <tr>
@@ -64,16 +64,16 @@
                 {% endfor %}
             {% endspaceless %}
         </tbody>
-        {% if categories %}
-            <tfoot>
+        <tfoot>
+            {% if categories %}
                 <tr>
                     <th scope="row" colspan="2" class="l">{% translate "Total" %}</th>
-                    <td class="value">{{ total_m|pmrvalue }}</td>
+                    <td class="value">{{ total_m|pmvalue }}</td>
                     <td class="bar m">
                         <div style="width: {% widthratio total_m max -100 %}%"></div>
                         {% if total < 0 %}
                             <div class="tot" style="width:{% widthratio total max -100 %}%">
-                                <span>{{ total|pmrvalue }}</span>
+                                <span>{{ total|pmvalue }}</span>
                             </div>
                         {% endif %}
                     </td>
@@ -81,13 +81,35 @@
                         <div style="width: {% widthratio total_p max 100 %}%"></div>
                         {% if total > 0 %}
                             <div class="tot" style="width:{% widthratio total max 100 %}%">
-                                <span>{{ total|pmrvalue }}</span>
+                                <span>{{ total|pmvalue }}</span>
                             </div>
                         {% endif %}
                     </td>
-                    <td class="value">{{ total_p|pmrvalue }}</td>
+                    <td class="value">{{ total_p|pmvalue }}</td>
                 </tr>
-            </tfoot>
-        {% endif %}
+            {% endif %}
+            {% if statement %}
+                <tr>
+                    <th scope="row" class="l">{% translate "Expected total" %}</th>
+                    <td class="c">{{ total|check:statement.diff }}</td>
+                    <td></td>
+                    <td class="bar m">
+                        {% if statement.diff < 0 %}
+                            <div class="tot" style="width:{% widthratio statement.diff max -100 %}%">
+                                <span>{{ statement.diff|pmvalue }}</span>
+                            </div>
+                        {% endif %}
+                    </td>
+                    <td class="bar p">
+                        {% if statement.diff >= 0 %}
+                            <div class="tot" style="width:{% widthratio statement.diff max 100 %}%">
+                                <span>{{ statement.diff|pmvalue }}</span>
+                            </div>
+                        {% endif %}
+                    </td>
+                    <td></td>
+                </tr>
+            {% endif %}
+        </tfoot>
     </table>
 </div>
diff --git a/nummi/category/templatetags/category.py b/nummi/category/templatetags/category.py
index 5f76341..aaf3bf9 100644
--- a/nummi/category/templatetags/category.py
+++ b/nummi/category/templatetags/category.py
@@ -1,15 +1,17 @@
 from django import template
 from django.db import models
 from django.db.models.functions import Greatest
+from main.templatetags.main_extras import remix
 
 register = template.Library()
 
 
 @register.inclusion_tag("category/category_plot.html")
-def category_plot(transactions, **kwargs):
+def category_plot(transactions, budget=True, **kwargs):
+    if budget:
+        transactions = transactions.exclude(category__budget=False)
     categories = (
-        transactions.exclude(category__budget=False)
-        .values("category", "category__name", "category__icon")
+        transactions.values("category", "category__name", "category__icon")
         .annotate(
             sum=models.Sum("value"),
             sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
@@ -29,3 +31,13 @@ def category_plot(transactions, **kwargs):
             total=models.Sum("sum"),
         )
     )
+
+
+@register.filter
+def check(s, diff):
+    if s is None:
+        s = 0
+    if s == diff:
+        return remix("check", "green")
+    else:
+        return remix("close", "red")
diff --git a/nummi/category/urls.py b/nummi/category/urls.py
index 64b8ae9..9936065 100644
--- a/nummi/category/urls.py
+++ b/nummi/category/urls.py
@@ -5,7 +5,8 @@ from . import views
 
 urlpatterns = [
     path("new", views.CategoryCreateView.as_view(), name="new_category"),
-    path("<category>", views.CategoryUpdateView.as_view(), name="category"),
+    path("<category>", views.CategoryDetailView.as_view(), name="category"),
+    path("<category>/edit", views.CategoryUpdateView.as_view(), name="edit_category"),
     path(
         "<category>/transactions",
         views.CategoryTListView.as_view(),
diff --git a/nummi/category/views.py b/nummi/category/views.py
index 36fe0d0..f01d908 100644
--- a/nummi/category/views.py
+++ b/nummi/category/views.py
@@ -1,7 +1,12 @@
 from django.shortcuts import get_object_or_404
 from django.urls import reverse_lazy
 from history.utils import history
-from main.views import NummiCreateView, NummiDeleteView, NummiUpdateView
+from main.views import (
+    NummiCreateView,
+    NummiDeleteView,
+    NummiDetailView,
+    NummiUpdateView,
+)
 from transaction.views import TransactionListView
 
 from .forms import CategoryForm
@@ -18,10 +23,15 @@ class CategoryUpdateView(NummiUpdateView):
     form_class = CategoryForm
     pk_url_kwarg = "category"
 
+
+class CategoryDetailView(NummiDetailView):
+    model = Category
+    pk_url_kwarg = "category"
+
     def get_context_data(self, **kwargs):
         _max = 8
         data = super().get_context_data(**kwargs)
-        category = data["form"].instance
+        category = data["object"]
 
         data["transactions"] = category.transaction_set.all()[:_max]
         if len(data["transactions"]) == _max:
diff --git a/nummi/main/locale/fr_FR/LC_MESSAGES/django.po b/nummi/main/locale/fr_FR/LC_MESSAGES/django.po
index fbf05c3..4ff140a 100644
--- a/nummi/main/locale/fr_FR/LC_MESSAGES/django.po
+++ b/nummi/main/locale/fr_FR/LC_MESSAGES/django.po
@@ -7,7 +7,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: \n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2024-01-04 16:04+0100\n"
+"POT-Creation-Date: 2024-01-04 16:18+0100\n"
 "PO-Revision-Date: 2023-04-23 08:03+0200\n"
 "Last-Translator: Edgar P. Burkhart <traduction@edgarpierre.fr>\n"
 "Language-Team: \n"
@@ -101,27 +101,27 @@ msgstr "Compte"
 msgid "Balance"
 msgstr "Solde"
 
-#: .\main\templates\main\index.html:19 .\main\templates\main\index.html:30
+#: .\main\templates\main\index.html:19 .\main\templates\main\index.html:31
 msgid "Edit"
 msgstr "Modifier"
 
-#: .\main\templates\main\index.html:34
+#: .\main\templates\main\index.html:36
 msgid "No account"
 msgstr "Aucun compte"
 
-#: .\main\templates\main\index.html:41
+#: .\main\templates\main\index.html:43
 msgid "Create account"
 msgstr "Créer un compte"
 
-#: .\main\templates\main\index.html:48
+#: .\main\templates\main\index.html:50
 msgid "Categories"
 msgstr "Catégories"
 
-#: .\main\templates\main\index.html:54
+#: .\main\templates\main\index.html:56
 msgid "Create category"
 msgstr "Créer une catégorie"
 
-#: .\main\templates\main\index.html:61
+#: .\main\templates\main\index.html:63
 msgid "History"
 msgstr "Historique"
 
diff --git a/nummi/main/static/main/css/plot.css b/nummi/main/static/main/css/plot.css
index 9a2bc7a..4a9eb36 100644
--- a/nummi/main/static/main/css/plot.css
+++ b/nummi/main/static/main/css/plot.css
@@ -8,9 +8,9 @@ table.full-width col.bar {
   td.bar {
     position: relative;
     padding: 0;
+    overflow: hidden;
     @media (width < 720px) {
       width: 0;
-      overflow: hidden;
     }
 
     div {
diff --git a/nummi/main/templatetags/main_extras.py b/nummi/main/templatetags/main_extras.py
index a85d8db..fd29a10 100644
--- a/nummi/main/templatetags/main_extras.py
+++ b/nummi/main/templatetags/main_extras.py
@@ -8,16 +8,16 @@ register = template.Library()
 
 @register.filter
 def value(val, pm=False, r=2):
-    if not val:
+    if val is None:
         return ""
     _prefix = ""
     _suffix = "&nbsp;€"
-    _val = formats.number_format(round(val, r), r, use_l10n=True, force_grouping=True)
+    _val = formats.number_format(val, decimal_pos=r, use_l10n=True, force_grouping=True)
 
     if val > 0:
         if pm:
             _prefix += "&plus;&nbsp;"
-    else:
+    elif val < 0:
         _val = _val[1:]
         _prefix += "&minus;&nbsp;"
 
@@ -39,14 +39,6 @@ def remix(icon, cls=""):
     return mark_safe(f"""<span class="ri-{icon}-line {cls}"></span>""")
 
 
-@register.filter
-def check(sum, diff):
-    if sum == diff:
-        return remix("check", "green")
-    else:
-        return remix("close", "red")
-
-
 @register.filter
 def extension(file):
     return file.name.split(".")[-1].upper()
diff --git a/nummi/statement/templates/statement/statement_form.html b/nummi/statement/templates/statement/statement_form.html
index b09e993..f5e04c8 100644
--- a/nummi/statement/templates/statement/statement_form.html
+++ b/nummi/statement/templates/statement/statement_form.html
@@ -6,10 +6,7 @@
 {% block h2_new %}
     {% translate "New statement" %}
 {% endblock %}
-{% block h2 %}
-    {{ form.instance.sum|check:form.instance.diff }}
-    {{ form.instance }}
-{% endblock %}
+{% block h2 %}{{ form.instance }}{% endblock %}
 {% block pre %}
     {% if account %}
         <p>
@@ -21,10 +18,10 @@
     {% if not form.instance|adding %}
         <section>
             <h3>{% translate "Categories" %}</h3>
-            {% category_plot transactions %}
+            {% category_plot transactions budget=False statement=object %}
         </section>
         <section>
-            <h3>{% translate "Transactions" %} ({{ form.instance.sum|pmvalue }} / {{ form.instance.diff|pmvalue }})</h3>
+            <h3>{% translate "Transactions" %}</h3>
             {% include "transaction/transaction_table.html" %}
         </section>
     {% endif %}
