Skip to content

Commit

Permalink
fix: use instalment_after_recoveries property
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke committed Dec 5, 2024
1 parent e44b8dd commit f1c707e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
25 changes: 19 additions & 6 deletions backend/benefit/applications/services/applications_csv_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,40 @@ def query_instalment_by_number(
)

def get_instalment_1_amount(self, application: Application) -> decimal.Decimal:
"""
Return the amount that was granted for the first instalment.
"""
instalment = self.query_instalment_by_number(application, 1)
if instalment and instalment.amount:
return instalment.amount
else:
return None

def get_instalment_2_amount_paid(self, application: Application) -> decimal.Decimal:
def get_instalment_2_amount_after_recoveries(self, application: Application) -> decimal.Decimal:
"""
Return the actual amount that is payable on the second instalment,
after possible alterations have been deductet.
"""
instalment = self.query_instalment_by_number(application, 2)
if instalment and instalment.amount_paid:
return instalment.amount_paid
if instalment and instalment.amount_after_recoveries:
return instalment.amount_after_recoveries
else:
return None

def get_instalment_2_amount(self, application: Application) -> decimal.Decimal:
"""
Return the amount that was granted for the second instalment.
"""
instalment = self.query_instalment_by_number(application, 2)
if instalment and instalment.amount_paid:
return instalment.amount_paid
if instalment and instalment.amount:
return instalment.amount
else:
return None

def get_instalment_2_due_date(self, application: Application) -> date:
"""
Return the due date of the second instalment.
"""
instalment = self.query_instalment_by_number(application, 2)
if instalment:
return instalment.due_date
Expand Down Expand Up @@ -360,7 +373,7 @@ def CSV_COLUMNS(self) -> List[CsvColumn]:
)
columns.append(
csv_default_column(
"Maksettu maksuerä 2", self.get_instalment_2_amount_paid
"Maksettu 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,7 @@ def CSV_COLUMNS(self):
)
columns.append(
csv_default_column(
"Maksettu maksuerä 2", self.get_instalment_2_amount_paid
"Maksettu maksuerä 2", self.get_instalment_2_amount_after_recoveries
),
)
columns.append(
Expand Down
5 changes: 3 additions & 2 deletions backend/benefit/applications/services/talpa_csv_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_relevant_instalment_amount(
status=InstalmentStatus.ACCEPTED,
due_date__lte=timezone.now().date(),
)
return instalment.amount_paid
return instalment.amount_after_recoveries
except ObjectDoesNotExist:
LOGGER.error(
f"Valid payable Instalment not found for application {application.application_number}"
Expand All @@ -40,7 +40,8 @@ def get_relevant_instalment_amount(
f"Multiple payable Instalments found for application \
{application.application_number}, there should be only one"
)
return application.calculation.calculated_benefit_amount
else:
return application.calculation.calculated_benefit_amount

@property
def CSV_COLUMNS(self):
Expand Down
3 changes: 1 addition & 2 deletions backend/benefit/applications/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ def talpa_applications_csv_service():


@pytest.fixture
def talpa_applications_csv_service_with_one_application(application_batch
):
def talpa_applications_csv_service_with_one_application(application_batch):
application1 = application_batch.applications.all().first()

return TalpaCsvService(Application.objects.filter(pk=application1.pk), True)
Expand Down
47 changes: 46 additions & 1 deletion backend/benefit/applications/tests/test_talpa_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,42 @@ def test_talpa_csv_string_lines_generator(talpa_applications_csv_service):
)


def test_talpa_csv_output(talpa_applications_csv_service_with_one_application):
@pytest.mark.parametrize(
"instalments_enabled, number_of_instalments",
[
(False, 1),
(True, 1),
(True, 2),
],
)
def test_talpa_csv_output(
talpa_applications_csv_service_with_one_application,
instalments_enabled,
number_of_instalments,
settings,
):
settings.PAYMENT_INSTALMENTS_ENABLED = instalments_enabled
application = (
talpa_applications_csv_service_with_one_application.applications.first()
)
application.calculation.instalments.all().delete()

if instalments_enabled:
for i in range(number_of_instalments):
status = InstalmentStatus.ACCEPTED
due_date = datetime.now(timezone.utc).date()
if i == 1:
status = InstalmentStatus.WAITING
due_date = timezone.now() + timedelta(days=181)

Instalment.objects.create(
calculation=application.calculation,
amount=decimal.Decimal("123.45"),
instalment_number=i + 1,
status=status,
due_date=due_date,
)

csv_lines = split_lines_at_semicolon(
talpa_applications_csv_service_with_one_application.get_csv_string()
)
Expand All @@ -65,6 +100,16 @@ def test_talpa_csv_output(talpa_applications_csv_service_with_one_application):
== talpa_applications_csv_service_with_one_application.applications.first().application_number
)

if instalments_enabled:
wanted_instalment = application.calculation.instalments.get(
status=InstalmentStatus.ACCEPTED,
due_date__lte=timezone.now().date(),
)
assert (
decimal.Decimal(csv_lines[1][8])
== wanted_instalment.amount_after_recoveries
)


def test_talpa_csv_non_ascii_characters(
talpa_applications_csv_service_with_one_application,
Expand Down

0 comments on commit f1c707e

Please sign in to comment.