diff --git a/BUGS b/BUGS index a11fb71..255b531 100644 --- a/BUGS +++ b/BUGS @@ -1,3 +1,2 @@ -when I remove and re-add a different onclick handler for new-bt and canc-bt, it -just does not really seem to work. If I call NewBillType(id) where id > 0, it -actuall updates in the backend, but it always does NewBillType(0) +when I cancel items, the changes need to be reverted for: + new bill, new bill type, change bill diff --git a/README b/README index 826f7e5..d97690b 100644 --- a/README +++ b/README @@ -1,4 +1,7 @@ TODO: + * fix BUGs + * convert code over to use bill_type instead of name in bills.html + CONSIDER in code: * when we time the payment of GMHBA / HCF (and at what cadence) and include it in calcs better diff --git a/db.py b/db.py index 7a0180d..cefe028 100644 --- a/db.py +++ b/db.py @@ -84,13 +84,25 @@ def init_db(): FOREIGN KEY(comparison_set_id) REFERENCES comparison_set(id) )''') + cur.execute('''CREATE TABLE IF NOT EXISTS bill_type ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name STRING + )''') + + cur.execute('''CREATE TABLE IF NOT EXISTS bill_data ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + bill_type INTEGER, + amount INTEGER, + bill_date DATE, + FOREIGN KEY(bill_type) REFERENCES bill_type(id) + )''') # Check if table is empty, if so insert default values cur.execute('SELECT COUNT(*) FROM finance') if cur.fetchone()[0] == 0: ### # For now manually update below on the fortnight of the original pay shcedule to compare saved version vs. our reality. Update: - # Savings (Macq+me bank) -- noting ME bank is: $1434.3 + # Savings (Macq+me bank) -- noting ME bank is: $1876.19, nab is 2727.95 # TLS/CBA prices # Interest rate # D_leave_owed_in_days @@ -217,6 +229,7 @@ def get_comp_set_data(cset_id): # do this for convenience in printing single last cset data point COMP['date'], COMP['amount'] = COMP['savings_data'][-1] + conn.close() return COMP @@ -229,4 +242,69 @@ def get_comp_set_options(finance): # get saved finance data for this comparison set cur.execute( f"select id, name from comparison_set order by id" ) finance['COMP_SETS'].extend( cur.fetchall() ) + conn.close() + return + +def get_bill_data(): + conn = connect_db(True) + cur = conn.cursor() + cur.execute('SELECT bd.id, bt.id as bill_type_id, bt.name, bd.amount, bd.bill_date FROM bill_type bt, bill_data bd where bt.id = bd.bill_type order by bt.name, bd.bill_date') + bd = cur.fetchall() + conn.close() + return bd + +def get_bill_types(): + conn = connect_db(True) + cur = conn.cursor() + cur.execute('SELECT id, name FROM bill_type order by name') + bt = cur.fetchall() + conn.close() + return bt + +def new_bill( name, amount, bill_date ): + conn = connect_db(False) + cur = conn.cursor() + cur.execute( f"insert into bill_data ( 'bill_type', 'amount', 'bill_date' ) values ( '{name}', '{amount}', '{bill_date}' )" ) + conn.commit() + conn.close() + return + +def update_bill_data( id, name, amount, bill_date ): + conn = connect_db(False) + cur = conn.cursor() + cur.execute( f"update bill_data set bill_type =(select id from bill_type where name ='{name}'), amount='{amount}', bill_date='{bill_date}' where id = {id}" ) + conn.commit() + conn.close() + return + +def insert_bill_type( bt ): + conn = connect_db(False) + cur = conn.cursor() + cur.execute( f"insert into bill_type ( 'name' ) values ( '{bt}' )" ) + conn.commit() + conn.close() + return + +def update_bill_type(id, bill_type): + conn = connect_db(False) + cur = conn.cursor() + cur.execute( f"update bill_type set name ='{bill_type}' where id = {id}" ) + conn.commit() + conn.close() + return + +def delete_bill(id): + conn = connect_db(False) + cur = conn.cursor() + cur.execute( f"delete from bill_data where id = '{id}'" ) + conn.commit() + conn.close() + return + +def delete_bill_type( id ): + conn = connect_db(False) + cur = conn.cursor() + cur.execute( f"delete from bill_type where id = '{id}'" ) + conn.commit() + conn.close() return diff --git a/main.py b/main.py index 45b24d1..6c0b445 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,8 @@ from flask import Flask, render_template, request, redirect, url_for, Response, jsonify from calc import calculate_savings_depletion from db import init_db, get_finance_data, update_finance, get_budget_data, insert_cset, get_comp_set_data, get_comp_set_options +from db import get_bill_data, new_bill, update_bill_data, delete_bill +from db import get_bill_types, insert_bill_type, update_bill_type, delete_bill_type from collections import defaultdict, Counter from datetime import datetime import csv @@ -133,21 +135,52 @@ def update(): request.form['compare_to'], request.form['Ioniq6_future'] ) - update_finance(finance_data) - return redirect(url_for('index')) +@app.route('/bills') +def DisplayBillData(): + bill_data = get_bill_data() + bill_types = get_bill_types() + now=datetime.today().strftime('%Y-%m-%d') + return render_template('bills.html', now=now, bill_data=bill_data, bill_types=bill_types ) + +@app.route('/newbilltype', methods=['POST']) +def InsertBillType(): + data = request.get_json() + insert_bill_type( data['bill_type'] ) + return "200" + +@app.route('/updatebilltype', methods=['POST']) +def UpdateBillType(): + data = request.get_json() + update_bill_type( data['id'], data['bill_type'] ) + return "200" + +@app.route('/newbill', methods=['POST']) +def InsertBill(): + data = request.get_json() + new_bill( data['name'], data['amount'], data['bill_date'] ) + return "200" + +@app.route('/updatebill', methods=['POST']) +def UpdateBill(): + data = request.get_json() + update_bill_data( data['id'], data['name'], data['amount'], data['bill_date'] ) + return "200" + +@app.route('/delbilltype', methods=['POST']) +def DeleteBillType(): + data = request.get_json() + delete_bill_type( data['id'] ) + return "200" + +@app.route('/delbill', methods=['POST']) +def DeleteBill(): + data = request.get_json() + delete_bill( data['id'] ) + return "200" + # Main program if __name__ == '__main__': app.run(debug=True) - - -########## -# -# How to cross-check, so we get paid: 4762.29 + 1962.56 per fortnight or: $174846.1 AFTER TAX -# take $20k for Cam, and $20k for Mich for schools last year, $10k for Cam pres, take $72k for living, take $8k in furniture. -# We went from 250 to 300k (more or less), so about right -# to note: transfers to Cam/Mich - $850 -# -########## diff --git a/templates/bills.html b/templates/bills.html index c31090a..1cf9910 100644 --- a/templates/bills.html +++ b/templates/bills.html @@ -21,50 +21,47 @@

Bill Details (go to Finance Tracker)

+
- -
{% for el in bill_types %} {% endfor %}
-
-
- - +
-
-
-
-
-
- {% for el in bill_data %}
-
-
-
- - +
+
+
- {% endfor %} + {% for el in bill_data %} +
+
+
+
+ + + + +
+ {% endfor %}
- +
@@ -81,7 +78,7 @@
- + @@ -89,8 +86,84 @@ {% endfor %}