Skip to content

Commit

Permalink
feat: full pdf summary of applications (HL-708, HL-903) (#2524)
Browse files Browse the repository at this point in the history
* feat: new function to change test seeds

* feat: application HTML/PDF print view

* feat: add 'print application' link as button

* chore: skip flaky handler browser test
  • Loading branch information
sirtawast authored Nov 28, 2023
1 parent 410ac0e commit 644aaf1
Show file tree
Hide file tree
Showing 28 changed files with 1,286 additions and 93 deletions.
25 changes: 25 additions & 0 deletions backend/benefit/applications/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.db import transaction
from django.db.models import Q, QuerySet
from django.http import FileResponse, HttpResponse, StreamingHttpResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
Expand All @@ -15,7 +16,9 @@
from rest_framework import filters as drf_filters, status
from rest_framework.decorators import action
from rest_framework.parsers import MultiPartParser
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from sql_util.aggregates import SubqueryCount

from applications.api.v1.serializers.application import (
Expand All @@ -35,6 +38,10 @@
prepare_csv_file,
prepare_pdf_files,
)
from applications.services.app_generated_files import (
generate_application_summary_file,
get_context_for_summary_context,
)
from applications.services.applications_csv_report import ApplicationsCsvService
from common.permissions import BFIsApplicant, BFIsHandler, TermsOfServiceAccepted
from messages.models import MessageType
Expand Down Expand Up @@ -491,3 +498,21 @@ def _csv_pdf_response(
)
response["Content-Disposition"] = f"attachment; filename={zip_filename}"
return response


class PrintDetail(APIView):
renderer_classes = [TemplateHTMLRenderer]
permission_classes = [BFIsApplicant]

def get(self, request, *args, **kwargs):
pk = kwargs["pk"]
application = get_object_or_404(Application, pk=pk)
self.check_object_permissions(request, application)

if settings.DEBUG and request.query_params.get("as_html") == "1":
context = get_context_for_summary_context(application)
return Response(context, template_name="application.html")

return HttpResponse(
generate_application_summary_file(application), "application/pdf"
)
2 changes: 2 additions & 0 deletions backend/benefit/applications/services/ahjo_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ExportFileInfo:
"show_de_minimis_aid_footer": False,
"show_employee_names": True,
"show_sums": True,
"is_private": True,
},
},
TEMPLATE_ID_COMPOSED_DECLINED_PRIVATE: {
Expand All @@ -99,6 +100,7 @@ class ExportFileInfo:
"show_de_minimis_aid_footer": False,
"show_employee_names": True,
"show_sums": False,
"is_private": True,
},
},
}
Expand Down
41 changes: 41 additions & 0 deletions backend/benefit/applications/services/app_generated_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pdfkit
from django.template import loader


def get_context_for_summary_context(application):
def count_version_number(number_of_versions):
# Minimum version 1.0, increase by 0.1 for each version
version = max(number_of_versions + 1, 10) / 10.0
return str(version).replace(",", ".")

def total_de_minimis_amount(application):
de_minimis_amount = 0
if application.de_minimis_aid_set.count():
for aid in application.de_minimis_aid_set.all():
de_minimis_amount = de_minimis_amount + float(aid.amount)
return (
"{:20,.2f}".format(de_minimis_amount)
.replace(",", " ")
.replace(".", ",")
)
return de_minimis_amount

return {
"application": application,
"attachments": application.attachments.all(),
"de_minimis_amount": total_de_minimis_amount(application),
"de_minimis_aid_set": application.de_minimis_aid_set.all(),
"versions": {
"application": count_version_number(len(application.history.all()))
},
}


def generate_application_summary_file(application, request=None) -> bytes:
def generate_summary_pdf(context) -> bytes:
template = loader.get_template("application.html")
rendered_template = template.render(context, request)
return pdfkit.from_string(rendered_template, False, None)

context = get_context_for_summary_context(application)
return generate_summary_pdf(context)
Original file line number Diff line number Diff line change
@@ -1,86 +1,105 @@
{% extends "base.html" %}
{% extends "base.html" %} {% block content %}
<p>{{ title }}</p>

{% block content %}
<p>{{ title }}</p>
<table aria-label="AHJO report" style="border: 1px solid black">
<tr>
<th scope="col">Hakemusnumero</th>
<th scope="col">Hakija</th>
<th scope="col">Y-tunnus</th>
{% if show_employee_names %}
<th scope="col">Työllistetty</th>
{% endif %}
<th scope="col">Aika</th>
<th scope="col">Tukimuoto</th>
{% if show_sums %}
<th class="text-right" scope="col">Summa/kk</th>
<th class="text-right" scope="col">Yhteensä</th>
{% endif %}
</tr>
{% for app in apps %}
{% if not show_ahjo_rows or app.ahjo_rows.count() == 0 %}
<tr>
<td>{{ app.ahjo_application_number }}</td>
<td>{{ app.company_name }}</td>
<td>{{ app.company.business_id }}</td>
{% if show_employee_names %}
<td>{{ app.employee.first_name }} {{ app.employee.last_name }}</td>
{% endif %}
<td>{{ app.start_date.strftime("%d.%m.%Y") }} - {{ app.end_date.strftime("%d.%m.%Y")}}</td>
<td>{{ app.get_benefit_type_display() }}</td>
{% if show_sums and app.calculated_benefit_amount and app.calculation.duration_in_months %}
<td class="text-right">{{ "%.0f €" | format(app.calculated_benefit_amount/(app.calculation.duration_in_months | round(2)))}} </td>
<td class="text-right"><strong>{{ "%.0f €" | format(app.calculated_benefit_amount)}}</strong></td>
{% elif show_sums %}
<td class="text-right">0 €</td>
<td class="text-right"><strong>0 €</strong></td>
{% endif %}
</tr>
{% else %}
{% for ahjo_row in app.ahjo_rows %}
<tr>
<td>{{ app.ahjo_application_number }}</td>
<td>{{ app.company_name }}</td>
<td>{{ app.company.business_id }}</td>
{% if show_employee_names %}
<td>{{ app.employee.first_name }} {{ app.employee.last_name }}</td>
{% endif %}
<td>{{ ahjo_row.start_date.strftime("%d.%m.%Y") }} - {{ ahjo_row.end_date.strftime("%d.%m.%Y")}}</td>
<td>{{ app.get_benefit_type_display() }}</td>
{% if show_sums %}
<td class="text-right">{{ "%.0f €" | format(ahjo_row.amount/(ahjo_row.duration_in_months | round(2)))}}</td>
<td class="text-right"><strong>{{ "%.0f €" | format(ahjo_row.amount)}}</strong></td>
{% endif %}
</tr>
{% endfor %}
{% endif %}
{% endfor %}
<style>
{% if is_private %}
tr th, tr:last-child td {
background: firebrick !important;
}
{% endif %}
</style>

<table aria-label="AHJO report" style="border: 1px solid black">
<tr>
<th scope="col">Hakemusnumero</th>
<th scope="col">Hakija</th>
<th scope="col">Y-tunnus</th>
{% if show_employee_names %}
<th scope="col">Työllistetty</th>
{% endif %}
<th scope="col">Aika</th>
<th scope="col">Tukimuoto</th>
{% if show_sums %}
{% set benefit = namespace(amounts=0) %}
{% for app in apps %}
{% if app.calculated_benefit_amount is not none %}
{% set benefit.amounts = benefit.amounts + app.calculated_benefit_amount %}
{% endif %}
{% endfor %}
<tr>
<td colspan="8" class="footer text-right"> Kaikki yhteensä
<strong>
{{ "%.0f €" | format(benefit.amounts) }}
</strong>
</td>
</tr>
<th class="text-right" scope="col">Summa/kk</th>
<th class="text-right" scope="col">Yhteensä</th>
{% endif %}
</table>

{% if show_de_minimis_aid_footer %}
<p style="color: #999898; padding: 0px 50px; text-align: center">Tämän päätöksen suoma tuki myönnetään vähämerkityksisenä eli ns. de
minimis -tukena.
Tuen myöntämisessä noudatetaan komission asetusta (EU) 1407/2013, 24.12.2013, perustamissopimuksen 107 ja 108 artiklan soveltamisesta vähämerkityksiseen tukeen (EUVL nro L352, 24.12.2013).</p>
</tr>
{% for app in apps %} {% if not show_ahjo_rows or app.ahjo_rows.count() == 0
%}
<tr>
<td>{{ app.ahjo_application_number }}</td>
<td>{{ app.company_name }}</td>
<td>{{ app.company.business_id }}</td>
{% if show_employee_names %}
<td>{{ app.employee.first_name }} {{ app.employee.last_name }}</td>
{% endif %}
<td>
{{ app.start_date.strftime("%d.%m.%Y") }} - {{
app.end_date.strftime("%d.%m.%Y")}}
</td>
<td>{{ app.get_benefit_type_display() }}</td>
{% if show_sums and app.calculated_benefit_amount and
app.calculation.duration_in_months %}
<td class="text-right">
{{ "%.0f €" |
format(app.calculated_benefit_amount/(app.calculation.duration_in_months
| round(2)))}}
</td>
<td class="text-right">
<strong
>{{ "%.0f €" | format(app.calculated_benefit_amount)}}</strong
>
</td>
{% elif show_sums %}
<td class="text-right">0 €</td>
<td class="text-right"><strong>0 €</strong></td>
{% endif %}
</tr>
{% else %} {% for ahjo_row in app.ahjo_rows %}
<tr>
<td>{{ app.ahjo_application_number }}</td>
<td>{{ app.company_name }}</td>
<td>{{ app.company.business_id }}</td>
{% if show_employee_names %}
<td>{{ app.employee.first_name }} {{ app.employee.last_name }}</td>
{% endif %}
<td>
{{ ahjo_row.start_date.strftime("%d.%m.%Y") }} - {{
ahjo_row.end_date.strftime("%d.%m.%Y")}}
</td>
<td>{{ app.get_benefit_type_display() }}</td>
{% if show_sums %}
<td class="text-right">
{{ "%.0f €" | format(ahjo_row.amount/(ahjo_row.duration_in_months |
round(2)))}}
</td>
<td class="text-right">
<strong>{{ "%.0f €" | format(ahjo_row.amount)}}</strong>
</td>
{% endif %}
</tr>
{% endfor %} {% endif %} {% endfor %} {% if show_sums %} {% set benefit =
namespace(amounts=0) %} {% for app in apps %} {% if
app.calculated_benefit_amount is not none %} {% set benefit.amounts =
benefit.amounts + app.calculated_benefit_amount %} {% endif %} {% endfor %}
<tr>
<td colspan="8" class="footer text-right">
Kaikki yhteensä
<strong> {{ "%.0f €" | format(benefit.amounts) }} </strong>
</td>
</tr>
{% endif %}
</table>

{% if not show_employee_names %}
<p style="color: #999898; padding: 0px 50px; text-align: center">Työllistettyjen nimet poistettu. JulkL 24§ 1 mom
25 k.</p>
{% endif %}
{% endblock %}
{% if show_de_minimis_aid_footer %}
<p style="color: #999898; padding: 0px 50px; text-align: center">
Tämän päätöksen suoma tuki myönnetään vähämerkityksisenä eli ns. de minimis
-tukena. Tuen myöntämisessä noudatetaan komission asetusta (EU) 1407/2013,
24.12.2013, perustamissopimuksen 107 ja 108 artiklan soveltamisesta
vähämerkityksiseen tukeen (EUVL nro L352, 24.12.2013).
</p>
{% endif %} {% if not show_employee_names %}
<p style="color: #999898; padding: 0px 50px; text-align: center">
Työllistettyjen nimet poistettu. JulkL 24§ 1 mom 25 k.
</p>
{% endif %} {% endblock %}
5 changes: 5 additions & 0 deletions backend/benefit/applications/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base.html" %} {% block content %} {% include "style.html" %}
<div class="wrapper">
<main>{{ _("Not found") }}</main>
</div>
{%endblock%}
Loading

0 comments on commit 644aaf1

Please sign in to comment.