diff --git a/backend/benefit/applications/services/applications_csv_report.py b/backend/benefit/applications/services/applications_csv_report.py index b82d5c9a53..5c3a83f82c 100644 --- a/backend/benefit/applications/services/applications_csv_report.py +++ b/backend/benefit/applications/services/applications_csv_report.py @@ -130,27 +130,40 @@ def query_instalment_by_number( ) def get_instalment_1_amount(self, application: Application) -> decimal.Decimal: + """ + Return the amount that was granted for the first instalment. + """ instalment = self.query_instalment_by_number(application, 1) if instalment and instalment.amount: return instalment.amount else: return None - def get_instalment_2_amount_paid(self, application: Application) -> decimal.Decimal: + def get_instalment_2_amount_after_recoveries(self, application: Application) -> decimal.Decimal: + """ + Return the actual amount that is payable on the second instalment, + after possible alterations have been deductet. + """ instalment = self.query_instalment_by_number(application, 2) - if instalment and instalment.amount_paid: - return instalment.amount_paid + if instalment and instalment.amount_after_recoveries: + return instalment.amount_after_recoveries else: return None def get_instalment_2_amount(self, application: Application) -> decimal.Decimal: + """ + Return the amount that was granted for the second instalment. + """ instalment = self.query_instalment_by_number(application, 2) - if instalment and instalment.amount_paid: - return instalment.amount_paid + if instalment and instalment.amount: + return instalment.amount else: return None def get_instalment_2_due_date(self, application: Application) -> date: + """ + Return the due date of the second instalment. + """ instalment = self.query_instalment_by_number(application, 2) if instalment: return instalment.due_date @@ -360,7 +373,7 @@ def CSV_COLUMNS(self) -> List[CsvColumn]: ) columns.append( csv_default_column( - "Maksettu maksuerä 2", self.get_instalment_2_amount_paid + "Maksettu maksuerä 2", self.get_instalment_2_amount_after_recoveries ), ) columns.append( diff --git a/backend/benefit/applications/services/applications_power_bi_csv_report.py b/backend/benefit/applications/services/applications_power_bi_csv_report.py index 2b6ef5f917..b18fabce3a 100644 --- a/backend/benefit/applications/services/applications_power_bi_csv_report.py +++ b/backend/benefit/applications/services/applications_power_bi_csv_report.py @@ -128,7 +128,7 @@ def CSV_COLUMNS(self): ) columns.append( csv_default_column( - "Maksettu maksuerä 2", self.get_instalment_2_amount_paid + "Maksettu maksuerä 2", self.get_instalment_2_amount_after_recoveries ), ) columns.append( diff --git a/backend/benefit/applications/services/talpa_csv_service.py b/backend/benefit/applications/services/talpa_csv_service.py index 6d6c158d15..5724158daf 100644 --- a/backend/benefit/applications/services/talpa_csv_service.py +++ b/backend/benefit/applications/services/talpa_csv_service.py @@ -30,7 +30,7 @@ def get_relevant_instalment_amount( status=InstalmentStatus.ACCEPTED, due_date__lte=timezone.now().date(), ) - return instalment.amount_paid + return instalment.amount_after_recoveries except ObjectDoesNotExist: LOGGER.error( f"Valid payable Instalment not found for application {application.application_number}" @@ -40,7 +40,8 @@ def get_relevant_instalment_amount( f"Multiple payable Instalments found for application \ {application.application_number}, there should be only one" ) - return application.calculation.calculated_benefit_amount + else: + return application.calculation.calculated_benefit_amount @property def CSV_COLUMNS(self): diff --git a/backend/benefit/applications/tests/conftest.py b/backend/benefit/applications/tests/conftest.py index 6ba2a8267c..6070d46cc1 100755 --- a/backend/benefit/applications/tests/conftest.py +++ b/backend/benefit/applications/tests/conftest.py @@ -194,8 +194,7 @@ def talpa_applications_csv_service(): @pytest.fixture -def talpa_applications_csv_service_with_one_application(application_batch -): +def talpa_applications_csv_service_with_one_application(application_batch): application1 = application_batch.applications.all().first() return TalpaCsvService(Application.objects.filter(pk=application1.pk), True) diff --git a/backend/benefit/applications/tests/test_talpa_integration.py b/backend/benefit/applications/tests/test_talpa_integration.py index 6dc836a4b0..d276cbb03d 100644 --- a/backend/benefit/applications/tests/test_talpa_integration.py +++ b/backend/benefit/applications/tests/test_talpa_integration.py @@ -48,7 +48,42 @@ def test_talpa_csv_string_lines_generator(talpa_applications_csv_service): ) -def test_talpa_csv_output(talpa_applications_csv_service_with_one_application): +@pytest.mark.parametrize( + "instalments_enabled, number_of_instalments", + [ + (False, 1), + (True, 1), + (True, 2), + ], +) +def test_talpa_csv_output( + talpa_applications_csv_service_with_one_application, + instalments_enabled, + number_of_instalments, + settings, +): + settings.PAYMENT_INSTALMENTS_ENABLED = instalments_enabled + application = ( + talpa_applications_csv_service_with_one_application.applications.first() + ) + application.calculation.instalments.all().delete() + + if instalments_enabled: + for i in range(number_of_instalments): + status = InstalmentStatus.ACCEPTED + due_date = datetime.now(timezone.utc).date() + if i == 1: + status = InstalmentStatus.WAITING + due_date = timezone.now() + timedelta(days=181) + + Instalment.objects.create( + calculation=application.calculation, + amount=decimal.Decimal("123.45"), + instalment_number=i + 1, + status=status, + due_date=due_date, + ) + csv_lines = split_lines_at_semicolon( talpa_applications_csv_service_with_one_application.get_csv_string() ) @@ -65,6 +100,16 @@ def test_talpa_csv_output(talpa_applications_csv_service_with_one_application): == talpa_applications_csv_service_with_one_application.applications.first().application_number ) + if instalments_enabled: + wanted_instalment = application.calculation.instalments.get( + status=InstalmentStatus.ACCEPTED, + due_date__lte=timezone.now().date(), + ) + assert ( + decimal.Decimal(csv_lines[1][8]) + == wanted_instalment.amount_after_recoveries + ) + def test_talpa_csv_non_ascii_characters( talpa_applications_csv_service_with_one_application,