Skip to content

Commit

Permalink
fix: no calculation data for rejected applications (#3080)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke committed Jun 11, 2024
1 parent 88b87be commit 7f7f96d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 29 deletions.
19 changes: 16 additions & 3 deletions backend/benefit/applications/services/ahjo_xml_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from lxml import etree
from lxml.etree import XMLSchema, XMLSchemaParseError, XMLSyntaxError

from applications.enums import ApplicationStatus
from applications.models import AhjoDecisionText, Application
from calculator.enums import RowType
from calculator.models import Calculation, CalculationRow
Expand Down Expand Up @@ -103,6 +104,7 @@ class BenefitPeriodRow:
class AhjoSecretXMLBuilder(AhjoXMLBuilder):
def generate_xml(self) -> AhjoXMLString:
context = self.get_context_for_secret_xml()

# Set the locale for this thread to the application's language
translation.activate(self.application.applicant_language)

Expand Down Expand Up @@ -185,6 +187,20 @@ def _prepare_single_period_row(
return calculation_rows_for_xml

def get_context_for_secret_xml(self) -> dict:
"""Get the context for the secret XML."""
context = {
"application": self.application,
"benefit_type": _("Salary Benefit"),
"language": self.application.applicant_language,
"include_calculation_data": False,
}
if self.application.status == ApplicationStatus.ACCEPTED:
context_rest = self.get_context_for_accepted_decision_xml()
context = {**context, **context_rest}
context["include_calculation_data"] = True
return context

def get_context_for_accepted_decision_xml(self) -> dict:
total_amount_row, calculation_rows = self._get_period_rows_for_xml(
self.application.calculation
)
Expand All @@ -201,11 +217,8 @@ def get_context_for_secret_xml(self) -> dict:
calculation_rows
)
return {
"application": self.application,
"benefit_type": _("Salary Benefit"),
"calculation_periods": calculation_data_for_xml,
"total_amount_row": total_amount_row,
"language": self.application.applicant_language,
}

def generate_xml_file_name(self) -> str:
Expand Down
30 changes: 17 additions & 13 deletions backend/benefit/applications/templates/secret_decision.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,25 @@
<td>{{ benefit_type }}</td>
</tr>

<tr>
<th scope="row">{{ _("Applying for dates") }}</th>
<td>{{ total_amount_row.start_date | date:"d.m.Y" }} - {{ total_amount_row.end_date |date:"d.m.Y" }}</td>
</tr>
<tr>
<th>{{ _("Received aid") }}</th>
<td>{{ total_amount_row.amount }} €</td>
</tr>
{% if application.calculation.granted_as_de_minimis_aid %}
<tr>
<th>{{ _("Additional information") }}</th>
<td>{{ _("De Minimis description") }}</td>
</tr>
{% if include_calculation_data %}
<tr>
<th scope="row">{{ _("Applying for dates") }}</th>
<td>{{ total_amount_row.start_date | date:"d.m.Y" }} - {{ total_amount_row.end_date |date:"d.m.Y" }}</td>
</tr>
<tr>
<th>{{ _("Received aid") }}</th>
<td>{{ total_amount_row.amount }} €</td>
</tr>
{% if application.calculation.granted_as_de_minimis_aid %}
<tr>
<th>{{ _("Additional information") }}</th>
<td>{{ _("De Minimis description") }}</td>
</tr>
{% endif %}
{% endif %}
</tbody>
</table>
{% if include_calculation_data %}
<h2>{{ _("Calculation of the benefit") }}</h2>
<table class="rowbordersonly">
<thead>
Expand All @@ -62,6 +65,7 @@
<tr><th colspan="2">{{ _("Total for benefit time period") }}</th><td class="sum"><strong>{{ total_amount_row.amount }} €</strong></td></tr>
</tbody>
</table>
{% endif %}
</main>
</body>

78 changes: 65 additions & 13 deletions backend/benefit/applications/tests/test_ahjo_xml_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from applications.enums import ApplicationStatus
from applications.services.ahjo_xml_builder import (
AhjoPublicXMLBuilder,
AhjoSecretXMLBuilder,
Expand Down Expand Up @@ -114,7 +115,19 @@ def test_public_xml_file_name(decided_application, public_xml_builder):
assert xml_file_name == wanted_file_name


def test_secret_xml_decision_string(decided_application, secret_xml_builder):
@pytest.mark.parametrize(
"application_status",
[
ApplicationStatus.ACCEPTED,
ApplicationStatus.REJECTED,
],
)
def test_secret_xml_decision_string(
application_status, decided_application, secret_xml_builder
):
decided_application.status = application_status
decided_application.save()

calculation = decided_application.calculation
row = CalculationRowFactory(
calculation=calculation,
Expand All @@ -124,18 +137,39 @@ def test_secret_xml_decision_string(decided_application, secret_xml_builder):
end_date=calculation.end_date,
)

xml_string = secret_xml_builder.generate_xml()

wanted_replacements = [
str(decided_application.application_number),
decided_application.company.name,
decided_application.company.business_id,
decided_application.employee.last_name,
decided_application.employee.first_name,
f"{row.start_date.strftime('%d.%m.%Y')} - {row.end_date.strftime('%d.%m.%Y')}",
str(int(row.amount)),
str(int(calculation.calculated_benefit_amount)),
]
if application_status == ApplicationStatus.ACCEPTED:
xml_string = secret_xml_builder.generate_xml()

wanted_replacements = [
str(decided_application.application_number),
decided_application.company.name,
decided_application.company.business_id,
decided_application.employee.last_name,
decided_application.employee.first_name,
f"{row.start_date.strftime('%d.%m.%Y')} - {row.end_date.strftime('%d.%m.%Y')}",
str(int(row.amount)),
str(int(calculation.calculated_benefit_amount)),
]

elif application_status == ApplicationStatus.REJECTED:
xml_string = secret_xml_builder.generate_xml()

wanted_replacements = [
str(decided_application.application_number),
decided_application.company.name,
decided_application.company.business_id,
decided_application.employee.last_name,
decided_application.employee.first_name,
]
unwanted_replacements = [
f"{row.start_date.strftime('%d.%m.%Y')} - {row.end_date.strftime('%d.%m.%Y')}",
str(int(row.amount)),
str(int(calculation.calculated_benefit_amount)),
]
assert all(
[replacement not in xml_string for replacement in unwanted_replacements]
)

assert all([replacement in xml_string for replacement in wanted_replacements])


Expand Down Expand Up @@ -198,12 +232,28 @@ def test_prepare_single_period_row(secret_xml_builder, monthly_row_1, total_eur_
assert period_rows[0].total_amount == int(total_eur_row.amount)


def test_get_context_for_secret_xml_for_rejected_application(
decided_application, secret_xml_builder
):
decided_application.status = ApplicationStatus.REJECTED
decided_application.save()

context = secret_xml_builder.get_context_for_secret_xml()

assert context["application"] == decided_application
assert context["language"] == decided_application.applicant_language
assert context["include_calculation_data"] is False
assert "calculation_periods" not in context
assert "total_amount_row" not in context


def test_get_context_for_secret_xml_with_single_period(
decided_application, monthly_row_1, total_eur_row, secret_xml_builder
):
context = secret_xml_builder.get_context_for_secret_xml()

assert context["application"] == decided_application
assert context["include_calculation_data"] is True
assert len(context["calculation_periods"]) == 1
assert isinstance(context["calculation_periods"][0], BenefitPeriodRow)
assert context["calculation_periods"][0].start_date == total_eur_row.start_date
Expand Down Expand Up @@ -245,6 +295,8 @@ def test_get_context_for_secret_xml_with_multiple_periods(
context = secret_xml_builder.get_context_for_secret_xml()

assert context["application"] == decided_application
assert context["include_calculation_data"] is True

assert len(context["calculation_periods"]) == 2
assert isinstance(context["calculation_periods"][0], BenefitPeriodRow)
assert context["calculation_periods"][0].start_date == sub_total_row_1.start_date
Expand Down

0 comments on commit 7f7f96d

Please sign in to comment.