Skip to content

Commit

Permalink
fix: HL-1544 (#3500)
Browse files Browse the repository at this point in the history
* fix: first instalment amount if two instalments

* feat: instalments in Django admin

* fix: instalments disabled by default
  • Loading branch information
rikuke authored Nov 5, 2024
1 parent d405af5 commit 0b49eb3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
15 changes: 13 additions & 2 deletions backend/benefit/calculator/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from calculator.models import (
Calculation,
CalculationRow,
Instalment,
PaySubsidy,
PreviousBenefit,
TrainingCompensation,
Expand All @@ -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",
Expand All @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions backend/benefit/calculator/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions backend/benefit/calculator/tests/test_calculator_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions backend/benefit/helsinkibenefit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")

0 comments on commit 0b49eb3

Please sign in to comment.