-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.py
207 lines (179 loc) · 6.15 KB
/
methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from datetime import (
date,
)
from decimal import (
Decimal,
)
from typing import (
Dict,
List,
Tuple,
Union,
)
from helpers import (
get_year_days_count,
)
class CalculateMethod:
"""
Метод расчета графика платежей по кредиту
"""
def __init__(
self,
realty_cost: Decimal,
initial_instalment: Decimal,
first_payment_date: date,
percents_rates: Dict[date, Decimal],
credit_period: int,
):
self._realty_cost = realty_cost
self._initial_instalment = initial_instalment
self._first_payment_date = first_payment_date
self._percents_rates = percents_rates
self._credit_sum = realty_cost - initial_instalment
self._credit_period = credit_period
self._result: List[Tuple[Union[str, Decimal], ...]] = []
@property
def result(self):
return self._result
def calculate(self):
"""
Метод запуска подсчета графика платежей
"""
raise NotImplementedError
class AnnuityCalculateMethod(CalculateMethod):
"""
Аннуитентный метод расчета платежей по кредиту
"""
def __init__(
self,
*args,
monthly_payment_changes: Dict[date, Decimal],
early_repayments: Dict[date, Decimal],
**kwargs,
):
super().__init__(
*args,
**kwargs,
)
self._monthly_payment_changes = monthly_payment_changes
self._early_repayments = early_repayments
self._percent_payment = Decimal()
self._date_next = None
self._changed_regular_payment = Decimal()
self._main_month_main_payment = Decimal()
self._all_payment = 0
def _calculate_percent_payment(self):
"""
Расчет суммы процентов кредита за месяц
"""
if self._first_payment_date.month == 12:
month_next = 1
year_next = self._first_payment_date.year + 1
self._date_next = date(
year=year_next,
month=month_next,
day=self._first_payment_date.day,
)
next_year_first_day = date(year_next, 1, 1)
year_prev_days_count = get_year_days_count(
year=self._first_payment_date.year,
)
year_next_days_count = get_year_days_count(year_next)
self._percents_payment = Decimal(
self._credit_sum *
self._percents_rate * (
next_year_first_day - self._first_payment_date
).days /
100 /
year_prev_days_count +
self._credit_sum *
self._percents_rate * (
self._date_next - next_year_first_day
).days /
100 /
year_next_days_count
)
else:
month_next = self._first_payment_date.month + 1
self._date_next = date(
year=self._first_payment_date.year,
month=month_next,
day=self._first_payment_date.day,
)
year_days_count = get_year_days_count(
year=self._first_payment_date.year,
)
self._percents_payment = Decimal(
self._credit_sum *
self._percents_rate * Decimal(
(self._date_next - self._first_payment_date).days /
100 /
year_days_count
)
)
def _calculate_month_payment(self):
"""
Подсчет платежа за месяц
"""
annuity_payment = Decimal(
self._credit_sum *
self._index *
pow(self._index + 1, self._credit_period) /
(pow(self._index + 1, self._credit_period) - 1)
)
if self._date_next in self._monthly_payment_changes:
self._changed_regular_payment = (
self._monthly_payment_changes[self._date_next]
)
if self._changed_regular_payment:
self._main_month_main_payment = (
self._changed_regular_payment - self._percents_payment
)
else:
self._main_month_main_payment = (
annuity_payment - self._percents_payment
)
if self._date_next in self._early_repayments:
self._main_month_main_payment = (
self._early_repayments[self._date_next] - self._percents_payment
)
self._all_payment = (
self._all_payment +
self._main_month_main_payment +
self._percents_payment
)
self._credit_sum -= self._main_month_main_payment
self._result.append(
(
self._date_next.strftime('%b/%d/%Y'),
annuity_payment,
f'{self._percents_rate:.1f}%',
self._percents_payment,
self._main_month_main_payment,
self._credit_sum,
)
)
self._first_payment_date = self._date_next
self._credit_period -= 1
def _prepare_percent_rate(self):
"""
Установка ставки по ипотеки. Возможно рефинансирование
"""
for change_percent_date in self._percents_rates.keys():
if change_percent_date <= self._first_payment_date:
self._percents_rate = self._percents_rates[change_percent_date]
else:
break
def _prepare_index(self):
self._index = Decimal(self._percents_rate / 100 / 12)
def calculate(self):
"""
Расчет графика платежей для аннуитентного метода
"""
for _ in range(self._credit_period):
self._prepare_percent_rate()
self._prepare_index()
self._calculate_percent_payment()
self._calculate_month_payment()
if self._credit_sum <= 0:
break