-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
209 lines (177 loc) · 7.77 KB
/
model.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
# Copyright (C) 2020 - 2021 Pakeetharan Balasupramaniyam <pakee@gmail.com>
# Copyright (C) 2020 - 2021 Mohamed Zumair <mhdzumair@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from os import unlink
from os.path import abspath
from sqlite3 import connect
import datetime
from tempfile import NamedTemporaryFile
from ds_exception import NotEnoughMoney
def singleton(class_):
class SingletonClass(class_):
_instance = None
def __new__(cls, *args, **kwargs):
if SingletonClass._instance is None:
SingletonClass._instance = super(SingletonClass, cls).__new__(
cls, *args, **kwargs
)
SingletonClass._instance._sealed = False
return SingletonClass._instance
def __init__(self, *args, **kwargs):
if self._sealed:
return
super(SingletonClass, self).__init__(*args, **kwargs)
self._sealed = True
SingletonClass.__name__ = class_.__name__
return SingletonClass
@singleton
class Model:
def __init__(self):
self.connection = connect("account.db")
self.cursor = self.connection.cursor()
self.pdf_file = NamedTemporaryFile(suffix=".pdf", delete=False)
self.pdf_js = abspath("inc/pdfjs/web/viewer.html")
def data_entry(
self,
trans_type,
method_of_trans,
description,
date,
source_table,
cheque_number,
amount: float,
):
self.cursor.execute(
f"INSERT INTO {'INCOME' if trans_type == 'Income' else 'EXPENSE'} "
" VALUES (:name,:desc, julianday(:date), :money_type, :cheque_number, :amount)",
{
"name": method_of_trans,
"desc": description,
"date": date,
"money_type": source_table,
"cheque_number": cheque_number,
"amount": amount,
},
)
self.connection.commit()
if trans_type == "Income":
symbol = "+"
else:
symbol = "-"
self.source_update_entry(source_table, symbol, date, amount)
def source_update_entry(self, source_table, symbol, date, amount):
self.cursor.execute(
f"UPDATE {source_table} SET balance=balance {symbol} :amount WHERE date >= julianday(:date);",
{"amount": amount, "date": date},
)
return self.connection.commit()
def bank_update(self, trans_type, date, amount: float, source_table):
if trans_type == "Bank Deposit":
self.source_update_entry(source_table, "+", date, amount)
self.source_update_entry("CASH", "-", date, amount)
elif trans_type == "Bank Withdraw":
self.source_update_entry("CASH", "+", date, amount)
self.source_update_entry(source_table, "-", date, amount)
self.connection.commit()
def get_records(self, start_date, end_date, level):
if level == "1":
self.cursor.execute(
"SELECT method_of_trans, money_type, amount from INCOME WHERE "
"date BETWEEN julianday(:startdate) AND julianday(:enddate) ORDER BY method_of_trans",
{"startdate": start_date, "enddate": end_date},
)
income_entries = self.cursor.fetchall()
self.cursor.execute(
"SELECT method_of_trans, money_type, amount from EXPENSE WHERE "
"date BETWEEN julianday(:startdate) AND julianday(:enddate) ORDER BY method_of_trans",
{"startdate": start_date, "enddate": end_date},
)
expense_entries = self.cursor.fetchall()
income_entries = sorting(income_entries)
expense_entries = sorting(expense_entries)
else:
self.cursor.execute(
"SELECT method_of_trans, description, date(date), money_type, cheque_number, amount "
"from INCOME WHERE date BETWEEN julianday(:startdate) AND julianday(:enddate) ORDER BY date",
{"startdate": start_date, "enddate": end_date},
)
income_entries = self.cursor.fetchall()
self.cursor.execute(
"SELECT method_of_trans, description, date(date), money_type, cheque_number, amount "
"from EXPENSE WHERE date BETWEEN julianday(:startdate) AND julianday(:enddate) ORDER BY date",
{"startdate": start_date, "enddate": end_date},
)
expense_entries = self.cursor.fetchall()
return (income_entries, expense_entries)
def get_balance(self, start_date, end_date):
self.cursor.execute(
"SELECT date(date), balance FROM BANK WHERE date BETWEEN "
"julianday(:startdate) AND julianday(:enddate) ORDER BY date DESC LIMIT 1",
{"startdate": start_date, "enddate": end_date},
)
final_bank_balance = self.cursor.fetchone()
self.cursor.execute(
"SELECT date(date), balance FROM CASH WHERE date BETWEEN "
"julianday(:startdate) AND julianday(:enddate) ORDER BY date DESC LIMIT 1",
{"startdate": start_date, "enddate": end_date},
)
final_cash_balance = self.cursor.fetchone()
self.cursor.execute(
"SELECT date(date), balance FROM BUILDING WHERE date BETWEEN "
"julianday(:startdate) AND julianday(:enddate) ORDER BY date DESC LIMIT 1",
{"startdate": start_date, "enddate": end_date},
)
final_building_balance = self.cursor.fetchone()
return (final_bank_balance, final_cash_balance, final_building_balance)
def close_connection(self):
self.pdf_file.close()
unlink(self.pdf_file.name)
self.connection.close()
@classmethod
def fill_upto_today_balance(cls, source_table):
connection = connect("account.db")
cursor = connection.cursor()
cursor.execute(f"SELECT date(MAX(date)), balance FROM {source_table}")
last_entry_date, last_entry_money = cursor.fetchone()
today = str(datetime.date.today())
if last_entry_date == today:
return
start = datetime.datetime.strptime(last_entry_date, "%Y-%m-%d")
end = datetime.datetime.strptime(today, "%Y-%m-%d")
end += datetime.timedelta(1)
start += datetime.timedelta(1)
dates = [
start + datetime.timedelta(days=x) for x in range(0, (end - start).days)
]
for date in dates:
date = date.strftime("%Y-%m-%d")
cursor.execute(
f"INSERT INTO {source_table} (date, balance) VALUES (julianday(:date), :balance)",
{"date": date, "balance": last_entry_money},
)
connection.commit()
def sorting(entries):
sorted_entries = []
for entry in entries:
index = None
for row in sorted_entries:
if entry[0] == row[0] and entry[1] == row[1]:
index = sorted_entries.index(row)
break
if index is not None:
sorted_entries[index][2] += entry[2]
else:
sorted_entries.append(list(entry))
return sorted_entries