From 7f4e4aaf1a4c700f63a3f15338eaf14daf1a7d7d Mon Sep 17 00:00:00 2001 From: rikuke <33894149+rikuke@users.noreply.github.com> Date: Tue, 19 Nov 2024 08:07:37 +0200 Subject: [PATCH] fix: 1st instalment status accepted when created (#3543) * fix: 1st instalment status accepted when created * fix: failing csv decimal test --- .../tests/test_talpa_integration.py | 29 +++++++++++++++++-- backend/benefit/calculator/rules.py | 17 +++++++---- .../calculator/tests/test_calculator_api.py | 6 ++++ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/backend/benefit/applications/tests/test_talpa_integration.py b/backend/benefit/applications/tests/test_talpa_integration.py index 76692bb354..12643599df 100644 --- a/backend/benefit/applications/tests/test_talpa_integration.py +++ b/backend/benefit/applications/tests/test_talpa_integration.py @@ -87,12 +87,35 @@ def test_talpa_csv_delimiter(pruned_applications_csv_service_with_one_applicatio ) -def test_talpa_csv_decimal(pruned_applications_csv_service_with_one_application): +@pytest.mark.parametrize( + "instalments_enabled", + [ + (False,), + (True,), + ], +) +def test_talpa_csv_decimal( + pruned_applications_csv_service_with_one_application, + settings, + instalments_enabled, +): + settings.PAYMENT_INSTALMENTS_ENABLED = instalments_enabled application = ( pruned_applications_csv_service_with_one_application.applications.first() ) - application.calculation.calculated_benefit_amount = decimal.Decimal("123.45") - application.calculation.save() + if instalments_enabled: + application.calculation.instalments.all().delete() + Instalment.objects.create( + calculation=application.calculation, + amount=decimal.Decimal("123.45"), + instalment_number=1, + status=InstalmentStatus.ACCEPTED, + due_date=datetime.now(timezone.utc).date(), + ) + else: + application.calculation.calculated_benefit_amount = decimal.Decimal("123.45") + application.calculation.save() + csv_lines = split_lines_at_semicolon( pruned_applications_csv_service_with_one_application.get_csv_string() ) diff --git a/backend/benefit/calculator/rules.py b/backend/benefit/calculator/rules.py index 6ce3a5df12..0ab9230e63 100644 --- a/backend/benefit/calculator/rules.py +++ b/backend/benefit/calculator/rules.py @@ -9,7 +9,7 @@ from django.utils import timezone from applications.enums import ApplicationStatus, BenefitType, PaySubsidyGranted -from calculator.enums import DescriptionType, RowType +from calculator.enums import DescriptionType, InstalmentStatus, RowType from calculator.models import ( Calculation, CalculationRow, @@ -179,30 +179,37 @@ def _calculate_instalment_amounts( and the second being equal to the rest of the total. """ if total_benefit_amount <= self.instalment_threshold: - return [(1, total_benefit_amount, timezone.now())] + return [ + (1, total_benefit_amount, timezone.now(), InstalmentStatus.ACCEPTED) + ] first_instalment_amount = self.first_instalment_limit second_instalment_amount = total_benefit_amount - first_instalment_amount return [ - (1, first_instalment_amount, timezone.now()), + (1, first_instalment_amount, timezone.now(), InstalmentStatus.ACCEPTED), ( 2, second_instalment_amount, timezone.now() + datetime.timedelta(days=181), + InstalmentStatus.WAITING, ), ] def _create_instalments( - self, instalments: list[tuple[int, decimal.Decimal, datetime.datetime]] + self, + instalments: list[ + tuple[int, decimal.Decimal, datetime.datetime, InstalmentStatus] + ], ) -> None: """Create instalment objects from the provided instalment data.""" - for instalment_number, amount, due_date in instalments: + for instalment_number, amount, due_date, status in instalments: Instalment.objects.create( calculation=self.calculation, instalment_number=instalment_number, amount=amount, due_date=due_date, + status=status, ) @transaction.atomic diff --git a/backend/benefit/calculator/tests/test_calculator_api.py b/backend/benefit/calculator/tests/test_calculator_api.py index 8126be48e4..463fafbb2c 100644 --- a/backend/benefit/calculator/tests/test_calculator_api.py +++ b/backend/benefit/calculator/tests/test_calculator_api.py @@ -25,6 +25,7 @@ get_handler_detail_url, ) from calculator.api.v1.serializers import CalculationSerializer +from calculator.enums import InstalmentStatus from calculator.tests.factories import CalculationFactory, PaySubsidyFactory from common.tests.conftest import get_client_user from common.utils import duration_in_months, to_decimal @@ -728,6 +729,7 @@ def test_application_calculation_instalments( instalment_1 = handling_application.calculation.instalments.all()[0] assert instalment_1.due_date is not None + assert instalment_1.status == InstalmentStatus.ACCEPTED due_date = instalment_1.due_date now_date = timezone.now().date() @@ -741,15 +743,19 @@ def test_application_calculation_instalments( instalment_1.amount == handling_application.calculation.calculated_benefit_amount ) + assert instalment_1.status == InstalmentStatus.ACCEPTED if number_of_instalments == 2: assert instalment_1.amount == decimal.Decimal(settings.FIRST_INSTALMENT_LIMIT) + assert instalment_1.status == InstalmentStatus.ACCEPTED + instalment_2 = handling_application.calculation.instalments.all()[1] assert ( instalment_2.amount == handling_application.calculation.calculated_benefit_amount - decimal.Decimal(settings.FIRST_INSTALMENT_LIMIT) ) + assert instalment_2.status == InstalmentStatus.WAITING due_date = instalment_2.due_date future_date = timezone.now() + timedelta(days=181)