-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_function.py
More file actions
287 lines (245 loc) · 12.5 KB
/
Copy pathdb_function.py
File metadata and controls
287 lines (245 loc) · 12.5 KB
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
import db
from datetime import datetime, timedelta
def addCategory(nameOfCategory, typeOfCategory):
tableName = "Categories"
columnWillInsert = "(category_name, category_type)"
valueWillInsert = (nameOfCategory, typeOfCategory)
db.insertInfoIntoTable(tableName, columnWillInsert, valueWillInsert)
def deleteCategory(nameOfCategory):
tableName = "Categories"
db.deleteInfoIntoTable(tableName, "category_name = ?", (nameOfCategory,))
def addAccount(nameOfAccount, typeOfAccount, initial_balance = 0):
tableName = "Accounts"
columnWillInsert = "(account_name, account_type, account_balance)"
valueWillInsert = (nameOfAccount, typeOfAccount, initial_balance)
db.insertInfoIntoTable(tableName, columnWillInsert, valueWillInsert)
if initial_balance > 0:
conn = db.connectToDatabase()
cursor = conn.cursor()
cursor.execute("SELECT account_id FROM Accounts WHERE account_name = ?", (nameOfAccount,))
acc_id = cursor.fetchone()['account_id']
conn.close()
addTransaction(
description="ยอดยกมา (Initial Balance)",
category_id=13,
amount=initial_balance,
account_id=acc_id
)
def deleteAccount(nameOfAccount):
tableName = "Accounts"
db.deleteInfoIntoTable(tableName, "account_name = ?", (nameOfAccount,))
def changeValueInAccount(columnWillChange, valueWillChange, conditionOfColumn = None):
db.changeInfoIntoTable(tableName = "Accounts", tupleOfColumn_WillChange = (columnWillChange,), tupleOfInfo_WillChange_Values = (valueWillChange,), conditionOfColumn = conditionOfColumn)
def changeBalanceInAccount(balance, id):
changeValueInAccount(columnWillChange = "account_balance", valueWillChange = balance, conditionOfColumn = f"account_id = {id}")
def addTransaction(description, category_id = None, amount = 0, account_id = None, transfer_group_id = None, date_input = None):
tableName = "Transactions"
columnWillInsert = "(transaction_date, description, category_id, amount, account_id, transfer_group_id)"
if date_input is None:
record_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
else:
record_time = date_input
valueWillInsert = (record_time, description, category_id, amount, account_id, transfer_group_id)
db.insertInfoIntoTable(tableName, columnWillInsert, valueWillInsert)
def transferMoney(amount, from_acc_id, to_acc_id, desc="โอนเงิน", date_input=None):
conn = db.connectToDatabase()
cursor = conn.cursor()
try:
cursor.execute("SELECT account_name FROM Accounts WHERE account_id = ?", (from_acc_id,))
from_acc_name = cursor.fetchone()['account_name']
cursor.execute("SELECT account_name FROM Accounts WHERE account_id = ?", (to_acc_id,))
to_acc_name = cursor.fetchone()['account_name']
cursor.execute("SELECT category_id FROM Categories WHERE category_type='transfer_from'") # หรือ transfrom_from ตาม DB คุณ
cat_out_data = cursor.fetchone()
cat_out = cat_out_data['category_id'] if cat_out_data else None
cursor.execute("SELECT category_id FROM Categories WHERE category_type='transfer_to'") # หรือ transfrom_to ตาม DB คุณ
cat_in_data = cursor.fetchone()
cat_in = cat_in_data['category_id'] if cat_in_data else None
record_time = date_input if date_input else datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute("SELECT MAX(transfer_group_id) FROM Transactions")
row = cursor.fetchone()
# ถ้าไม่มีข้อมูล (None) ให้เริ่มที่ 0, ถ้ามีให้เอาค่าเดิมมา
last_group_id = row[0] if row[0] is not None else 0
new_group_id = last_group_id + 1
cursor.execute("""
INSERT INTO Transactions (transaction_date, description, category_id, amount, account_id, transfer_group_id)
VALUES (?, ?, ?, ?, ?, ?)""",
(record_time, f"โอนไป {to_acc_name} ({desc})", cat_out, -amount, from_acc_id, new_group_id))
cursor.execute("UPDATE Accounts SET account_balance = account_balance - ? WHERE account_id = ?", (amount, from_acc_id))
# [ขาเข้า]
cursor.execute("""
INSERT INTO Transactions (transaction_date, description, category_id, amount, account_id, transfer_group_id)
VALUES (?, ?, ?, ?, ?, ?)""",
(record_time, f"ได้รับจาก {from_acc_name} ({desc})", cat_in, amount, to_acc_id, new_group_id))
cursor.execute("UPDATE Accounts SET account_balance = account_balance + ? WHERE account_id = ?", (amount, to_acc_id))
conn.commit()
print(f"Transfer Success: {from_acc_name} -> {to_acc_name} ({amount})")
except Exception as e:
conn.rollback()
print(f"Transfer Failed: {e}")
finally:
conn.close()
def getTransactionsByDateRange(start_date_str, end_date_str, account_id = None):
# start_date_str: "2023-11-20"
# end_date_str: "2023-11-26"
start_full = f"{start_date_str} 00:00:00"
end_full = f"{end_date_str} 23:59:59"
conn = db.connectToDatabase()
cursor = conn.cursor()
if not account_id:
sql = """
SELECT T.transaction_id, T.transaction_date, T.description, T.amount,
C.category_name, C.category_type, A.account_name
FROM Transactions T
LEFT JOIN Categories C ON T.category_id = C.category_id
JOIN Accounts A ON T.account_id = A.account_id
WHERE T.transaction_date BETWEEN ? AND ?
ORDER BY T.transaction_date DESC
"""
cursor.execute(sql, (start_full, end_full))
else:
sql = """
SELECT T.transaction_id, T.transaction_date, T.description, T.amount,
C.category_name, C.category_type, A.account_name
FROM Transactions T
LEFT JOIN Categories C ON T.category_id = C.category_id
JOIN Accounts A ON T.account_id = A.account_id
WHERE T.account_id = ? AND T.transaction_date BETWEEN ? AND ?
ORDER BY T.transaction_date DESC
"""
cursor.execute(sql, (account_id, start_full, end_full))
result = [dict(row) for row in cursor.fetchall()]
conn.close()
return result
def deleteTransaction(transaction_id):
conn = db.connectToDatabase()
cursor = conn.cursor()
try:
cursor.execute("DELETE FROM Transactions WHERE transaction_id = ?", (transaction_id,))
conn.commit()
except Exception as e:
print(f"Error deleting: {e}")
finally:
conn.close()
def updateTransaction(transaction_id, description, amount):
conn = db.connectToDatabase()
cursor = conn.cursor()
try:
cursor.execute("""
UPDATE Transactions
SET description = ?, amount = ?
WHERE transaction_id = ?
""", (description, amount, transaction_id))
conn.commit()
except Exception as e:
print(f"Error updating: {e}")
finally:
conn.close()
def editTransactionSafe(t_id, new_desc, new_amount, new_account_id=None, new_category_id=None):
conn = db.connectToDatabase()
cursor = conn.cursor()
try:
sql_get_old = """
SELECT T.amount, T.account_id, T.category_id, C.category_type
FROM Transactions T
JOIN Categories C ON T.category_id = C.category_id
WHERE T.transaction_id = ?
"""
cursor.execute(sql_get_old, (t_id,))
old_data = cursor.fetchone()
if not old_data:
print("Error: Transaction not found")
return
old_amount = old_data['amount']
old_acc_id = old_data['account_id']
old_type = old_data['category_type'].strip().lower()
final_acc_id = new_account_id if new_account_id else old_acc_id
final_cat_id = new_category_id if new_category_id else old_data['category_id']
if old_type == 'expense' or old_type == 'transfer_from':
cursor.execute("UPDATE Accounts SET account_balance = account_balance + ? WHERE account_id = ?", (old_amount, old_acc_id))
elif old_type == 'income' or old_type == 'transfer_to':
cursor.execute("UPDATE Accounts SET account_balance = account_balance - ? WHERE account_id = ?", (old_amount, old_acc_id))
cursor.execute("SELECT category_type FROM Categories WHERE category_id = ?", (final_cat_id,))
new_cat_data = cursor.fetchone()
new_type = new_cat_data['category_type'].strip().lower()
sql_update_t = """
UPDATE Transactions
SET description = ?, amount = ?, account_id = ?, category_id = ?
WHERE transaction_id = ?
"""
cursor.execute(sql_update_t, (new_desc, new_amount, final_acc_id, final_cat_id, t_id))
if new_type == 'expense' or new_type == 'transfer_from':
cursor.execute("UPDATE Accounts SET account_balance = account_balance - ? WHERE account_id = ?", (new_amount, final_acc_id))
elif new_type == 'income' or new_type == 'transfer_to':
cursor.execute("UPDATE Accounts SET account_balance = account_balance + ? WHERE account_id = ?", (new_amount, final_acc_id))
conn.commit()
print(f"Edit Success: Reverted {old_amount} ({old_type}) -> Applied {new_amount} ({new_type})")
except Exception as e:
conn.rollback()
print(f"Edit Failed: {e}")
finally:
conn.close()
def getExpenseBreakdown():
"""ดึงข้อมูลสรุปรายจ่ายแยกตามหมวดหมู่ เรียงจากมากไปน้อย"""
conn = db.connectToDatabase()
cursor = conn.cursor()
try:
sql = """
SELECT C.category_name, SUM(T.amount) as total_amount
FROM Transactions T
JOIN Categories C ON T.category_id = C.category_id
WHERE C.category_type = 'expense' OR C.category_type = 'transfer_from'
GROUP BY C.category_name
ORDER BY total_amount DESC
"""
cursor.execute(sql)
result = cursor.fetchall()
return result
except Exception as e:
print(f"Error getting breakdown: {e}")
return []
finally:
conn.close()
def getAllAccountBalances():
"""ดึงข้อมูลบัญชีและยอดเงินล่าสุด"""
accounts = db.getDB("Accounts")
return accounts
def recordDailyWealth():
"""คำนวณและบันทึกความมั่งคั่งประจำวัน"""
conn = db.connectToDatabase()
cursor = conn.cursor()
try:
# 1. คำนวณยอดเงินรวมปัจจุบัน
# ดึง Account ทั้งหมดมาคำนวณ
cursor.execute("SELECT account_type, account_balance FROM Accounts")
accounts = cursor.fetchall()
assets = 0
liabilities = 0
for acc in accounts:
bal = acc['account_balance']
atype = acc['account_type']
# กลุ่มสินทรัพย์
if atype in ['cash', 'bank', 'saving', 'receivable', 'asset']:
assets += bal
# กลุ่มหนี้สิน (ปกติยอดเป็นลบ แต่เราจะเก็บค่าสัมบูรณ์ไว้คำนวณ)
elif atype == 'payable':
liabilities += abs(bal)
net_worth = assets - liabilities # สูตรความมั่งคั่ง
today_str = datetime.now().strftime('%Y-%m-%d')
# 2. บันทึกลง DB (Insert หรือ Replace ทับของเดิมในวันเดียวกัน)
cursor.execute("""
INSERT OR REPLACE INTO WealthHistory (record_date, total_assets, total_liabilities, net_worth)
VALUES (?, ?, ?, ?)
""", (today_str, assets, liabilities, net_worth))
conn.commit()
# print(f"Auto-recorded wealth for {today_str}: {net_worth}")
except Exception as e:
print(f"Wealth Record Error: {e}")
finally:
conn.close()
def getWealthHistoryData():
"""ดึงข้อมูลไปวาดกราฟ"""
conn = db.connectToDatabase()
cursor = conn.cursor()
cursor.execute("SELECT * FROM WealthHistory ORDER BY record_date ASC")
return cursor.fetchall()