diff --git a/db.py b/db.py index a17de13..f2b9943 100644 --- a/db.py +++ b/db.py @@ -35,6 +35,41 @@ def init_db(): Sell_shares INTEGER )''') + cur.execute('''CREATE TABLE IF NOT EXISTS comparison_set ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name STRING, + D_Salary INTEGER, + D_Num_fortnights_pay INTEGER, + School_Fees INTEGER, + Car_loan_via_pay INTEGER, + Car_loan INTEGER, + Car_balloon INTEGER, + Living_Expenses INTEGER, + Savings INTEGER, + Interest_Rate REAL, + Inflation REAL, + Mich_present INTEGER, + Overseas_trip INTEGER, + Mark_reno INTEGER, + D_leave_owed_in_days REAL, + D_TLS_shares INTEGER, + M_TLS_shares INTEGER, + D_CBA_shares INTEGER, + TLS_price REAL, + CBA_price REAL, + Overseas_trip_date STRING, + Mark_reno_date STRING, + Sell_shares INTEGER + )''') + + cur.execute('''CREATE TABLE IF NOT EXISTS comparison_savings_data ( + comparison_set_id INTEGER, + name STRING, + value INTEGER, + FOREIGN KEY(comparison_set_id) REFERENCES comparison_set(id) + )''') + + # Check if table is empty, if so insert default values cur.execute('SELECT COUNT(*) FROM finance') if cur.fetchone()[0] == 0: @@ -104,3 +139,38 @@ def update_finance(data): WHERE id = 1''', data) conn.commit() conn.close() + + +def insert_cset( data ): + conn = connect_db() + cur = conn.cursor() + cur.execute('''INSERT INTO comparison_set ( + name, D_Salary, D_Num_fortnights_pay, School_Fees, Car_loan_via_pay, + Car_loan, Car_balloon, Living_Expenses, Savings, Interest_Rate, Inflation, Mich_present, + Overseas_trip, Mark_reno, D_leave_owed_in_days, D_TLS_shares, M_TLS_shares, D_CBA_shares, + TLS_price, CBA_price, Overseas_trip_date, Mark_reno_date, Sell_shares + ) + VALUES ( + :name, :D_Salary, :D_Num_fortnights_pay, :School_Fees, :Car_loan_via_pay, + :Car_loan, :Car_balloon, :Living_Expenses, :Savings, :Interest_Rate, :Inflation, :Mich_present, + :Overseas_trip, :Mark_reno, :D_leave_owed_in_days, :D_TLS_shares, :M_TLS_shares, :D_CBA_shares, + :TLS_price, :CBA_price, :Overseas_trip_date, :Mark_reno_date, :Sell_shares + ) + ''', data['vars']) + cset_id = cur.lastrowid + + for d in data['savings_data']: + cur.execute( f"INSERT INTO comparison_savings_data ( comparison_set_id, name, value ) VALUES ( {cset_id}, '{d[0]}', '{d[1]}' )" ) + + conn.commit() + conn.close() + return cset_id + +def last_cset_savings_data(cset_id): + conn = connect_db() + cur = conn.cursor() + cur.execute('''select name, value from comparison_savings_data + where name = ( select max(name) from comparison_savings_data ) ''' ) + name, value = cur.fetchone() # fetchone() fetches a single row + conn.close() + return name, value diff --git a/main.py b/main.py index 3d361dc..a63e797 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ # main.py from flask import Flask, render_template, request, redirect, url_for, Response from calc import calculate_savings_depletion -from db import init_db, get_finance_data, update_finance, get_budget_data +from db import init_db, get_finance_data, update_finance, get_budget_data, last_cset_savings_data from collections import defaultdict from datetime import datetime import csv @@ -20,12 +20,13 @@ def index(): if depletion_date: depletion_date=depletion_date.date(); # just show date + # HARDCODED FOR NOW + cset_id = 1 # work out comparison func, but hardcode for now: COMP={} -# COMP['date']='2025-02-05' -# COMP['amount']=414887 - COMP['date']='2031-04-09' - COMP['amount']=4273.14 + COMP['date'], COMP['amount'] = last_cset_savings_data( cset_id ) + COMP['vars']=finance_data + COMP['savings_data']=savings_per_fortnight return render_template('index.html', finance=finance_data, depletion_date=depletion_date, savings=savings_per_fortnight, TLS=TLS, CBA=CBA, BUDGET=BUDGET, COMP=COMP) diff --git a/templates/index.html b/templates/index.html index 816f959..7faa25d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,11 +6,14 @@ Finance Form + +
-

Finance Tracker

-
+

Finance Tracker

+ +
@@ -24,18 +27,18 @@
-
-
- - -
-
+
+
+ + +
+
@@ -57,18 +60,18 @@
-
-
- - -
-
+
+
+ + +
+
@@ -90,7 +93,7 @@
-
+
@@ -110,12 +113,26 @@
-
+
+
+
+ + +
+
@@ -130,32 +147,12 @@
-
+
-
- -
-
- -
-
-
- - -
-
@@ -168,24 +165,9 @@
-
- -
+