Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] MVJ-610 Setting start point figure for rent's tasotarkistus #799

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions leasing/models/rent.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,25 @@ def is_active_on_period(self, date_range_start, date_range_end):
return False

def set_start_price_index_point_figure(self):
last_year_figure = None
latest_figure = None

if self.old_dwellings_in_housing_companies_price_index:
start_index_number_yearly = IndexPointFigureYearly.objects.get(
all_index_point_figures = IndexPointFigureYearly.objects.filter(
index=self.old_dwellings_in_housing_companies_price_index,
year=self.lease.start_date.year - 1,
)
self.start_price_index_point_figure = start_index_number_yearly.value
).order_by("-year")

last_year_figure = all_index_point_figures.filter(
year=self.lease.start_date.year - 1
).first()
latest_figure = all_index_point_figures.first()

if last_year_figure is not None and last_year_figure.value:
self.start_price_index_point_figure = last_year_figure.value
elif latest_figure is not None and latest_figure.value:
self.start_price_index_point_figure = latest_figure.value
else:
self.start_price_index_point_figure = None


class RentDueDate(TimeStampedSafeDeleteModel):
Expand Down
14 changes: 14 additions & 0 deletions leasing/serializers/rent.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class Meta:
"manual_ratio_previous",
"override_receivable_type",
"old_dwellings_in_housing_companies_price_index",
"start_price_index_point_figure",
"old_dwellings_in_housing_companies_price_index_type",
)

Expand Down Expand Up @@ -571,6 +572,19 @@ class Meta:
"old_dwellings_in_housing_companies_price_index_type",
)

def update(self, instance, validated_data):

if (
instance.old_dwellings_in_housing_companies_price_index is None
and validated_data.get("old_dwellings_in_housing_companies_price_index")
is not None
):
instance.old_dwellings_in_housing_companies_price_index = (
validated_data.get("old_dwellings_in_housing_companies_price_index")
)
instance.set_start_price_index_point_figure()
return super().update(instance, validated_data)

def validate(self, rent_data: dict):
self.validate_override_receivable_type_value(rent_data)
return rent_data
Expand Down
35 changes: 35 additions & 0 deletions leasing/tests/models/test_rent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2887,3 +2887,38 @@ def test_set_start_price_index_point_figure_with_last_year_index(
rent.set_start_price_index_point_figure()

assert rent.start_price_index_point_figure == expected_point_figure.value


@pytest.mark.django_db
def test_set_start_price_index_point_figure_last_year_index_not_found(
rent_factory,
lease_factory,
old_dwellings_in_housing_companies_price_index_factory,
index_point_figure_yearly_factory,
):
"""The point figure should be set to the latest available index
if the year previous to the LEASE's start date is not found."""
lease = lease_factory(start_date=date(year=2026, month=1, day=1))
old_dwellings_in_housing_companies_price_index = (
old_dwellings_in_housing_companies_price_index_factory()
)
index_point_figure_yearly_factory(
value=101, year=2021, index=old_dwellings_in_housing_companies_price_index
)
index_point_figure_yearly_factory(
value=102, year=2022, index=old_dwellings_in_housing_companies_price_index
)
index_point_figure_yearly_factory(
value=103, year=2023, index=old_dwellings_in_housing_companies_price_index
)
expected_point_figure = index_point_figure_yearly_factory(
value=104, year=2024, index=old_dwellings_in_housing_companies_price_index
)
rent: Rent = rent_factory(
lease=lease,
old_dwellings_in_housing_companies_price_index=old_dwellings_in_housing_companies_price_index,
)

rent.set_start_price_index_point_figure()

assert rent.start_price_index_point_figure == expected_point_figure.value
Loading