-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.py
353 lines (311 loc) · 12.1 KB
/
dialog.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Main dialog of the loan calculator.
"""
__author__ = "Sofie & Bernd Krietenstein"
__copyright__ = "Copyright (C) 2018 Sofie & Bernd Krietenstein"
__license__ = "see LICENSE file"
import os
import pathlib
import yaml
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QHBoxLayout,
QVBoxLayout,
QGridLayout,
QLabel,
QLineEdit,
QDateEdit,
QDateTimeEdit,
QPushButton,
QFileDialog,
QDialog,
QMessageBox
)
from PyQt5.QtCore import (
QDate
)
import conf
import log
import util
from kredit import AnnuitaetenKredit
from plot import PlotWindow
from table import TableDialog
from soti import SotiDialog
class CreditSettings(object):
"""
Stores the credit settings.
"""
def __init__(self,
kreditsumme=0.0,
zins=0.0,
tilgung=0.0,
start_date="01/2000",
extra_payments=[]):
"""
C'tor.
"""
self.kreditsumme = kreditsumme
self.zins = zins
self.tilgung = tilgung
self.start_date = start_date
self.extra_payments = extra_payments
class MainDialog(QWidget):
"""
Main dialog of the loan calculator.
"""
def __init__(self):
super().__init__()
self._vbox = None
self._data_grid = None
self._hbox_buttons = None
self._hbox_close_button = None
self._kredit_summe_edit = None
self._tilgung_prozent_edit = None
self._zins_prozent_edit = None
self._start_month_edit = None
self._monats_rate_label = None
self._laufzeit_label = None
self._kosten_label = None
self._load_button = None
self._save_button = None
self._calc_button = None
self._soti_button = None
self._table_button = None
self._table_button = None
self._plot_button = None
self._close_button = None
self._window = None
self._table_window = None
self._current_project_file_name = None
self._settings = None
self._kredit_verlauf = []
self._extra_payments = []
self.init_ui()
# Load/initialize settings
self._credit_dir = os.path.join(pathlib.Path.home(), '.credit')
self._credit_settings_file = os.path.join(self._credit_dir, 'settings.yaml')
if not os.path.exists(self._credit_dir):
os.makedirs(self._credit_dir)
if not os.path.exists(self._credit_settings_file):
self._settings = CreditSettings()
self._save_settings(self._credit_settings_file)
self._load_settings(self._credit_settings_file)
def init_ui(self):
self.setGeometry(300, 300, 300, 300)
self.setWindowTitle('Annuity Loan Calculator')
self.init_layout()
self.init_project_buttons()
self.init_input_fields()
self.init_calc_buttons()
self.init_output_fields()
self.init_show_buttons()
self.init_close_button()
self._load_button.clicked.connect(self.load_button_pressed)
self._save_button.clicked.connect(self.save_button_pressed)
self._calc_button.clicked.connect(self.calc_button_pressed)
self._soti_button.clicked.connect(self.soti_button_pressed)
self._plot_button.clicked.connect(self.plot_button_pressed)
self._table_button.clicked.connect(self.table_button_pressed)
self._close_button.clicked.connect(self.close_button_pressed)
self.show()
def _load_settings(self, file_name):
if os.path.exists(file_name):
try:
self._settings = yaml.load(open(file_name, 'r'))
self._kredit_summe_edit.setText("{:.2f}".format(self._settings.kreditsumme))
self._tilgung_prozent_edit.setText("{:.2f}".format(self._settings.tilgung))
self._zins_prozent_edit.setText("{:.2f}".format(self._settings.zins))
self._start_month_edit.lineEdit().setText(self._settings.start_date)
except yaml.YAMLError as ex:
log.LOGGER.error(ex)
def _save_settings(self, file_name):
save_dir = os.path.dirname(file_name)
if os.path.exists(save_dir):
try:
self._settings.kreditsumme = float(self._kredit_summe_edit.text())
except ValueError:
self._settings.kreditsumme = 0.0
try:
self._settings.zins = float(self._zins_prozent_edit.text())
except ValueError:
self._settings.zins = 5.0
try:
self._settings.tilgung = float(self._tilgung_prozent_edit.text())
except ValueError:
self._settings.tilgung = 1.0
try:
self._settings.start_date = self._start_month_edit.lineEdit().text()
except ValueError:
self._settings.start_date = "2000-01-01"
try:
with open(file_name, mode='w') as file:
file.write(yaml.dump(self._settings))
except yaml.YAMLError as ex:
log.LOGGER.error(ex)
else:
QMessageBox.warning(
self,
"Project coud not be saved",
"Target directory {} does not exist".format(save_dir))
def init_layout(self):
self._vbox = QVBoxLayout()
self._data_grid = QGridLayout()
self._hbox_buttons = QHBoxLayout()
self._hbox_close_button = QHBoxLayout()
self._vbox.addLayout(self._data_grid)
self._vbox.addLayout(self._hbox_buttons)
self._vbox.addLayout(self._hbox_close_button)
self._vbox.addStretch(1)
self.setLayout(self._vbox)
def init_project_buttons(self):
start_row = 0
self._load_button = QPushButton("Load Project")
self._save_button = QPushButton("Save Project")
hbox = QHBoxLayout()
hbox.addWidget(self._load_button)
hbox.addWidget(self._save_button)
self._data_grid.addLayout(hbox, start_row + 0, 0, 1, 2)
def init_input_fields(self):
start_row = 1
self._data_grid.addWidget(QLabel("Loan Amount"), start_row + 0, 0)
self._data_grid.addWidget(QLabel("Down Payment [%]"), start_row + 1, 0)
self._data_grid.addWidget(QLabel("Nominal Interest [%]"), start_row + 2, 0)
self._data_grid.addWidget(QLabel("Start Date"), start_row + 3, 0)
self._kredit_summe_edit = QLineEdit()
self._tilgung_prozent_edit = QLineEdit()
self._zins_prozent_edit = QLineEdit()
self._start_month_edit = QDateEdit(QDate.currentDate())
self._start_month_edit.setDisplayFormat(conf.DATE_FORMAT)
self._start_month_edit.currentSection = QDateTimeEdit.MonthSection
# self._start_month_edit.setCalendarPopup(True)
self._data_grid.addWidget(self._kredit_summe_edit, start_row + 0, 1)
self._data_grid.addWidget(self._tilgung_prozent_edit, start_row + 1, 1)
self._data_grid.addWidget(self._zins_prozent_edit, start_row + 2, 1)
self._data_grid.addWidget(self._start_month_edit, start_row + 3, 1)
def init_calc_buttons(self):
start_row = 5
self._calc_button = QPushButton("Calculate")
self._soti_button = QPushButton("Extra Down Payments")
hbox = QHBoxLayout()
hbox.addWidget(self._calc_button)
hbox.addWidget(self._soti_button)
self._data_grid.addLayout(hbox, start_row + 0, 0, 1, 2)
def init_output_fields(self):
start_row = 6
self._monats_rate_label = QLabel("...")
self._laufzeit_label = QLabel("...")
self._kosten_label = QLabel("...")
self._data_grid.addWidget(QLabel("Monthly Rate:"), start_row + 0, 0)
self._data_grid.addWidget(QLabel("Period:"), start_row + 1, 0)
self._data_grid.addWidget(QLabel("Total Interest:"), start_row + 2, 0)
self._data_grid.addWidget(self._monats_rate_label, start_row + 0, 1)
self._data_grid.addWidget(self._laufzeit_label, start_row + 1, 1)
self._data_grid.addWidget(self._kosten_label, start_row + 2, 1)
def init_show_buttons(self):
self._table_button = QPushButton("Show Schedue")
self._plot_button = QPushButton("Show Chart")
self._hbox_buttons.addWidget(self._table_button)
self._hbox_buttons.addWidget(self._plot_button)
def init_close_button(self):
self._close_button = QPushButton("Close")
self._hbox_close_button.addStretch(1)
self._hbox_close_button.addWidget(self._close_button)
@property
def summe(self):
"""
:returns loan amount
"""
return float(self._kredit_summe_edit.text())
@property
def tilgung(self):
"""
:returns redemption rate in %
"""
return float(self._tilgung_prozent_edit.text())
@property
def zins(self):
"""
:returns nominal interest in %
"""
return float(self._zins_prozent_edit.text())
@property
def monatsrate(self):
raise NotImplementedError("Setting is not allowed")
@monatsrate.setter
def monatsrate(self, x):
self._monats_rate_label.setText("{0:.2f}".format(x))
@property
def laufzeit(self):
raise NotImplementedError("Setting is not allowed")
@laufzeit.setter
def laufzeit(self, x):
self._laufzeit_label.setText(str(x))
@property
def kosten(self):
raise NotImplementedError("Setting is not allowed")
@kosten.setter
def kosten(self, x):
"""
:param x (float): cost of the credit (sum of interest payments).
"""
self._kosten_label.setText("{0:.2f}".format(x))
def load_button_pressed(self, e):
if self._current_project_file_name:
start_file = self._current_project_file_name
else:
start_file = str(pathlib.Path.home())
file_name = QFileDialog.getOpenFileName(
self,
"Open Project File",
start_file,
"Credit Project Files (*.yaml)")[0]
self._load_settings(file_name)
self._current_project_file_name = file_name
def save_button_pressed(self, e):
if self._current_project_file_name:
start_file = self._current_project_file_name
else:
start_file = str(pathlib.Path.home())
file_name = QFileDialog.getSaveFileName(
self,
"Save Project File",
start_file,
"Credit Project Files (*.yaml)")[0]
self._save_settings(file_name)
self._current_project_file_name = file_name
def calc_button_pressed(self, e):
kredit = AnnuitaetenKredit(self.summe, self.tilgung, self.zins)
# Convert absolute to relative months
extra_payments = {}
for payment in self._settings.extra_payments:
month = util.month_diff(
QDate.fromString(payment[0], conf.DATE_FORMAT),
QDate.fromString(self._settings.start_date, conf.DATE_FORMAT))
extra_payments[month] = payment[1]
self._kredit_verlauf = kredit.berechne_kreditverlauf(extra_payments)
self.monatsrate = kredit.Monatsrate
self.laufzeit = "{0:d} Years {1:d} Months".format(
int(self._kredit_verlauf[-1].Monat) // 12,
self._kredit_verlauf[-1].Monat % 12)
self.kosten = kredit.GesamtKosten
def soti_button_pressed(self, e):
soti_dialog = SotiDialog(self, self._settings.extra_payments)
ret = soti_dialog.exec()
if ret == QDialog.Accepted:
self._settings.extra_payments = soti_dialog.payments
def table_button_pressed(self, e):
if len(self._kredit_verlauf):
table_dialog = TableDialog(self)
table_dialog.show_table(self._kredit_verlauf, self._start_month_edit.date())
table_dialog.exec()
def plot_button_pressed(self, e):
if len(self._kredit_verlauf):
plot_dialog = PlotWindow(self)
plot_dialog.plot(self._kredit_verlauf)
plot_dialog.exec()
def close_button_pressed(self):
self._save_settings(self._credit_settings_file)
self.close()