From 1bb5b600c9d5a49a42bebb74d4b1422a2c1ae49e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Tue, 15 Mar 2022 20:28:06 +0100
Subject: [PATCH] Add country-level financial data page

---
 .../finances/_hx_country_level_data.html      | 63 +++++++++++++++++++
 .../finances/country_level_data.html          | 44 +++++++++++++
 scipost_django/finances/urls.py               | 10 +++
 scipost_django/finances/views.py              | 45 +++++++++++++
 .../organizations/organization_list.html      |  6 +-
 5 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 scipost_django/finances/templates/finances/_hx_country_level_data.html
 create mode 100644 scipost_django/finances/templates/finances/country_level_data.html

diff --git a/scipost_django/finances/templates/finances/_hx_country_level_data.html b/scipost_django/finances/templates/finances/_hx_country_level_data.html
new file mode 100644
index 000000000..6351b1b67
--- /dev/null
+++ b/scipost_django/finances/templates/finances/_hx_country_level_data.html
@@ -0,0 +1,63 @@
+{% load countries %}
+{% get_country country as country_obj %}
+
+<h2 class="highlight">Summed over all Organizations in {{ country_obj.name }}&emsp;<i class="{{ country_obj.flag_css }}"></i></h2>
+
+<table class="table">
+  <thead>
+    <tr>
+      <th></th>
+      <th style="text-align: right;">Contribution</th>
+      <th style="text-align: right;">Expenditures</th>
+      <th style="text-align: right;">Balance</th>
+    </tr>
+  </thead>
+  <tbody>
+    <tr class="bg-light">
+      <td>Cumulative</td>
+      <td style="text-align: right;">{{ cumulative.contribution }}</td>
+      <td style="text-align: right;">{{ cumulative.expenditures }}</td>
+      <td style="text-align: right;">{{ cumulative.balance }}</td>
+    </tr>
+    {% for year, val in per_year.items %}
+      <tr>
+	<td>{{ year }}</td>
+	<td style="text-align: right;">{{ val.contribution }}</td>
+	<td style="text-align: right;">{{ val.expenditures }}</td>
+	<td style="text-align: right;">{{ val.balance }}</td>
+      </tr>
+    {% endfor %}
+  </tbody>
+</table>
+
+
+<h2 class="highlight mt-5">Cumulative, per Organization</h2>
+
+<table class="table">
+  <thead>
+    <tr>
+      <th></th>
+      <th style="text-align: right;">Contribution</th>
+      <th style="text-align: right;">Expenditures</th>
+      <th style="text-align: right;">Balance</th>
+    </tr>
+  </thead>
+  <tbody>
+    <tr class="bg-light">
+      <td>Total</td>
+      <td style="text-align: right;">{{ cumulative.contribution }}</td>
+      <td style="text-align: right;">{{ cumulative.expenditures }}</td>
+      <td style="text-align: right;">{{ cumulative.balance }}</td>
+    </tr>
+    {% for organization in organizations %}
+      {% if organization.cf_balance_info.cumulative.contribution > 0 or organization.cf_balance_info.cumulative.expenditures > 0 %}
+	<tr>
+	  <td><a href="https://{{ request.get_host }}{{ organization.get_absolute_url }}">{{ organization }}</a></td>
+	  <td style="text-align: right;">{{ organization.cf_balance_info.cumulative.contribution }}</td>
+	  <td style="text-align: right;">{{ organization.cf_balance_info.cumulative.expenditures }}</td>
+	  <td style="text-align: right;">{{ organization.cf_balance_info.cumulative.balance }}</td>
+	</tr>
+      {% endif %}
+    {% endfor %}
+  </tbody>
+</table>
diff --git a/scipost_django/finances/templates/finances/country_level_data.html b/scipost_django/finances/templates/finances/country_level_data.html
new file mode 100644
index 000000000..5bde18935
--- /dev/null
+++ b/scipost_django/finances/templates/finances/country_level_data.html
@@ -0,0 +1,44 @@
+{% extends 'finances/base.html' %}
+
+{% load countries %}
+{% load static %}
+
+{% block meta_description %}{{ block.super }} Country balance info{% endblock meta_description %}
+{% block pagetitle %}: Country balance info{% endblock pagetitle %}
+
+{% block headsup %}
+  <link rel="stylesheet" href="{% static 'flags/sprite-hq.css' %}">
+{% endblock headsup %}
+
+{% block content %}
+  <div class="row">
+    <div class="col-12">
+      <h2>Country-Level Financial Data</h2>
+
+      <div class="row mt-4">
+	<div class="col-lg-3">
+	  <h3>Click on flag to view that country's data</h3>
+	</div>
+	<div class="col-lg-8">
+	  <ul>
+	    {% for code in countrycodes %}
+	      {% get_country code as country_obj %}
+	      <li style="display: inline-block;" class="m-1">
+		<a hx-get="{% url 'finances:_hx_country_level_data' country=code %}"
+		   hx-target="#country_data"
+		>
+		  <i class="{{ country_obj.flag_css }}"
+		     data-bs-toggle="tooltip"
+		     title="{{ country_obj.name }}"></i>
+		</a>
+	      </li>
+	    {% endfor %}
+	  </ul>
+	</div>
+      </div>
+
+      <div id="country_data" class="mt-4"></div>
+
+    </div>
+  </div>
+{% endblock content %}
diff --git a/scipost_django/finances/urls.py b/scipost_django/finances/urls.py
index 876e1fc37..6acbb508c 100644
--- a/scipost_django/finances/urls.py
+++ b/scipost_django/finances/urls.py
@@ -17,6 +17,16 @@ urlpatterns = [
         name="business_model",
     ),
     path("apex", views.apex, name="apex"),
+    path(
+        "country_level_data",
+        views.country_level_data,
+        name="country_level_data",
+    ),
+    path(
+        "_hx_country_level_data/<slug:country>",
+        views._hx_country_level_data,
+        name="_hx_country_level_data",
+    ),
     # Subsidies
     path("subsidies/", views.SubsidyListView.as_view(), name="subsidies"),
     path("subsidies/add/", views.SubsidyCreateView.as_view(), name="subsidy_create"),
diff --git a/scipost_django/finances/views.py b/scipost_django/finances/views.py
index ec1268ff1..a53f747f1 100644
--- a/scipost_django/finances/views.py
+++ b/scipost_django/finances/views.py
@@ -167,6 +167,51 @@ def apex(request):
     return render(request, "finances/apex.html", context)
 
 
+def country_level_data(request):
+    context = {}
+    context["countrycodes"] = [
+        code["country"]
+        for code in list(
+                Organization.objects.all().distinct("country").values("country")
+        )
+    ]
+    return render(request, "finances/country_level_data.html", context)
+
+
+def _hx_country_level_data(request, country):
+    organizations = Organization.objects.filter(country=country)
+    pubyears = [str(y) for y in range(int(timezone.now().strftime("%Y")), 2015, -1)]
+    context = {
+        "country": country,
+        "organizations": organizations,
+        "cumulative": {"contribution": 0, "expenditures": 0, "balance": 0},
+        "per_year": {}
+    }
+    for year in pubyears:
+        context["per_year"][year] = {
+            "contribution": 0,
+            "expenditures": 0,
+            "balance": 0,
+        }
+    cumulative_expenditures = 0
+    for organization in organizations.all():
+        for key in ("contribution", "expenditures", "balance"):
+            context["cumulative"][
+                key
+            ] += organization.cf_balance_info["cumulative"][key]
+        for year in pubyears:
+            context["per_year"][year]["contribution"] += (
+                organization.cf_balance_info[year]["contribution"]
+            )
+            context["per_year"][year]["expenditures"] += (
+                organization.cf_balance_info[year]["expenditures"]["total"]
+            )
+            context["per_year"][year]["balance"] += (
+                organization.cf_balance_info[year]["balance"]
+            )
+    return render(request, "finances/_hx_country_level_data.html", context)
+
+
 #############
 # Subsidies #
 #############
diff --git a/scipost_django/organizations/templates/organizations/organization_list.html b/scipost_django/organizations/templates/organizations/organization_list.html
index b4cf7bc62..300d5c17b 100644
--- a/scipost_django/organizations/templates/organizations/organization_list.html
+++ b/scipost_django/organizations/templates/organizations/organization_list.html
@@ -92,7 +92,11 @@
 	{% for code in countrycodes %}
 	  {% get_country code as country_obj %}
 	  <li style="display: inline-block;">
-	    <a href="{% add_get_parameters country=code %}"><i class="{{ country_obj.flag_css }}" data-bs-toggle="tooltip" title="{{ country_obj.name }}"></i></a>
+	    <a href="{% add_get_parameters country=code %}">
+	      <i class="{{ country_obj.flag_css }}"
+		 data-bs-toggle="tooltip"
+		 title="{{ country_obj.name }}"></i>
+	    </a>
 	  </li>
 	{% endfor %}
       </ul>
-- 
GitLab