|
| 1 | +import sqlite3 |
| 2 | +from matplotlib import pyplot as plt |
| 3 | + |
| 4 | +plt.style.use('ggplot') |
| 5 | + |
| 6 | + |
| 7 | +def send_bg_color(color): |
| 8 | + connection = sqlite3.connect("Finance.db") |
| 9 | + cur = connection.cursor() |
| 10 | + cur.execute(f"UPDATE colorChoice SET color = (:color) WHERE rowid = 1", {"color": color}) |
| 11 | + connection.commit() |
| 12 | + connection.close() |
| 13 | + |
| 14 | + |
| 15 | +def get_bg_color(): |
| 16 | + connection = sqlite3.connect("Finance.db") |
| 17 | + cur = connection.cursor() |
| 18 | + cur.execute(f"SELECT * FROM colorChoice") |
| 19 | + data = cur.fetchall() |
| 20 | + return data[0][0] |
| 21 | + |
| 22 | + |
| 23 | +def send_data(query, data): |
| 24 | + connection = sqlite3.connect("Finance.db") |
| 25 | + cur = connection.cursor() |
| 26 | + num_data = int(data) |
| 27 | + cur.execute(f"INSERT INTO {query} VALUES (:payed)", {"payed": num_data}) |
| 28 | + connection.commit() |
| 29 | + connection.close() |
| 30 | + |
| 31 | + |
| 32 | +def update_record(query, data): |
| 33 | + connection = sqlite3.connect("Finance.db") |
| 34 | + cur = connection.cursor() |
| 35 | + num_data = int(data) |
| 36 | + cur.execute(f"UPDATE {query} SET userGoal = (:goal) WHERE rowid = 1", {"goal": num_data}) |
| 37 | + connection.commit() |
| 38 | + connection.close() |
| 39 | + |
| 40 | + |
| 41 | +def sum_partic_expense(query): |
| 42 | + connection = sqlite3.connect("Finance.db") |
| 43 | + cur = connection.cursor() |
| 44 | + cur.execute(f"SELECT SUM(payed) FROM {query}") |
| 45 | + d = cur.fetchall() |
| 46 | + connection.commit() |
| 47 | + connection.close() |
| 48 | + return d[0][0] |
| 49 | + |
| 50 | + |
| 51 | +def get_total_spent(): |
| 52 | + spent = 0 |
| 53 | + spent_fun = sum_partic_expense("FUN") |
| 54 | + spent_transportation = sum_partic_expense("TRANSPORTATION") |
| 55 | + spent_food = sum_partic_expense("FOOD") |
| 56 | + spent_clothes = sum_partic_expense("CLOTHES") |
| 57 | + spent_bills = sum_partic_expense("BILLS") |
| 58 | + spent_other = sum_partic_expense("OTHER") |
| 59 | + |
| 60 | + if spent_fun is not None: |
| 61 | + spent += spent_fun |
| 62 | + |
| 63 | + if spent_transportation is not None: |
| 64 | + spent += spent_transportation |
| 65 | + |
| 66 | + if spent_food is not None: |
| 67 | + spent += spent_food |
| 68 | + |
| 69 | + if spent_clothes is not None: |
| 70 | + spent += spent_clothes |
| 71 | + |
| 72 | + if spent_bills is not None: |
| 73 | + spent += spent_bills |
| 74 | + |
| 75 | + if spent_other is not None: |
| 76 | + spent += spent_other |
| 77 | + |
| 78 | + return spent |
| 79 | + |
| 80 | + |
| 81 | +def get_data(query): |
| 82 | + connection = sqlite3.connect("Finance.db") |
| 83 | + cur = connection.cursor() |
| 84 | + cur.execute(f"SELECT * FROM {query}") |
| 85 | + c = cur.fetchall() |
| 86 | + connection.commit() |
| 87 | + connection.close() |
| 88 | + return c |
| 89 | + |
| 90 | + |
| 91 | +def graph(graph_type): |
| 92 | + g_type = get_data(graph_type) |
| 93 | + plt.plot(range(1, len(g_type) + 1), g_type) |
| 94 | + plt.title(f"MONEY($) spent on category: {graph_type}") |
| 95 | + plt.xlabel("Number of purchases") |
| 96 | + plt.ylabel("CAD ($)") |
| 97 | + plt.show() |
| 98 | + |
| 99 | + |
| 100 | +def graph_all(): |
| 101 | + labels = [] |
| 102 | + slices = [] |
| 103 | + |
| 104 | + fun = sum_partic_expense("FUN") |
| 105 | + transport = sum_partic_expense("TRANSPORTATION") |
| 106 | + food = sum_partic_expense("FOOD") |
| 107 | + clothes = sum_partic_expense("CLOTHES") |
| 108 | + bills = sum_partic_expense("BILLS") |
| 109 | + other = sum_partic_expense("OTHER") |
| 110 | + |
| 111 | + if fun is not None: |
| 112 | + labels.append("FUN") |
| 113 | + slices.append(fun) |
| 114 | + |
| 115 | + if transport is not None: |
| 116 | + labels.append("TRANSPORT") |
| 117 | + slices.append(transport) |
| 118 | + |
| 119 | + if food is not None: |
| 120 | + labels.append("FOOD") |
| 121 | + slices.append(food) |
| 122 | + |
| 123 | + if clothes is not None: |
| 124 | + labels.append("CLOTHES") |
| 125 | + slices.append(clothes) |
| 126 | + |
| 127 | + if bills is not None: |
| 128 | + labels.append("BILLS") |
| 129 | + slices.append(bills) |
| 130 | + |
| 131 | + if other is not None: |
| 132 | + labels.append("OTHER") |
| 133 | + slices.append(other) |
| 134 | + |
| 135 | + colors = ("limegreen", "cyan", "yellow", "pink", |
| 136 | + "red", "lightblue") |
| 137 | + |
| 138 | + plt.pie(slices, labels=labels, shadow=True, wedgeprops={'edgecolor': 'black'}, autopct=f"%0.2f%%", colors=colors) |
| 139 | + plt.title("PIE CHART") |
| 140 | + plt.tight_layout() |
| 141 | + plt.show() |
| 142 | + |
| 143 | + |
| 144 | +def get_count(query): |
| 145 | + con = sqlite3.connect("Finance.db") |
| 146 | + cur = con.cursor() |
| 147 | + cur.execute(f"SELECT COUNT (*) FROM {query}") |
| 148 | + count = cur.fetchall() |
| 149 | + con.commit() |
| 150 | + con.close() |
| 151 | + return count[0][0] |
| 152 | + |
| 153 | + |
| 154 | +def delete_recent(query): |
| 155 | + con = sqlite3.connect("Finance.db") |
| 156 | + count = get_count(query) |
| 157 | + cur = con.cursor() |
| 158 | + cur.execute(f"DELETE FROM {query} WHERE rowid= (:delete)", {"delete": count}) |
| 159 | + con.commit() |
| 160 | + con.close() |
| 161 | + |
| 162 | + |
| 163 | +""" |
| 164 | +This will drop the table, |
| 165 | +then it will create a new table since there sqlite does not support truncate function |
| 166 | +""" |
| 167 | + |
| 168 | + |
| 169 | +def delete_data_in_table(query): |
| 170 | + con = sqlite3.connect("Finance.db") |
| 171 | + cur = con.cursor() |
| 172 | + cur.execute(f"DROP TABLE {query}") |
| 173 | + cur.execute(f"""CREATE TABLE {query}( |
| 174 | + payed real |
| 175 | + )""") |
| 176 | + con.commit() |
| 177 | + con.close() |
| 178 | + |
| 179 | + |
| 180 | +def delete_all(): |
| 181 | + delete_data_in_table("fun") |
| 182 | + delete_data_in_table("transportation") |
| 183 | + delete_data_in_table("food") |
| 184 | + delete_data_in_table("clothes") |
| 185 | + delete_data_in_table("bills") |
| 186 | + delete_data_in_table("other") |
| 187 | + |
| 188 | + |
0 commit comments