SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 4d1de76f authored by Jean-Sébastien Caux's avatar Jean-Sébastien Caux
Browse files

Improve display of support history for AffiliateJournal

parent dd318952
No related branches found
No related tags found
No related merge requests found
...@@ -55,20 +55,31 @@ class AffiliateJournal(models.Model): ...@@ -55,20 +55,31 @@ class AffiliateJournal(models.Model):
return int(self.cost_info["default"]) return int(self.cost_info["default"])
@property @property
def get_balance_info(self): def balance_info(self):
return self.get_balance_info()
def get_balance_info(self, organization=None):
""" """
For each publishing year, provide financial info. For each publishing year, provide financial info.
""" """
maxyear = self.publications.first().publication_date.year publications = self.publications.all()
minyear = self.publications.last().publication_date.year subsidies = self.affiliatejournalyearsubsidy_set.all()
if organization:
publications = publications.filter(
pubfractions__organization=organization
)
subsidies = subsidies.filter(organization=organization)
if not publications:
return {}
maxyear = publications.first().publication_date.year
minyear = publications.last().publication_date.year
years = range(maxyear, minyear - 1, -1) years = range(maxyear, minyear - 1, -1)
balance_info = {} balance_info = {}
for year in years: for year in years:
nr_publications = self.publications.filter( nr_publications = publications.filter(
publication_date__year=year).count() publication_date__year=year).count()
subsidy_tally = self.affiliatejournalyearsubsidy_set.filter( subsidy_tally = subsidies.filter(
journal=self, year=year
year=year,
).aggregate(models.Sum("amount"))["amount__sum"] ).aggregate(models.Sum("amount"))["amount__sum"]
if not subsidy_tally: if not subsidy_tally:
subsidy_tally = 0 subsidy_tally = 0
...@@ -80,4 +91,20 @@ class AffiliateJournal(models.Model): ...@@ -80,4 +91,20 @@ class AffiliateJournal(models.Model):
"balance": (subsidy_tally - "balance": (subsidy_tally -
nr_publications * self.cost_per_publication(year)), nr_publications * self.cost_per_publication(year)),
} }
if organization:
from ..models import AffiliatePubFraction
sum_pubfracs = AffiliatePubFraction.objects.filter(
publication__journal=self,
organization=organization,
publication__publication_date__year=year,
).aggregate(models.Sum("fraction"))["fraction__sum"]
if not sum_pubfracs:
sum_pubfracs = 0
balance_info[year]["pubfractions"] = sum_pubfracs
balance_info[year][
"expenditure"
] = int(sum_pubfracs * self.cost_per_publication(year))
balance_info[year][
"balance"
] = int(subsidy_tally - sum_pubfracs * self.cost_per_publication(year))
return balance_info return balance_info
...@@ -30,8 +30,60 @@ ...@@ -30,8 +30,60 @@
<div class="tab-pane pt-4" id="support-{{ org.id }}" role="tabpanel" aria-labelledby="support-{{ org.id }}-tab"> <div class="tab-pane pt-4" id="support-{{ org.id }}" role="tabpanel" aria-labelledby="support-{{ org.id }}-tab">
<h3 class="highlight">Support history</h3> <h3 class="highlight">Support history</h3>
<table class="table table-hover mb-5">
<thead class="table-light">
<tr>
<th>Year</th>
<th style="text-align: right;">Amount</th>
</tr>
</thead>
<tbody>
{% for subsidy in subsidies.all %}
<tr>
<td>{{ subsidy.year }}</td>
<td style="text-align: right;">{{ subsidy.amount }}</td>
</tr>
{% empty %}
<tr>
<td>No subsidies found</td>
</tr>
{% endfor %}
</tbody>
</table>
<h3 class="highlight mt-4">Balance of expenditures versus support received</h3> <h3 class="highlight mt-4">Balance of expenditures versus support received</h3>
<h3 class="text-warning">{% include "bi/cone-striped.html" %}</h3> <table class="table" style="text-align: right;">
<thead>
<tr>
<th>Year</th>
<th>Nr publications</th>
<th>Pubfractions</th>
<th>Unit cost</th>
<th>Expenditure</th>
<th>Subsidies</th>
<th>Balance (&euro;)</th>
</tr>
</thead>
<tbody>
<tr class="table-light">
<td>Cumulative</td>
<td colspan="5"></td>
<td>{{ balance_cumulative }}</td>
</tr>
{% for year, balance in balance_info.items %}
<tr>
<td>{{ year }}</td>
<td>{{ balance.nr_publications }}</td>
<td>{{ balance.pubfractions }}</td>
<td>{{ balance.unit_cost }}</td>
<td>{{ balance.expenditure }}</td>
<td>{{ balance.subsidies }}</td>
<td>{{ balance.balance }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div> </div>
</div> </div>
......
...@@ -195,7 +195,7 @@ ...@@ -195,7 +195,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for year, balance in object.get_balance_info.items %} {% for year, balance in object.balance_info.items %}
<tr> <tr>
<td>{{ year }}</td> <td>{{ year }}</td>
<td>{{ balance.nr_publications }}</td> <td>{{ balance.nr_publications }}</td>
......
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
<th>Organization</th> <th>Organization</th>
<th>Country</th> <th>Country</th>
<th>Year</th> <th>Year</th>
<th align="right">Subsidy (&euro;)</th> <th style="text-align: right;">Subsidy (&euro;)</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
</td> </td>
<td><img src="{{ subsidy.organization.country.flag }}" alt="{{ subsidy.organization.country }} flag"/>&nbsp;<span class="text-muted"><small>[{{ subsidy.organization.country }}]</small></span>&nbsp;&nbsp;{{ subsidy.organization.get_country_display }}</td> <td><img src="{{ subsidy.organization.country.flag }}" alt="{{ subsidy.organization.country }} flag"/>&nbsp;<span class="text-muted"><small>[{{ subsidy.organization.country }}]</small></span>&nbsp;&nbsp;{{ subsidy.organization.get_country_display }}</td>
<td>{{ subsidy.year }}</td> <td>{{ subsidy.year }}</td>
<td align="right">{{ subsidy.amount }}</td> <td style="text-align: right;">{{ subsidy.amount }}</td>
{% if 'manage_journal_content' in user_perms %} {% if 'manage_journal_content' in user_perms %}
<td> <td>
<a href="{% url 'affiliates:journal_delete_subsidy' slug=journal.slug pk=subsidy.id %}"> <a href="{% url 'affiliates:journal_delete_subsidy' slug=journal.slug pk=subsidy.id %}">
......
...@@ -239,11 +239,20 @@ def affiliatejournal_organization_detail(request, slug, organization_id): ...@@ -239,11 +239,20 @@ def affiliatejournal_organization_detail(request, slug, organization_id):
organization=organization, organization=organization,
publication__journal=journal, publication__journal=journal,
).prefetch_related("publication__journal") ).prefetch_related("publication__journal")
subsidies = AffiliateJournalYearSubsidy.objects.filter(
journal=journal,
organization=organization,
)
context = { context = {
"journal": journal, "journal": journal,
"organization": organization, "organization": organization,
"affiliatepubfractions": affiliatepubfractions, "affiliatepubfractions": affiliatepubfractions,
"subsidies": subsidies,
"balance_info": journal.get_balance_info(organization=organization),
} }
context["balance_cumulative"] = sum(
[y["balance"] for y in context["balance_info"].values()]
)
return render( return render(
request, request,
"affiliates/affiliatejournal_organization_detail.html", "affiliates/affiliatejournal_organization_detail.html",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment