Skip to content

Commit

Permalink
feat: Hl 1445 add powerBI fields (#3334)
Browse files Browse the repository at this point in the history
* feat: change columns according to powerBI spec

* feat: test columns for the powerBI report
  • Loading branch information
rikuke authored Sep 20, 2024
1 parent f140a59 commit 69a2421
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class PowerBiIntegrationView(APIView):
def get(self, request, *args, **kwargs) -> StreamingHttpResponse:
# Apply the filter
filterset = ApplicationPowerBiFilter(
request.GET, queryset=Application.objects.all()
request.GET,
queryset=Application.objects.all().prefetch_related("alteration_set"),
)

if filterset.is_valid():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,109 @@
from applications.services.applications_csv_report import ( # csv_default_column,
from datetime import datetime
from typing import Union

from applications.enums import ApplicationBatchStatus
from applications.models import Application
from applications.services.applications_csv_report import (
ApplicationsCsvService,
csv_default_column,
format_bool,
format_datetime,
get_application_origin_label,
get_benefit_type_label,
)

# from applications.services.csv_export_base import CsvColumn
from applications.services.csv_export_base import CsvColumn, get_organization_type


class ApplicationsPowerBiCsvService(ApplicationsCsvService):
"""
This subclass customizes the CSV_COLUMNS for a different export format.
"""

def get_completed_in_talpa_date(
self, application: Application
) -> Union[datetime, None]:
if application.batch.status == ApplicationBatchStatus.COMPLETED:
return application.batch.modified_at.strftime("%d.%m.%Y")
return None

def get_alteration_amount(self, application: Application) -> float:
sum = 0
for alteration in application.alteration_set.all():
if alteration.recovery_amount:
sum += alteration.recovery_amount
return sum

@property
def CSV_COLUMNS(self):
"""
Customize the CSV columns but also return the parent class's columns.
"""
# Get the parent class CSV_COLUMNS
parent_columns = super().CSV_COLUMNS
calculated_benefit_amount = "calculation.calculated_benefit_amount"

# Define custom columns to add to or modify the parent columns
custom_columns = [
# CsvColumn("Custom Column 1", "custom_field_1"),
# CsvColumn("Custom Column 2", "custom_field_2"),
columns = [
CsvColumn("Hakemusnumero", "application_number"),
CsvColumn("Työnantajan tyyppi", get_organization_type),
CsvColumn("Työnantajan Y-tunnus", "company.business_id"),
csv_default_column(
"Helsinki-lisän määrä lopullinen", calculated_benefit_amount
),
csv_default_column("Päätöspäivä", "batch.decision_date"),
CsvColumn("Hakemuksen tila", "status"),
CsvColumn(
"Hakemuksen tyyppi", "application_origin", get_application_origin_label
),
CsvColumn("Hakemus saapunut", "created_at", format_datetime),
csv_default_column("Haettava lisä", "benefit_type", get_benefit_type_label),
csv_default_column("Haettu alkupäivä", "start_date"),
csv_default_column("Haettu päättymispäivä", "end_date"),
CsvColumn("Työnantajan yhtiömuoto", "company_form"),
CsvColumn("Työnantajan yhtiömuoto (YTJ-numero)", "company_form_code"),
CsvColumn(
"Yhdistys jolla taloudellista toimintaa?",
"association_has_business_activities",
format_bool,
),
CsvColumn("Hakijan kieli", "applicant_language"),
csv_default_column("Palkkatuki myönnetty?", "pay_subsidy_granted", str),
csv_default_column("Palkkatukiprosentti", "pay_subsidy_percent"),
csv_default_column(
"Oppisopimus?",
"apprenticeship_program",
format_bool,
default_value=None,
),
csv_default_column(
"Työntekijän kuukausipalkka (hakijalta)", "employee.monthly_pay"
),
csv_default_column(
"Työntekijän lomaraha (hakijalta)", "employee.vacation_money"
),
csv_default_column(
"Työntekijän muut kulut (hakijalta)", "employee.other_expenses"
),
csv_default_column("Työntekijän työtunnit", "employee.working_hours"),
csv_default_column("Työntekijän syntymäpäivä", "employee.birthday"),
csv_default_column(
"Helsinki-lisän määrä lopullinen", calculated_benefit_amount
),
csv_default_column("Laskelman alkupäivä", "calculation.start_date"),
csv_default_column("Laskelman päättymispäivä", "calculation.end_date"),
csv_default_column("Käsittelypäivä", "handled_at", format_datetime),
csv_default_column(
"Valtiotukimaksimi", "calculation.state_aid_max_percentage"
),
csv_default_column("Laskelman lopputulos", calculated_benefit_amount),
csv_default_column(
"Myönnetään de minimis -tukena?",
"calculation.granted_as_de_minimis_aid",
format_bool,
default_value=None,
),
csv_default_column("Päätöspäivä", "batch.decision_date"),
csv_default_column(
"Talpaan viennin päivä", self.get_completed_in_talpa_date
),
csv_default_column("Takaisinlaskutettu", self.get_alteration_amount),
]

return parent_columns + custom_columns
return columns
14 changes: 14 additions & 0 deletions backend/benefit/applications/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
AhjoRecordTitle,
AhjoRecordType,
ApplicationAlterationType,
ApplicationBatchStatus,
ApplicationStatus,
BenefitType,
DecisionType,
Expand All @@ -36,6 +37,9 @@
ApplicationAlterationCsvService,
)
from applications.services.applications_csv_report import ApplicationsCsvService
from applications.services.applications_power_bi_csv_report import (
ApplicationsPowerBiCsvService,
)
from applications.tests.factories import (
AcceptedDecisionProposalFactory,
AhjoDecisionTextFactory,
Expand Down Expand Up @@ -832,6 +836,9 @@ def decided_application_with_decision_date(application_with_ahjo_decision):
auto_generated_by_ahjo=True,
decision_date=date.today(),
)
batch.status = ApplicationBatchStatus.COMPLETED
batch.save()
application_with_ahjo_decision.pay_subsidy_percent = 100
application_with_ahjo_decision.batch = batch
application_with_ahjo_decision.save()
return application_with_ahjo_decision
Expand Down Expand Up @@ -878,6 +885,13 @@ def application_alteration(decided_application):
)


@pytest.fixture
def application_powerbi_csv_service(decided_application_with_decision_date):
return ApplicationsPowerBiCsvService(
Application.objects.filter(id=decided_application_with_decision_date.id)
)


@pytest.fixture
def decisionmaker_response():
return (
Expand Down
112 changes: 112 additions & 0 deletions backend/benefit/applications/tests/test_applications_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
PaySubsidyGranted,
)
from applications.models import AhjoSetting, ApplicationAlteration, ApplicationBatch
from applications.services.applications_csv_report import (
format_bool,
format_datetime,
get_application_origin_label,
)
from applications.tests.common import (
check_csv_cell_list_lines_generator,
check_csv_string_lines_generator,
Expand Down Expand Up @@ -358,6 +363,112 @@ def test_sensitive_data_removed_csv_output(sanitized_csv_service_with_one_applic
assert col_heading not in csv_lines[0]


def test_power_bi_report_csv_output(application_powerbi_csv_service):
csv_lines = split_lines_at_semicolon(
application_powerbi_csv_service.get_csv_string()
)

expected_headers = [
'\ufeff"Hakemusnumero"', # Ensure BOM (Byte Order Mark) is correctly handled
'"Työnantajan tyyppi"',
'"Työnantajan Y-tunnus"',
'"Helsinki-lisän määrä lopullinen"',
'"Päätöspäivä"',
'"Hakemuksen tila"',
'"Hakemuksen tyyppi"',
'"Hakemus saapunut"',
'"Haettava lisä"',
'"Haettu alkupäivä"',
'"Haettu päättymispäivä"',
'"Työnantajan yhtiömuoto"',
'"Työnantajan yhtiömuoto (YTJ-numero)"',
'"Yhdistys jolla taloudellista toimintaa?"',
'"Hakijan kieli"',
'"Palkkatuki myönnetty?"',
'"Palkkatukiprosentti"',
'"Oppisopimus?"',
'"Työntekijän kuukausipalkka (hakijalta)"',
'"Työntekijän lomaraha (hakijalta)"',
'"Työntekijän muut kulut (hakijalta)"',
'"Työntekijän työtunnit"',
'"Työntekijän syntymäpäivä"',
'"Helsinki-lisän määrä lopullinen"',
'"Laskelman alkupäivä"',
'"Laskelman päättymispäivä"',
'"Käsittelypäivä"',
'"Valtiotukimaksimi"',
'"Laskelman lopputulos"',
'"Myönnetään de minimis -tukena?"',
'"Päätöspäivä"',
'"Talpaan viennin päivä"',
'"Takaisinlaskutettu"',
]

# Assert that each column header matches
for index, header in enumerate(expected_headers):
assert (
csv_lines[0][index] == header
), f"Expected {header} but got {csv_lines[0][index]}"

applications = application_powerbi_csv_service.get_applications()

assert int(csv_lines[1][0]) == applications[0].application_number

for i, application in enumerate(applications):
# Index 1 corresponds to the first row of values (i.e., skipping the header row)
csv_row = csv_lines[i + 1] # CSV rows start at 1 (skip header)

assert int(csv_row[0]) == application.application_number
assert csv_row[1] == '"Yritys"'
assert csv_row[2] == f'"{application.company.business_id}"'
assert csv_row[3] == str((application.calculation.calculated_benefit_amount))
assert csv_row[4] == f'"{format_datetime(application.batch.decision_date)}"'
assert csv_row[5] == f'"{application.status}"'

assert (
csv_row[6]
== f'"{get_application_origin_label(application.application_origin)}"'
)
assert csv_row[7] == f'"{format_datetime(application.created_at)}"'
assert csv_row[8] == '"Työllistämisen Helsinki-lisä"'
assert csv_row[9] == f'"{str(application.start_date)}"'
assert csv_row[10] == f'"{str(application.end_date)}"'

assert csv_row[11] == f'"{application.company_form}"'
assert csv_row[12] == str(application.company_form_code)
assert (
csv_row[13]
== f'"{format_bool(application.association_has_business_activities)}"'
)
assert csv_row[14] == f'"{application.applicant_language}"'
assert csv_row[15] == f'"{str(application.pay_subsidy_granted)}"'
assert csv_row[16] == str(application.pay_subsidy_percent)
assert csv_row[17] == f'"{format_bool(application.apprenticeship_program)}"'
assert csv_row[18] == str(application.employee.monthly_pay)
assert csv_row[19] == str(application.employee.vacation_money)
assert csv_row[20] == str(application.employee.other_expenses)
assert csv_row[21] == str(application.employee.working_hours)
assert csv_row[22] == f'"{str(application.employee.birthday)}"'
assert csv_row[23] == str((application.calculation.calculated_benefit_amount))
assert csv_row[24] == f'"{str(application.calculation.start_date)}"'
assert csv_row[25] == f'"{str(application.calculation.end_date)}"'
assert csv_row[26] == f'"{format_datetime(application.handled_at)}"'
assert csv_row[27] == str(application.calculation.state_aid_max_percentage)
assert csv_row[28] == str((application.calculation.calculated_benefit_amount))
assert (
csv_row[29]
== f'"{format_bool(application.calculation.granted_as_de_minimis_aid)}"'
)
assert csv_row[30] == f'"{format_datetime(application.batch.decision_date)}"'
assert (
csv_row[31]
== f'"{str(application_powerbi_csv_service.get_completed_in_talpa_date(application))}"'
)
assert csv_row[32] == str(
application_powerbi_csv_service.get_alteration_amount(application)
)


def test_application_alteration_csv_output(
application_alteration_csv_service, settings
):
Expand Down Expand Up @@ -517,6 +628,7 @@ def test_pruned_applications_csv_output(
assert len(csv_lines[1]) == 18

assert int(csv_lines[1][0]) == application.application_number

assert csv_lines[1][1] == '"Yritys"'
assert csv_lines[1][2] == f'"{application.company_bank_account_number}"'
assert csv_lines[1][3] == f'"{application.company_name}"'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_get_power_bi_data(power_bi_client, decided_application_with_decision_da
assert len(rows) > 1
header = rows[0]
assert "Hakemusnumero" in header
assert "Työnantajan nimi" in header
assert "Talpaan viennin päivä" in header

assert rows[1][header.index("Hakemusnumero")] == str(
decided_application_with_decision_date.application_number
Expand Down

0 comments on commit 69a2421

Please sign in to comment.