-
Notifications
You must be signed in to change notification settings - Fork 0
/
e_expenses.py
683 lines (526 loc) · 24.2 KB
/
e_expenses.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
import requests
import psycopg2
import re
import calendar
import os
import copy
import e_account
import e_categories
from flask_sqlalchemy import sqlalchemy
from flask import Flask, jsonify, redirect, render_template, request, session
from flask_session import Session
from sqlalchemy.orm import scoped_session, sessionmaker
from dotenv import load_dotenv
from datetime import datetime
from saviour import apology, login_required, convertSQLToDict
from werkzeug.security import check_password_hash, generate_password_hash
from sqlalchemy import create_engine
load_dotenv()
# PostgreSQL Database credentials loaded from the .env file
DATABASE = os.getenv("DATABASE")
DATABASE_USERNAME = os.getenv("DATABASE_USERNAME")
DATABASE_PASSWORD = os.getenv("DATABASE_PASSWORD")
app = Flask(__name__)
# Configure session to use filesystem (instead of signed cookies) Cookies are sometimes helpfull but mostly bad 😂
# app.config["SESSION_FILE_DIR"] = mkdtemp() # only remove comment when testing locally for benefit of temp directories
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Create engine object to manage connections to DB, and scoped session to separate user interactions with DB
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
# Expense functions
# Add Expenses
def addExpenses(formData, userID):
expenses = []
expense = {"description": None, "category": None, "date": None, "amount": None}
# expenses.append(expense)
print(expenses)
# Check for the location from where addexepense is being executed
if "." not in formData[0][0]:
for key, value in formData:
expense[key] = value.strip()
expense["amount"] = float(expense["amount"])
expenses.append(expense)
else:
counter = 0
for key, value in formData:
cleanKey = key.split(".")
expense[cleanKey[0]] = value.strip()
counter += 1
# Every 4 loops add the expense to the list of expenses (because there are 4 fields for an expense record)
if counter % 4 == 0:
# Store the amount as a float
expense["amount"] = float(expense["amount"])
# Add dictionary to list
expenses.append(expense.copy())
for expense in expenses:
now = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
db.execute(
"insert into expenses (description, category, expenseDate, amount, submitTime, user_id) values (:description, :category, :expenseDate, :amount, :submitTime, :usersID)",
{
"description": expense["description"],
"category": expense["category"],
"expenseDate": expense["date"],
"amount": expense["amount"],
"submitTime": now,
"usersID": userID,
},
)
db.commit()
return expenses
def getTotalSpend_Year(userID):
results = db.execute(
"SELECT SUM(amount) AS expenses_year FROM expenses WHERE user_id = :usersID AND date_part('year', date(expensedate)) = date_part('year', CURRENT_DATE)",
{"usersID": userID},
# Source = https://www.postgresqltutorial.com/postgresql-date_part/
).fetchall()
totalSpendYear = convertSQLToDict(results)
return totalSpendYear[0]["expenses_year"]
def getTotalSpend_Month(userID):
results = db.execute(
"SELECT SUM(amount) AS expenses_month FROM expenses WHERE user_id = :usersID AND date_part('year', date(expensedate)) = date_part('year', CURRENT_DATE) AND date_part('month', date(expensedate)) = date_part('month', CURRENT_DATE)",
{"usersID": userID},
).fetchall()
totalSpendMonth = convertSQLToDict(results)
return totalSpendMonth[0]["expenses_month"]
def getTotalSpend_Week(userID):
# Query note: Day 0 of a week == Sunday. This query grabs expenses between the *current* weeks Monday and Sunday.
results = db.execute(
"SELECT SUM(amount) AS expenses_week FROM expenses WHERE user_id = :usersID AND date_part('year', date(expensedate)) = date_part('year', CURRENT_DATE) AND date_part('week', date(expensedate)) = date_part('week', CURRENT_DATE)",
{"usersID": userID},
).fetchall()
totalSpendWeek = convertSQLToDict(results)
return totalSpendWeek[0]["expenses_week"]
def getIncome(userID):
income = db.execute("SELECT income FROM users WHERE id = :usersID", {"usersID": userID}).fetchone()[0]
return float(income)
def getLastFiveExpenses(userID):
results = db.execute(
"SELECT description, category, expenseDate, amount FROM expenses WHERE user_id = :usersID ORDER BY id DESC LIMIT 5",
{"usersID": userID},
).fetchall()
lastFiveExpenses = convertSQLToDict(results)
if lastFiveExpenses:
return lastFiveExpenses
else:
return None
def deleteExpense(expense, userID):
result = db.execute(
"delete from expenses where user_id = :usersID and id = :oldExpenseID",
{"usersID": userID, "oldExpenseID": expense["id"]},
)
db.commit()
return True
def getExpense(formData, userID):
expense = {
"description": None,
"category": None,
"date": None,
"amount": None,
"submitTime": None,
"id": None,
}
expense["description"] = formData.get("oldDescription").strip()
expense["category"] = formData.get("oldCategory").strip()
expense["date"] = formData.get("oldDate").strip()
expense["amount"] = formData.get("oldAmount").strip()
expense["submitTime"] = formData.get("submitTime").strip()
expense["amount"] = float(expense["amount"].replace("$" or "₹", "").replace(",", ""))
expenseID = db.execute(
"SELECT id FROM expenses WHERE user_id = :usersID AND description = :oldDescription AND category = :oldCategory AND expenseDate = :oldDate AND amount = :oldAmount AND submitTime = :oldSubmitTime",
{
"usersID": userID,
"oldDescription": expense["description"],
"oldCategory": expense["category"],
"oldDate": expense["date"],
"oldAmount": expense["amount"],
"oldSubmitTime": expense["submitTime"],
},
).fetchone()
if expenseID:
expense["id"] = expenseID[0]
else:
expense["id"] = None
return expense
def updateExpense(oldExpense, formData, userID):
expense = {"description": None, "category": None, "date": None, "amount": None, "payer": None}
expense["description"] = formData.get("description").strip()
expense["category"] = formData.get("category").strip()
expense["date"] = formData.get("date").strip()
expense["amount"] = formData.get("amount").strip()
# Convert the amount from string to float for the DB
expense["amount"] = float(expense["amount"])
# Make sure the user actually is submitting changes and not saving the existing expense again
hasChanges = False
for key, value in oldExpense.items():
# Exit the loop when reaching submitTime since that is not something the user provides in the form for a new expense
if key == "submitTime":
break
else:
if oldExpense[key] != expense[key]:
hasChanges = True
break
if hasChanges is False:
return None
# Update the existing record
now = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
result = db.execute(
"UPDATE expenses SET description = :newDescription, category = :newCategory, expenseDate = :newDate, amount = :newAmount, submitTime = :newSubmitTime WHERE id = :existingExpenseID AND user_id = :usersID",
{
"newDescription": expense["description"],
"newCategory": expense["category"],
"newDate": expense["date"],
"newAmount": expense["amount"],
"newSubmitTime": now,
"existingExpenseID": oldExpense["id"],
"usersID": userID,
},
).rowcount
db.commit()
# Make sure result is not empty (indicating it could not update the expense)
if result:
# Add dictionary to list (to comply with design/standard of expensed.html)
expenses = []
expenses.append(expense)
return expenses
else:
return None
def generateMonthlyReport(userID, year=None):
if not year:
year = datetime.now().year
spending_month_chart = getMonthlySpending(userID, year)
results = db.execute(
"SELECT description, category, expensedate, amount FROM expenses WHERE user_id = :usersID AND date_part('year', date(expensedate)) = :year ORDER BY id ASC",
{"usersID": userID, "year": year},
).fetchall()
spending_month_table = convertSQLToDict(results)
monthlyReport = {"chart": spending_month_chart, "table": spending_month_table}
return monthlyReport
def getMonthlySpending(userID, year=None):
spending_month = []
month = {"name": None, "amount": None}
if not year:
year = datetime.now().year
results = db.execute(
"SELECT date_part('month', date(expensedate)) AS month, SUM(amount) AS amount FROM expenses WHERE user_id = :usersID AND date_part('year', date(expensedate)) = :year GROUP BY date_part('month', date(expensedate)) ORDER BY month",
{"usersID": userID, "year": year},
).fetchall()
spending_month_query = convertSQLToDict(results)
for record in spending_month_query:
month["name"] = calendar.month_abbr[int(record["month"])]
month["amount"] = record["amount"]
spending_month.append(month.copy())
return spending_month
def getBudgets(userID):
results = db.execute(
"SELECT id, name, amount, user_id FROM budgets WHERE user_id = :usersID ORDER BY name ASC", {"usersID": userID}
).fetchall()
budgets_query = convertSQLToDict(results)
if budgets_query:
# Create a dict with budget user_id as key and empty list as value which will store all budgets for that user_id
budgets = {budget["user_id"]: [] for budget in budgets_query}
# Update the dict by inserting budget info as values
for budget in budgets_query:
budgets[budget["user_id"]].append({"amount": budget["amount"], "id": budget["id"], "name": budget["name"]})
return budgets
print(budgets)
else:
return None
def generateBudgetsReport(userID):
budgetsReport = []
year = datetime.now().year
budgetsReport = e_account.getBudgets(userID, year)
# Loop through the budgets and add a new key/value pair to hold expense details per budget
if budgetsReport:
for record in budgetsReport:
budgetID = getBudgetID(record["name"], userID)
results = db.execute(
"SELECT expenses.description, expenses.category, expenses.expenseDate, expenses.amount FROM expenses WHERE user_id = :usersID AND category IN (SELECT categories.name FROM budgetcategories INNER JOIN categories on budgetcategories.category_id = categories.id WHERE budgetcategories.budgets_id = :budgetID)",
{"usersID": userID, "budgetID": budgetID},
).fetchall()
expenseDetails = convertSQLToDict(results)
record["expenses"] = expenseDetails
return budgetsReport
def getBudgetID(budgetName, userID):
budgetID = db.execute(
"SELECT id FROM budgets WHERE user_id = :usersID AND name = :budgetName",
{"usersID": userID, "budgetName": budgetName},
).fetchone()[0]
if not budgetID:
return None
else:
return budgetID
def deleteBudget(budgetName, userID):
budgetID = getBudgetID(budgetName, userID)
if budgetID:
db.execute("DELETE FROM budgetCategories WHERE budgets_id = :budgetID", {"budgetID": budgetID})
db.commit()
db.execute("DELETE FROM budgets WHERE id = :budgetID", {"budgetID": budgetID})
db.commit()
return budgetName
else:
return None
def getTotalBudgeted(userID, year=None):
amount = db.execute(
"SELECT SUM(amount) AS amount FROM budgets WHERE user_id = :usersID",
{"usersID": userID},
).fetchone()[0]
if amount is None:
return 0
else:
return amount
def isUniqueBudgetName(budgetName, budgetID, userID):
if budgetID == None:
results = db.execute("SELECT name FROM budgets WHERE user_id = :usersID", {"usersID": userID}).fetchall()
existingBudgets = convertSQLToDict(results)
else:
results = db.execute(
"SELECT name FROM budgets WHERE user_id = :usersID AND NOT id = :oldBudgetID",
{"usersID": userID, "oldBudgetID": budgetID},
).fetchall()
existingBudgets = convertSQLToDict(results)
isUniqueName = True
for budget in existingBudgets:
if budgetName.lower() == budget["name"].lower():
isUniqueName = False
break
if isUniqueName:
return True
else:
return False
def addCategory(budgetID, categoryIDS):
for categoryID in categoryIDS:
db.execute(
"INSERT INTO budgetCategories (budgets_id, category_id, amount) VALUES (:budgetID, :categoryID, :percentAmount)",
{"budgetID": budgetID, "categoryID": categoryID["id"], "percentAmount": categoryID["amount"]},
)
db.commit()
def getBudgetCategoryIDS(categories, userID):
categoryIDS = []
for category in categories:
categoryID = db.execute(
"SELECT categories.id FROM userCategories INNER JOIN categories ON userCategories.category_id = categories.id WHERE userCategories.user_id = :usersID AND categories.name = :categoryName",
{"usersID": userID, "categoryName": category["name"]},
).fetchone()[0]
id_amount = {"id": None, "amount": None}
id_amount["id"] = categoryID
id_amount["amount"] = category["percent"]
categoryIDS.append(id_amount)
return categoryIDS
def generateBudgetFromForm(formData):
budget = {"name": None, "amount": None, "categories": []}
counter = 0
# Loop through all of the form data to extract budgets details and store in the budget dict
for key, value in formData:
counter += 1
# First 3 keys represent the name/year/amount from the form, all other keys represent dynamically loaded categories from the form
if counter <= 2:
# Check name for invalid chars and uniqueness
if key == "name":
# Invalid chars are all special chars except underscores, spaces, and hyphens (uses same regex as what's on the HTML page)
validBudgetName = re.search("^([a-zA-Z0-9_\s\-]*)$", value)
if validBudgetName:
budget[key] = value.strip()
else:
return {
"apology": "Please enter a budget name without special characters except underscores, spaces, and hyphens"
}
# Convert the amount from string to float
else:
amount = float(value.strip())
budget[key] = amount
# All other keys will provide the *category* name / percent budgeted
else:
# Skip iteration if value is empty (empty means the user doesnt want the category in their budget)
if value == "":
continue
# Need to split the key since the HTML elements are loaded dynamically and named like 'categories.1', 'categories.2', etc.
cleanKey = key.split(".")
# Store the category name and associated % the user wants budgetd for the category
category = {"name": None, "percent": None}
if cleanKey[0] == "categories":
category["name"] = value.strip()
# Get the percent value and convert to decimal
percent = int(formData[counter][1].strip()) / 100
category["percent"] = percent
# Add the category to the list of categories within the dict
budget[cleanKey[0]].append(category)
# Pass on this field because we grab the percent above (Why? It's easier to keep these 2 lines than rewrite many lines. This is the lowest of low pri TODOs)
elif cleanKey[0] == "categoryPercent":
pass
else:
return {
"apology": "Only categories and their percentage of the overall budget are allowed to be stored"
}
return budget
def createBudget(budget, userID):
# Verify the budget name is not a duplicate of an existing budget
uniqueBudgetName = isUniqueBudgetName(budget["name"], None, userID)
if not uniqueBudgetName:
return {"apology": "Please enter a unique budget name, not a duplicate."}
# Insert new budget into DB
newBudgetID = db.execute(
"INSERT INTO budgets (name, amount, user_id) VALUES (:budgetName, :budgetAmount, :usersID) RETURNING id",
{
"budgetName": budget["name"],
"budgetAmount": budget["amount"],
"usersID": userID,
},
).fetchone()[0]
db.commit()
# Get category IDs from DB for the new budget
categoryIDS = getBudgetCategoryIDS(budget["categories"], userID)
# Insert a record for each category in the new budget
addCategory(newBudgetID, categoryIDS)
return budget
def deleteBudget(budgetName, userID):
budgetID = getBudgetID(budgetName, userID)
if budgetID:
db.execute("DELETE FROM budgetCategories WHERE budgets_id = :budgetID", {"budgetID": budgetID})
db.commit()
db.execute("DELETE FROM budgets WHERE id = :budgetID", {"budgetID": budgetID})
db.commit()
return budgetName
else:
return None
def getBudgetByID(budgetID, userID):
results = db.execute(
"SELECT name, amount, id FROM budgets WHERE user_id = :usersID AND id = :budgetID",
{"usersID": userID, "budgetID": budgetID},
).fetchall()
budget = convertSQLToDict(results)
return budget[0]
def getTotalBudgetedAmount(userID):
amount = db.execute(
"SELECT SUM(amount) AS amount FROM budgets WHERE user_id = :usersID", {"usersID": userID}
).fetchone()[0]
if amount is None:
return 0
else:
return amount
def getUpdatableBudget(budget, userID):
categories = e_categories.getSpendCategories(userID)
# Get the budget's spend categories and % amount for each category
results = db.execute(
"SELECT DISTINCT categories.name, budgetCategories.amount FROM budgetCategories INNER JOIN categories ON budgetCategories.category_id = categories.id INNER JOIN budgets ON budgetCategories.budgets_id = budgets.id WHERE budgets.id = :budgetsID",
{"budgetsID": budget["id"]},
).fetchall()
budgetCategories = convertSQLToDict(results)
# Add 'categories' as a new key/value pair to the existing budget dict
budget["categories"] = []
for category in categories:
for budgetCategory in budgetCategories:
# Mark the category as checked/True if it exists in the budget that the user wants to update
if category["name"] == budgetCategory["name"]:
amount = round(budgetCategory["amount"] * 100)
budget["categories"].append({"name": category["name"], "amount": amount, "checked": True})
break
else:
budget["categories"].append({"name": category["name"], "amount": None, "checked": False})
return budget
def updateBudget(oldBudgetName, budget, userID):
oldBudgetID = getBudgetID(oldBudgetName, userID)
uniqueBudgetName = isUniqueBudgetName(budget["name"], oldBudgetID, userID)
if not uniqueBudgetName:
return {"apology": "Please enter a unique budget name, not a duplicate."}
db.execute(
"UPDATE budgets SET name = :budgetName, amount = :budgetAmount WHERE id = :oldBudgetID AND user_id = :usersID",
{
"budgetName": budget["name"],
"budgetAmount": budget["amount"],
"oldBudgetID": oldBudgetID,
"usersID": userID,
},
)
db.commit()
db.execute("DELETE FROM budgetCategories WHERE budgets_id = :oldBudgetID", {"oldBudgetID": oldBudgetID})
db.commit()
categoryIDS = getBudgetCategoryIDS(budget["categories"], userID)
addCategory(oldBudgetID, categoryIDS)
return budget
def getHistory(userID):
results = db.execute(
"SELECT description, category, expenseDate AS date, amount, submitTime FROM expenses WHERE user_id = :usersID ORDER BY id ASC",
{"usersID": userID},
).fetchall()
history = convertSQLToDict(results)
return history
def getSpendingTrends(userID, year=None):
spending_trends = []
categoryTrend = {"name": None, "proportionalAmount": None, "totalSpent": None, "totalCount": None}
if not year:
year = datetime.now().year
results = db.execute(
"SELECT category, COUNT(category) as count, SUM(amount) as amount FROM expenses WHERE user_id = :usersID AND date_part('year', date(expensedate)) = :year GROUP BY category ORDER BY COUNT(category) DESC",
{"usersID": userID, "year": year},
).fetchall()
categories = convertSQLToDict(results)
totalSpent = 0
for categoryExpense in categories:
totalSpent += categoryExpense["amount"]
for category in categories:
proportionalAmount = round((category["amount"] / totalSpent) * 100)
if proportionalAmount < 1:
continue
else:
categoryTrend["name"] = category["category"]
categoryTrend["proportionalAmount"] = proportionalAmount
categoryTrend["totalSpent"] = category["amount"]
categoryTrend["totalCount"] = category["count"]
spending_trends.append(categoryTrend.copy())
return spending_trends
def generateSpendingTrendsReport(userID, year=None):
if not year:
year = datetime.now().year
categories = []
category = {"name": None, "expenseMonth": 0, "expenseCount": 0, "amount": 0}
spending_trends_table = {
"January": [],
"February": [],
"March": [],
"April": [],
"May": [],
"June": [],
"July": [],
"August": [],
"September": [],
"October": [],
"November": [],
"December": [],
}
categories_active = e_categories.getSpendCategories(userID)
categories_inactive = e_categories.getSpendCategories_Inactive(userID)
for activeCategory in categories_active:
category["name"] = activeCategory["name"]
categories.append(category.copy())
for inactiveCategory in categories_inactive:
category["name"] = inactiveCategory["category"]
categories.append(category.copy())
for month in spending_trends_table.keys():
spending_trends_table[month] = copy.deepcopy(categories)
results = db.execute(
"SELECT date_part('month', date(expensedate)) AS monthofcategoryexpense, category AS name, COUNT(category) AS count, SUM(amount) AS amount FROM expenses WHERE user_id = :usersID AND date_part('year', date(expensedate)) = :year GROUP BY date_part('month', date(expensedate)), category ORDER BY COUNT(category) DESC",
{"usersID": userID, "year": year},
).fetchall()
spending_trends_table_query = convertSQLToDict(results)
for categoryExpense in spending_trends_table_query:
monthOfExpense = calendar.month_name[int(categoryExpense["monthofcategoryexpense"])]
for category in spending_trends_table[monthOfExpense]:
if category["name"] == categoryExpense["name"]:
category["expenseMonth"] = categoryExpense["monthofcategoryexpense"]
category["expenseCount"] = categoryExpense["count"]
category["amount"] = categoryExpense["amount"]
break
else:
continue
numberOfCategories = len(categories)
categoryTotal = 0
for i in range(numberOfCategories):
for month in spending_trends_table.keys():
categoryTotal += spending_trends_table[month][i]["amount"]
categories[i]["amount"] = categoryTotal
categoryTotal = 0
spending_trends_chart = getSpendingTrends(userID, year)
spendingTrendsReport = {"chart": spending_trends_chart, "table": spending_trends_table, "categories": categories}
return spendingTrendsReport