Skip to content

Commit

Permalink
fix: 1st instalment status accepted when created (#3543)
Browse files Browse the repository at this point in the history
* fix: 1st instalment status accepted when created

* fix: failing csv decimal test
  • Loading branch information
rikuke authored Nov 19, 2024
1 parent d3caae5 commit 7f4e4aa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
29 changes: 26 additions & 3 deletions backend/benefit/applications/tests/test_talpa_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down
17 changes: 12 additions & 5 deletions backend/benefit/calculator/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions backend/benefit/calculator/tests/test_calculator_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 7f4e4aa

Please sign in to comment.