From 0b49eb3c3b17bf0031369a04062c68ba83a56c92 Mon Sep 17 00:00:00 2001 From: rikuke <33894149+rikuke@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:47:03 +0200 Subject: [PATCH] fix: HL-1544 (#3500) * fix: first instalment amount if two instalments * feat: instalments in Django admin * fix: instalments disabled by default --- backend/benefit/calculator/admin.py | 15 +++++++++++++-- backend/benefit/calculator/rules.py | 11 +++++++++-- .../calculator/tests/test_calculator_api.py | 5 +++-- backend/benefit/helsinkibenefit/settings.py | 2 ++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/backend/benefit/calculator/admin.py b/backend/benefit/calculator/admin.py index 19c526dd78..7fbe66632a 100644 --- a/backend/benefit/calculator/admin.py +++ b/backend/benefit/calculator/admin.py @@ -3,6 +3,7 @@ from calculator.models import ( Calculation, CalculationRow, + Instalment, PaySubsidy, PreviousBenefit, TrainingCompensation, @@ -18,9 +19,15 @@ class CalculationRowInline(admin.StackedInline): list_filter = ("calculation",) +class InstalmentInline(admin.TabularInline): + model = Instalment + extra = 0 + readonly_fields = ("created_at",) + + class CalculationInline(admin.StackedInline): model = Calculation - inlines = (CalculationRowInline,) + inlines = (InstalmentInline, CalculationRowInline) show_change_link = True list_display = ( "id", @@ -38,10 +45,14 @@ class CalculationInline(admin.StackedInline): class CalculationAdmin(admin.ModelAdmin): - inlines = (CalculationRowInline,) + inlines = ( + InstalmentInline, + CalculationRowInline, + ) list_filter = ("application", "application__status", "application__company") list_display = ( "id", + "application_id", "start_date", "end_date", "calculated_benefit_amount", diff --git a/backend/benefit/calculator/rules.py b/backend/benefit/calculator/rules.py index fd44b96bb5..6ce3a5df12 100644 --- a/backend/benefit/calculator/rules.py +++ b/backend/benefit/calculator/rules.py @@ -44,6 +44,7 @@ def __init__(self, calculation: Calculation): self.calculation = calculation self._row_counter = 0 self.instalment_threshold = settings.INSTALMENT_THRESHOLD + self.first_instalment_limit = settings.FIRST_INSTALMENT_LIMIT def _get_change_days( self, pay_subsidies, training_compensations, start_date, end_date @@ -172,13 +173,19 @@ def _calculate_instalment_amounts( ) -> list[tuple[int, decimal.Decimal, datetime.datetime]]: """Calculate the number of instalments and their amounts based on the total benefit. Returns a list of tuples containing (instalment_number, amount, due_date). + If the total benefit is less or equal to the instalment threshold, a single instalment is created. + If not, two instalments are created, with the amount of + the first instalment being equal to the FIRST_INSTALMENT_LIMIT, + 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())] - second_instalment_amount = total_benefit_amount - self.instalment_threshold + first_instalment_amount = self.first_instalment_limit + second_instalment_amount = total_benefit_amount - first_instalment_amount + return [ - (1, self.instalment_threshold, timezone.now()), + (1, first_instalment_amount, timezone.now()), ( 2, second_instalment_amount, diff --git a/backend/benefit/calculator/tests/test_calculator_api.py b/backend/benefit/calculator/tests/test_calculator_api.py index a83fadb885..8126be48e4 100644 --- a/backend/benefit/calculator/tests/test_calculator_api.py +++ b/backend/benefit/calculator/tests/test_calculator_api.py @@ -707,6 +707,7 @@ def test_application_calculation_instalments( settings.PAYMENT_INSTALMENTS_ENABLED = True settings.INSTALMENT_THRESHOLD = 9600 settings.SALARY_BENEFIT_NEW_MAX = 1500 + settings.FIRST_INSTALMENT_LIMIT = 9000 if has_subsidies is False: handling_application.pay_subsidies.all().delete() @@ -742,12 +743,12 @@ def test_application_calculation_instalments( ) if number_of_instalments == 2: - assert instalment_1.amount == decimal.Decimal(settings.INSTALMENT_THRESHOLD) + assert instalment_1.amount == decimal.Decimal(settings.FIRST_INSTALMENT_LIMIT) instalment_2 = handling_application.calculation.instalments.all()[1] assert ( instalment_2.amount == handling_application.calculation.calculated_benefit_amount - - decimal.Decimal(settings.INSTALMENT_THRESHOLD) + - decimal.Decimal(settings.FIRST_INSTALMENT_LIMIT) ) due_date = instalment_2.due_date diff --git a/backend/benefit/helsinkibenefit/settings.py b/backend/benefit/helsinkibenefit/settings.py index f3408c4de8..6f674f5022 100644 --- a/backend/benefit/helsinkibenefit/settings.py +++ b/backend/benefit/helsinkibenefit/settings.py @@ -180,6 +180,7 @@ PAYMENT_INSTALMENTS_ENABLED=(bool, False), SALARY_BENEFIT_NEW_MAX=(int, 1500), INSTALMENT_THRESHOLD=(int, 9600), + FIRST_INSTALMENT_LIMIT=(int, 9000), ) if os.path.exists(env_file): env.read_env(env_file) @@ -570,3 +571,4 @@ PAYMENT_INSTALMENTS_ENABLED = env.bool("PAYMENT_INSTALMENTS_ENABLED") SALARY_BENEFIT_NEW_MAX = env.int("SALARY_BENEFIT_NEW_MAX") INSTALMENT_THRESHOLD = env.int("INSTALMENT_THRESHOLD") +FIRST_INSTALMENT_LIMIT = env.int("FIRST_INSTALMENT_LIMIT")