From a57dd910f8131268fe5d3f9f7c4b61cd32ba82b1 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Wed, 9 Oct 2024 10:55:56 +0200
Subject: [PATCH] improve subsidy list and payments dropdown

---
 scipost_django/finances/forms.py              |   2 +-
 .../_hx_subsidy_finadmin_details.html         | 141 +++++++++---------
 .../templates/finances/_hx_subsidy_list.html  | 131 +++++++++-------
 .../templates/finances/subsidy_list.html      |   7 +-
 4 files changed, 155 insertions(+), 126 deletions(-)

diff --git a/scipost_django/finances/forms.py b/scipost_django/finances/forms.py
index c29d19449..c848c732e 100644
--- a/scipost_django/finances/forms.py
+++ b/scipost_django/finances/forms.py
@@ -219,7 +219,7 @@ class SubsidySearchForm(forms.Form):
                 ]
             )
 
-        return subsidies
+        return subsidies.distinct()
 
 
 class SubsidyPaymentForm(forms.ModelForm):
diff --git a/scipost_django/finances/templates/finances/_hx_subsidy_finadmin_details.html b/scipost_django/finances/templates/finances/_hx_subsidy_finadmin_details.html
index 52e2195c6..0657ad68e 100644
--- a/scipost_django/finances/templates/finances/_hx_subsidy_finadmin_details.html
+++ b/scipost_django/finances/templates/finances/_hx_subsidy_finadmin_details.html
@@ -1,82 +1,79 @@
-<details class="bg-danger bg-opacity-10">
-  <summary class="bg-danger bg-opacity-10 p-2">
-    Financial Administration
-    {% if subsidy.payments_all_scheduled %}
-      <span class="bg-success text-white ms-4 p-1">[all payments are scheduled]</span>
-    {% else %}
-      <span class="bg-warning text-white ms-4 p-1">[payment schedule to be completed]</span>
-    {% endif %}
-  </summary>
-  <div class="p-2">
-    <h3>Payment schedule</h3>
+<div class="subsidy-payment-table-container bg-danger bg-opacity-10">
+  <div hx-get="{% url "common:empty" %}"
+       hx-target="closest .subsidy-payment-table-container"
+       class="d-flex align-items-center p-2 pe-auto bg-danger bg-opacity-25">
+    <h3 class="mb-0">Payment schedule</h3>
+    <span class="ms-auto text-muted">(click to close)</span>
+  </div>
+  <div class="px-2 pb-2">
     <table class="table table-bordered">
       <thead>
-	<tr>
-	  <th>Reference</th>
-	  <th>Amount</th>
-	  <th>Date&nbsp;scheduled</th>
-	  <th>Invoice</th>
-	  <th>Proof&nbsp;of&nbsp;payment</th>
-	  <th>Actions</th>
-	</tr>
+        <tr>
+          <th>Reference</th>
+          <th>Amount</th>
+          <th>Date&nbsp;scheduled</th>
+          <th>Invoice</th>
+          <th>Proof&nbsp;of&nbsp;payment</th>
+          <th>Actions</th>
+        </tr>
       </thead>
       <tbody>
-	{% for payment in subsidy.payments.all %}
-	  <tr>
-	    <td>{{ payment.reference }}</td>
-	    <td>{{ payment.amount }}</td>
-	    <td>{{ payment.date_scheduled }}</td>
-	    <td>
-	      {% if payment.invoice %}
-		<span class="text-success">
-		  {% include "bi/check-square-fill.html" %}
-		</span>
-		<a href="{{ payment.invoice.get_absolute_url }}">
-		  {% include "bi/file-earmark-arrow-down.html" %}
-		</a>
-	      {% else %}
-		<span class="text-danger">{% include "bi/x-square-fill.html" %}</span>
-	      {% endif %}
-	    </td>
-	    <td>
-	      {% if payment.proof_of_payment %}
-		<span class="text-success">
-		  {% include "bi/check-square-fill.html" %}
-		</span>
-		<a href="{{ payment.proof_of_payment.get_absolute_url }}">
-		  {% include "bi/file-earmark-arrow-down.html" %}
-		</a>
-	      {% else %}
-		<span class="text-danger">{% include "bi/x-square-fill.html" %}</span>
-	      {% endif %}
-	    </td>
-	    <td>
-	      <a class="btn btn-sm btn-warning text-white px-1 py-0"
-		 hx-get="{% url 'finances:_hx_subsidypayment_form' subsidy_id=subsidy.id subsidypayment_id=payment.id %}"
-		 hx-target="#subsidy-{{ subsidy.id }}-payment-form"
-	      >
-		{% include "bi/pencil-square.html" %}
-	      </a>
-	      <a class="btn btn-sm btn-danger ms-2 px-1 py-0"
-		 hx-get="{% url 'finances:_hx_subsidypayment_delete' subsidy_id=subsidy.id subsidypayment_id=payment.id %}"
-		 hx-target="#subsidy-{{ subsidy.id }}-finadmin-details"
-		 hx-confirm="Are you sure you want to delete this payment information?"
-	      >
-		{% include "bi/trash-fill.html" %}
-	      </a>
-	    </td>
-	  </tr>
-	{% empty %}
-	  <tr><td colspan="5">No payment has been scheduled</td></tr>
-	{% endfor %}
+
+        {% for payment in subsidy.payments.all %}
+          <tr>
+            <td>{{ payment.reference }}</td>
+            <td>{{ payment.amount }}</td>
+            <td>{{ payment.date_scheduled }}</td>
+            <td>
+
+              {% if payment.invoice %}
+                <span class="text-success">{% include "bi/check-square-fill.html" %}</span>
+                <a href="{{ payment.invoice.get_absolute_url }}">
+                  {% include "bi/file-earmark-arrow-down.html" %}
+                </a>
+              {% else %}
+                <span class="text-danger">{% include "bi/x-square-fill.html" %}</span>
+              {% endif %}
+
+            </td>
+            <td>
+
+              {% if payment.proof_of_payment %}
+                <span class="text-success">{% include "bi/check-square-fill.html" %}</span>
+                <a href="{{ payment.proof_of_payment.get_absolute_url }}">
+                  {% include "bi/file-earmark-arrow-down.html" %}
+                </a>
+              {% else %}
+                <span class="text-danger">{% include "bi/x-square-fill.html" %}</span>
+              {% endif %}
+
+            </td>
+            <td>
+              <a class="btn btn-sm btn-warning text-white px-1 py-0"
+                 hx-get="{% url 'finances:_hx_subsidypayment_form' subsidy_id=subsidy.id subsidypayment_id=payment.id %}"
+                 hx-target="#subsidy-{{ subsidy.id }}-payment-form">
+                {% include "bi/pencil-square.html" %}
+              </a>
+              <a class="btn btn-sm btn-danger ms-2 px-1 py-0"
+                 hx-get="{% url 'finances:_hx_subsidypayment_delete' subsidy_id=subsidy.id subsidypayment_id=payment.id %}"
+                 hx-target="#subsidy-{{ subsidy.id }}-finadmin-details"
+                 hx-confirm="Are you sure you want to delete this payment information?">
+                {% include "bi/trash-fill.html" %}
+              </a>
+            </td>
+          </tr>
+        {% empty %}
+          <tr>
+            <td colspan="6">No payment has been scheduled</td>
+          </tr>
+        {% endfor %}
+
       </tbody>
     </table>
     <div id="subsidy-{{ subsidy.id }}-payment-form">
       <div hx-get="{% url 'finances:_hx_subsidypayment_button' subsidy_id=subsidy.id %}"
-	   hx-swap="outerHTML"
-	   hx-trigger="revealed"
-      >
-      </div>
+           hx-swap="outerHTML"
+           hx-trigger="revealed"></div>
     </div>
   </div>
-</details>
+</div>
diff --git a/scipost_django/finances/templates/finances/_hx_subsidy_list.html b/scipost_django/finances/templates/finances/_hx_subsidy_list.html
index c5d893554..9d75119eb 100644
--- a/scipost_django/finances/templates/finances/_hx_subsidy_list.html
+++ b/scipost_django/finances/templates/finances/_hx_subsidy_list.html
@@ -1,66 +1,95 @@
 {% for subsidy in page_obj %}
-  <tr>
+  <tr 
+    {% if "finadmin" in user_roles %} hx-get="{% url 'finances:_hx_subsidy_finadmin_details' subsidy_id=subsidy.id %}" hx-trigger="click" hx-target="#subsidy-{{ subsidy.id }}-finadmin-details" {% endif %}
+    >
     <td>
-      <a href="{{ subsidy.organization.get_absolute_url }}">
-	{{ subsidy.organization }}
-      </a>
+      <a href="{{ subsidy.organization.get_absolute_url }}">{{ subsidy.organization }}</a>
     </td>
     <td>
+
       {% if subsidy.nr_visible_notes %}
-      <span class="text-primary me-1" data-bs-placement='bottom' data-bs-toggle="tooltip" data-bs-html="true" title="There exist {{ subsidy.nr_visible_notes }} notes related to this Subsidy.">{% include 'bi/info-circle-fill.html' %}</span>
-      {% endif %}
-      <a href="{{ subsidy.get_absolute_url }}">
-	{{ subsidy.get_subsidy_type_display }}
-      </a>
-      {% if subsidy.renewal_of.all|length > 0 %}
-	<br/>
-	<span class="small text-muted">Renewal of:
-	  <ul class="list-unstyled">
-	    {% for prevsub in subsidy.renewal_of.all %}
-	      <li><a href="{{ prevsub.get_absolute_url }}">{{ prevsub }}</a></li>
-	    {% endfor %}
-	  </ul>
-	</span>
+        <span class="text-primary me-1"
+              data-bs-placement='bottom'
+              data-bs-toggle="tooltip"
+              data-bs-html="true"
+              title="There exist {{ subsidy.nr_visible_notes }} notes related to this Subsidy.">
+          {% include 'bi/info-circle-fill.html' %}
+        </span>
       {% endif %}
-      {% if subsidy.renewed_by.all|length > 0 %}
-	<br/>
-	<span class="small text-muted">Renewed by:
-	  <ul class="list-unstyled">
-	    {% for newsub in subsidy.renewed_by.all %}
-	      <li><a href="{{ newsub.get_absolute_url }}">{{ newsub }}</a></li>
-	    {% endfor %}
-	  </ul>
-	</span>
+
+      <a href="{{ subsidy.get_absolute_url }}">{{ subsidy.get_subsidy_type_display }}</a>
+    </td>
+
+    {% if perms.scipost.can_manage_subsidies %}
+      <td>
+
+        {% if subsidy.payments_all_scheduled %}
+          <span class="text-success">{% include 'bi/check-circle-fill.html' %}</span>
+        {% elif not subsidy.payments.exists %}
+          <span class="text-danger">{% include 'bi/x-circle-fill.html' %}</span>
+        {% else %}
+          <span class="text-warning">{% include 'bi/plus-circle-fill.html' %}</span>
+        {% endif %}
+
+ 
+      </td>
+    {% endif %}
+
+    <td>
+
+      {% if subsidy.amount_publicly_shown or perms.scipost.can_manage_subsidies %}
+        &euro;{{ subsidy.amount }}
+      {% else %}
+        -
       {% endif %}
+
     </td>
-    <td>{% if subsidy.amount_publicly_shown or perms.scipost.can_manage_subsidies %}&euro;{{ subsidy.amount }}{% else %}-{% endif %}</td>
-    <td>{{ subsidy.date_from }}</td>
-    <td class="bg-{{ subsidy.date_until_color_class }}">{{ subsidy.date_until }}</td>
+    <td class="text-nowrap">{{ subsidy.date_from }}</td>
+    <td class="text-nowrap bg-{{ subsidy.date_until_color_class }} {% if subsidy.date_until_color_class != "transparent" %}text-white{% endif %}">{{ subsidy.date_until }}</td>
+
     {% if perms.scipost.can_manage_subsidies %}
       <td>{{ subsidy.get_status_display }}</td>
       <td>
-	{% if subsidy.renewable == True %}<span class="text-success">{% include 'bi/check-circle-fill.html' %}</span>{% elif subsidy.renewable == False %}<span class="text-danger">{% include 'bi/x-circle-fill.html' %}</span>{% else %}<span class="text-warning">{% include 'bi/question-circle-fill.html' %}</span>{% endif %}
+	
+
+        {% if subsidy.renewable == True %}
+          <span class="text-success">{% include 'bi/check-circle-fill.html' %}</span>
+        {% elif subsidy.renewable == False %}
+          <span class="text-danger">{% include 'bi/x-circle-fill.html' %}</span>
+        {% else %}
+          <span class="text-warning">{% include 'bi/question-circle-fill.html' %}</span>
+        {% endif %}
+
       </td>
       <td>
-	{% if subsidy.renewed_by.all|length > 0 %}<span class="text-success">{% include 'bi/check-circle-fill.html' %}</span>{% else %}<span class="text-danger">{% include 'bi/x-circle-fill.html' %}</span>{% endif %}
+	
+
+        {% if subsidy.renewed_by.all|length > 0 %}
+          <span class="text-success">{% include 'bi/check-circle-fill.html' %}</span>
+        {% else %}
+          <span class="text-danger">{% include 'bi/x-circle-fill.html' %}</span>
+        {% endif %}
+
+      </td>
+      <td class="text-nowrap bg-{{ subsidy.renewal_action_date_color_class }} {% if subsidy.renewal_action_date_color_class != "transparent" %}text-white{% endif %}">
+        {{ subsidy.renewal_action_date }}
       </td>
-      <td class="bg-{{ subsidy.renewal_action_date_color_class }}">{{ subsidy.renewal_action_date }}</td>
     {% endif %}
+
   </tr>
+
   {% if "finadmin" in user_roles %}
-    <tr>
-      <td colspan="9" class="ps-4 pb-4">
-	<div id="subsidy-{{ subsidy.id }}-finadmin-details">
-	  <div hx-get="{% url 'finances:_hx_subsidy_finadmin_details' subsidy_id=subsidy.id %}"
-	       hx-trigger="revealed"
-	       hx-target="#subsidy-{{ subsidy.id }}-finadmin-details"
-	  >
-	  </div>
-	</div>
+    <tr class="">
+      <td colspan="10" class="p-0">
+        <div id="subsidy-{{ subsidy.id }}-finadmin-details"></div>
       </td>
     </tr>
   {% endif %}
+
 {% endfor %}
+
+
+
 {% if page_obj.has_next %}
   <tr id="subsidy-table-next-rows"
       hx-post="{% url 'finances:_hx_subsidy_list' %}?page={{ page_obj.next_page_number }}"
@@ -68,15 +97,15 @@
       hx-trigger="revealed"
       hx-target="#subsidy-table-next-rows"
       hx-swap="outerHTML"
-      hx-indicator="#indicator-subsidy-page-{{ page_obj.number }}"
-  >
-    <td id="indicator-subsidy-page-{{ page_obj.number }}"
-	    class="htmx-indicator p-2"
-    >
-      <button class="btn btn-warning" type="button" disabled>
-	<strong>Loading page {{ page_obj.next_page_number }} out of {{ page_obj.paginator.num_pages }}</strong>
-	<div class="spinner-grow spinner-grow-sm ms-2" role="status" aria-hidden="true"></div>
-      </button>
+      hx-indicator="#indicator-subsidy-page-{{ page_obj.number }}">
+    <td colspan="20" id="indicator-subsidy-page-{{ page_obj.number }}"
+        class="htmx-indicator p-0">
+        <div class="p-2 bg-primary bg-opacity-25 d-flex justify-content-center">
+          <strong>Loading page {{ page_obj.next_page_number }} out of {{ page_obj.paginator.num_pages }}</strong>
+          <div class="spinner-grow spinner-grow-sm ms-2"
+               role="status"
+               aria-hidden="true"></div>
+        </div>
     </td>
   </tr>
 {% endif %}
diff --git a/scipost_django/finances/templates/finances/subsidy_list.html b/scipost_django/finances/templates/finances/subsidy_list.html
index bab324d2d..7b6f155d5 100644
--- a/scipost_django/finances/templates/finances/subsidy_list.html
+++ b/scipost_django/finances/templates/finances/subsidy_list.html
@@ -78,11 +78,14 @@
       </div>
 
 
-      <table class="table">
-        <thead class="table-light">
+      <table class="table table-hover position-relative">
+        <thead class="table-light position-sticky top-0">
           <tr>
             <th>From Organization</th>
             <th>Type</th>
+            {% if perms.scipost.can_manage_subsidies %}
+            <th><span class="small" style="writing-mode: vertical-rl;">Payments<br />Scheduled?</span></th>
+            {% endif %}
             <th>Amount</th>
             <th>From</th>
             <th>Until</th>
-- 
GitLab