Skip to content

Commit

Permalink
fix: round alteration recovery amount down (#3648)
Browse files Browse the repository at this point in the history
* fix: round alteration recovery amount down

* fix: show both instalments in csv always
  • Loading branch information
rikuke authored Dec 12, 2024
1 parent 7bae95d commit ddd8c1e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
7 changes: 6 additions & 1 deletion backend/benefit/applications/api/v1/application_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import re
from datetime import date
from decimal import Decimal, ROUND_DOWN
from typing import List

from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -500,7 +501,11 @@ def update_with_csv(self, request):
)

alteration.recovery_justification = request.data.get("recovery_justification")
alteration.recovery_amount = request.data.get("recovery_amount")
recovery_amount = Decimal(request.data.get("recovery_amount"))
alteration.recovery_amount = recovery_amount.quantize(
Decimal("1"), rounding=ROUND_DOWN
)

alteration.recovery_end_date = request.data.get("recovery_end_date")
alteration.recovery_start_date = request.data.get("recovery_start_date")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date
from decimal import Decimal, ROUND_DOWN

from django.conf import settings
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -95,6 +96,12 @@ class Meta:
help_text="The handler responsible for the cancellation of this alteration, if any",
)

def validate_recovery_amount(self, value):
if value is not None:
# Round down the recovery_amount to the nearest integer while keeping it as Decimal
return value.quantize(Decimal("1"), rounding=ROUND_DOWN)
return value

def get_application_company_name(self, obj):
return obj.application.company.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django.conf import settings
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.utils import timezone, translation
from django.utils import translation

from applications.enums import ApplicationBatchStatus, ApplicationOrigin, BenefitType
from applications.models import Application
Expand Down Expand Up @@ -115,7 +115,6 @@ def query_instalment_by_number(
"""Return the actual payable amount of the currently accepted and due instalment"""
try:
instalment = application.calculation.instalments.get(
due_date__lte=timezone.now().date(),
instalment_number=number,
)
return instalment
Expand Down Expand Up @@ -375,7 +374,8 @@ def CSV_COLUMNS(self) -> List[CsvColumn]:
)
columns.append(
csv_default_column(
"Maksettu maksuerä 2", self.get_instalment_2_amount_after_recoveries
"Maksettava maksuerä 2",
self.get_instalment_2_amount_after_recoveries,
),
)
columns.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def CSV_COLUMNS(self):
)
columns.append(
csv_default_column(
"Maksettu maksuerä 2", self.get_instalment_2_amount_after_recoveries
"Maksettava maksuerä 2",
self.get_instalment_2_amount_after_recoveries,
),
)
columns.append(
Expand Down
4 changes: 2 additions & 2 deletions backend/benefit/applications/tests/test_alteration_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def test_application_alteration_create_ignored_fields_handler(
"use_invoice": False,
"recovery_start_date": application.start_date + relativedelta(days=7),
"recovery_end_date": application.end_date,
"recovery_amount": 4000,
"recovery_amount": 4000.97,
"contact_person_name": "Ella Esimerkki",
},
)
Expand Down Expand Up @@ -504,7 +504,7 @@ def test_application_alteration_patch_handler(handler_api_client, application):
),
{
"end_date": application.start_date + relativedelta(days=12),
"recovery_amount": 4000,
"recovery_amount": 4000.45,
},
)
assert response.status_code == 200
Expand Down

0 comments on commit ddd8c1e

Please sign in to comment.