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

a407828329451165 #64

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions openprocurement/tender/esco/npv_calculation.py
Expand Up @@ -61,3 +61,26 @@ def calculate_days_with_cost_reduction(
):
first_year_days = (date(announcement_date.year, 12, 31) - announcement_date).days
return [first_year_days] + [days_per_year] * NPV_CALCULATION_DURATION


def calculate_days_for_discount_rate(days_with_cost_reduction):
days = days_with_cost_reduction[:-1]
days.append(DAYS_PER_YEAR - days[0])
return days


def calculate_days_with_payments(
contract_duration,
first_year_days,
days_per_year=DAYS_PER_YEAR
):
days = [min(contract_duration, first_year_days)]
contract_duration -= days[0]
days += [days_per_year] * (contract_duration // days_per_year) + [contract_duration % days_per_year]
if (len(days) < NPV_CALCULATION_DURATION):
days += [0] * (NPV_CALCULATION_DURATION + 1 - len(days))
return days


def calculate_income(client_cost_reductions, client_payments):
return [client_cost_reductions[i] - client_payments[i] for i in range(len(client_cost_reductions))]
7 changes: 7 additions & 0 deletions openprocurement/tender/esco/tests/npv.py
Expand Up @@ -7,6 +7,9 @@
discount_rates,
discounted_income,
days_with_cost_reduction,
days_for_discount_rate,
days_with_payments,
income,
)


Expand All @@ -23,6 +26,10 @@ class NPVCalculationTest(unittest.TestCase):
test_discounted_income = snitch(discounted_income)
test_days_with_cost_reduction = snitch(days_with_cost_reduction)

test_days_for_discount_rate = snitch(days_for_discount_rate)
test_days_with_payments = snitch(days_with_payments)
test_income = snitch(income)


def suite():
suite = unittest.TestSuite()
Expand Down
39 changes: 37 additions & 2 deletions openprocurement/tender/esco/tests/npv_blanks.py
Expand Up @@ -5,6 +5,9 @@
DISCOUNTED_INCOME_COEF,
INCOME_CUSTOMER,
DISCOUNTED_INCOME_RES,
COST_REDUCTIONS,
PAYMENTS,
EXPECTED_INCOME,
)
from openprocurement.tender.esco.constants import DAYS_PER_YEAR, NPV_CALCULATION_DURATION
from openprocurement.tender.esco.npv_calculation import (
Expand All @@ -14,6 +17,9 @@
calculate_discounted_income,
calculate_discount_coef,
calculate_days_with_cost_reduction,
calculate_days_for_discount_rate,
calculate_days_with_payments,
calculate_income,
)

nbu_rate = 0.22
Expand Down Expand Up @@ -146,14 +152,43 @@ def days_with_cost_reduction(self):
[135] + [365] * NPV_CALCULATION_DURATION
)

announcement_date = date(2020, 01, 20)
announcement_date = date(2020, 1, 20)
self.assertEqual(
calculate_days_with_cost_reduction(announcement_date, DAYS_PER_YEAR),
[346] + [365] * NPV_CALCULATION_DURATION
)

announcement_date = date(2019, 01, 20)
announcement_date = date(2019, 1, 20)
self.assertEqual(
calculate_days_with_cost_reduction(announcement_date, DAYS_PER_YEAR),
[345] + [365] * NPV_CALCULATION_DURATION
)


def days_for_discount_rate(self):
days = calculate_days_for_discount_rate([135] + [365] * NPV_CALCULATION_DURATION)
# (NPV_CALCULATION_DURATION - 1) is a number of full years
expected_days = [135] + [365] * (NPV_CALCULATION_DURATION - 1) + [230]
self.assertEqual(days, expected_days)

days = calculate_days_for_discount_rate([346] + [365] * NPV_CALCULATION_DURATION)
expected_days = [346] + [365] * (NPV_CALCULATION_DURATION - 1) + [19]
self.assertEqual(days, expected_days)

days = calculate_days_for_discount_rate([345] + [365] * NPV_CALCULATION_DURATION)
expected_days = [345] + [365] * (NPV_CALCULATION_DURATION - 1) + [20]
self.assertEqual(days, expected_days)


def days_with_payments(self):
days = calculate_days_with_payments(830, 135)
expected_days = [135, 365, 330] + [0] * 18
self.assertEqual(days, expected_days)


def income(self):
client_cost_reductions = COST_REDUCTIONS['first_test']
client_payments = PAYMENTS['first_test']
client_income = calculate_income(client_cost_reductions, client_payments)
expected_client_income = EXPECTED_INCOME['first_test']
self.assertEqual(client_income, expected_client_income)
3 changes: 3 additions & 0 deletions openprocurement/tender/esco/tests/npv_test_data.py
Expand Up @@ -64,3 +64,6 @@

}

COST_REDUCTIONS = {'first_test':[Fraction("92.47")] + [Fraction("250.0")] * 20}
PAYMENTS = {'first_test':[Fraction("64.73"), Fraction("175.0"), Fraction("175.0"), Fraction("110.27")] + [Fraction("0.0")] * 17}
EXPECTED_INCOME = {'first_test':[Fraction("27.74"), Fraction("75.0"), Fraction("75.0"), Fraction("139.73")] + [Fraction("250.0")] * 17}