From fad75ac2ac571d459e6ece8345e85f4e700544b8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Sat, 16 Mar 2024 07:20:55 +0100
Subject: [PATCH] Display compensation and coverage info to Subsidy detail page

---
 scipost_django/finances/models/subsidy.py     | 17 +++-
 .../templates/finances/_subsidy_details.html  | 97 +++++++++++++++++++
 2 files changed, 111 insertions(+), 3 deletions(-)

diff --git a/scipost_django/finances/models/subsidy.py b/scipost_django/finances/models/subsidy.py
index 44a81a4be..969128667 100644
--- a/scipost_django/finances/models/subsidy.py
+++ b/scipost_django/finances/models/subsidy.py
@@ -122,7 +122,7 @@ class Subsidy(models.Model):
         return self.amount == self.payments.aggregate(Sum("amount"))["amount__sum"]
 
     @property
-    def committed(self):
+    def total_compensations(self):
         """
         Sum of the amounts of all PubFracCompensations related to this Subsidy.
         """
@@ -132,9 +132,20 @@ class Subsidy(models.Model):
             else 0
         )
 
+    @property
+    def total_coverages(self):
+        """
+        Sum of the PublicationExpenditureCoverages related to this Subsidy.
+        """
+        return (
+            self.pex_coverages.aggregate(Sum("amount"))["amount__sum"]
+            if self.pex_coverages.exists()
+            else 0
+        )
+
     @property
     def remainder(self):
         """
-        Part of the Subsidy amount which hasn't been used in a PubFracCompensation.
+        Part of the Subsidy amount which hasn't been allocated.
         """
-        return self.amount - self.committed
+        return self.amount - self.total_compensations - self.total_coverages
diff --git a/scipost_django/finances/templates/finances/_subsidy_details.html b/scipost_django/finances/templates/finances/_subsidy_details.html
index 1d17cc118..57de260f9 100644
--- a/scipost_django/finances/templates/finances/_subsidy_details.html
+++ b/scipost_django/finances/templates/finances/_subsidy_details.html
@@ -105,3 +105,100 @@
     </table>
   </div>
 </div>
+
+
+{% if 'edadmin' in user_roles %}
+  {% if subsidy.amount_publicly_shown %}
+
+    <div class="row">
+      <div class="col-12">
+	<h3 class="highlight">Expenditures compensated or covered by this Subsidy</h3>
+
+	<table class="table mt-2 caption-top">
+	  <caption>PubFrac Compensations</caption>
+	  <thead class="table-light">
+	    <tr>
+	      <th>Publication</th>
+	      <th>PubFrac value</th>
+	      <th>PubFrac compensation</th>
+	    </tr>
+	  </thead>
+	  <tbody>
+	    {% for compensation in subsidy.pubfrac_compensations.all %}
+	      <tr>
+		<td>
+		  <a href="{% url 'scipost:publication_detail' doi_label=compensation.pubfrac.publication.doi_label %}">{{ compensation.pubfrac.publication.doi_label }}</a>
+		</td>
+		<td>&euro;{{ compensation.pubfrac.cf_value }}</td>
+		<td>&euro;{{ compensation.amount }}</td>
+	      </tr>
+	    {% empty %}
+	      <tr>
+		<td>No Compensation defined</td>
+	      </tr>
+	    {% endfor %}
+	    <tr class="bg-secondary bg-opacity-10">
+	      <th>Total compensations from this Subsidy</th>
+	      <td></td>
+	      <td>&euro;{{ subsidy.total_compensations }}</td>
+	    </tr>
+	  </tbody>
+	</table>
+
+	<table class="table caption-top">
+	  <caption>Expenditure Coverages</caption>
+	  <thead class="table-light">
+	    <tr>
+	      <th>Publication</th>
+	      <th>Expenditure</th>
+	      <th>Coverage</th>
+	    </tr>
+	  </thead>
+	  <tbody>
+	    {% for coverage in subsidy.pex_coverages.all %}
+	      <tr>
+		<td>
+		  <a href="{% url 'scipost:publication_detail' doi_label=coverage.publication.doi_label %}">{{ coverage.publication.doi_label }}</a>
+		</td>
+		<td>&euro;{{ coverage.publication.expenditures }}</td>
+		<td>&euro;{{ coverage.amount }}</td>
+	      </tr>
+	    {% empty %}
+	      <tr>
+		<td>No Coverage defined</td>
+	      </tr>
+	    {% endfor %}
+	    <tr class="bg-secondary bg-opacity-10">
+	      <th>Total coverages from this Subsidy</th>
+	      <td></td>
+	      <td>&euro;{{ subsidy.total_coverages }}</td>
+	    </tr>
+	  </tbody>
+	</table>
+
+	<table class="table mt-2 caption-top">
+	  <caption>Balance</caption>
+	  <tbody>
+	    <tr>
+	      <th>Subsidy amount</th>
+	      <td>{{ subsidy.amount }}</td>
+	    </tr>
+	    <tr>
+	      <th>Compensations</th>
+	      <td>{{ subsidy.total_compensations }}</td>
+	    </tr>
+	    <tr>
+	      <th>Coverages</th>
+	      <td>{{ subsidy.total_coverages }}</td>
+	    </tr>
+	    <tr class="bg-secondary bg-opacity-10">
+	      <th>Remainder (allocated to reserve fund)</th>
+	      <td>{{ subsidy.remainder }}</td>
+	    </tr>
+	  </tbody>
+	</table>
+
+      </div>
+    </div>
+  {% endif %}
+{% endif %}
-- 
GitLab