diff --git a/README.md b/README.md index 4097d71..3cf3e00 100644 --- a/README.md +++ b/README.md @@ -21,5 +21,7 @@ Within Python Virtual Environment: 5. Run `python addbill.py` to add your server or domain bills. -6. Use your favourite scheduling method to set `python budget.py` to run regularly. It will post your finances to your fediverse server. +6. Run `python addfee.py` to add your regular fees. + +7. Use your favourite scheduling method to set `python budget.py` to run regularly. It will post your finances to your fediverse server. diff --git a/addfee.py b/addfee.py new file mode 100644 index 0000000..e74509f --- /dev/null +++ b/addfee.py @@ -0,0 +1,123 @@ +from datetime import datetime, timezone, timedelta +import time +import threading +import os +import sys +import os.path +import psycopg2 +import pytz +import dateutil +from dateutil.parser import parse +from decimal import * +getcontext().prec = 2 + +############################################################################### +# get fees rows +############################################################################### + +def print_fees(): + + try: + + conn = psycopg2.connect(database = budget_db, user = budget_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute("SELECT fee_description, payment_type, fee FROM fees") + + rows = cur.fetchall() + + for row in rows: + + print('fee description: ' + str(row[0]), ', payment type: ' + str(row[1]), 'fee: ' + str(row[2])) + + cur.close() + + except (Exception, psycopg2.DatabaseError) as error: + + print(error) + + finally: + + if conn is not None: + + conn.close() + +################################################################################### +# add fees to database +################################################################################### + +def insert_fees(fee_description, payment_type, fee): + + sql = "INSERT INTO fees(fee_description, payment_type, fee) VALUES(%s,%s,%s)" + + try: + + conn = psycopg2.connect(database = budget_db, user = budget_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute(sql, (fee_description, payment_type, fee)) + print("\n") + print("Updating fees...") + + conn.commit() + + cur.close() + + except (Exception, psycopg2.DatabaseError) as error: + + print(error) + + finally: + + if conn is not None: + + conn.close() + +############################################################################### +# INITIALISATION +############################################################################### + +# Returns the parameter from the specified file +def get_parameter( parameter, file_path ): + # Check if db_config.txt file exists + if not os.path.isfile(file_path): + if file_path == "config/db_config.txt": + print("File %s not found, exiting. Run db-setup.py."%file_path) + sys.exit(0) + + # Find parameter in file + with open( file_path ) as f: + for line in f: + if line.startswith( parameter ): + return line.replace(parameter + ":", "").strip() + + # Cannot find parameter, exit + print(file_path + " Missing parameter %s "%parameter) + print("Run setup.py") + sys.exit(0) + +# Load configuration from config file +config_filepath = "config/db_config.txt" +budget_db = get_parameter("budget_db", config_filepath) # E.g., budget +budget_db_user = get_parameter("budget_db_user", config_filepath) # E.g., mastodon + +############################################################################### + +while True: + pdb.set_trace() + fee_description = input("Fee description? (q to quit) ") + if fee_description == 'q': + sys.exit("Bye") + + fee_type = input("Fee type? (in ex. annual) ") + + fee = input("Fee? ") + if fee == '': + fee = '0.00' + fee = round(float(fee),2) + + insert_fees(fee_description, fee_type, fee) + + print_fees() diff --git a/budget.py b/budget.py index 27a1d7e..02b3d04 100644 --- a/budget.py +++ b/budget.py @@ -82,6 +82,50 @@ def get_donations(): conn.close() +def get_fixed_fees(): + + fees_desc_lst = [] + + fees_types_lst = [] + + fees_lst = [] + + + try: + + conn = None + + conn = psycopg2.connect(database = budget_db, user = budget_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute("select fee_description, payment_type, fee from fees") + + rows = cur.fetchall() + + for row in rows: + + fees_desc_lst.append(row[0]) + + fees_types_lst.append(row[1]) + + fees_lst.append(row[2]) + + cur.close() + + return (fees_desc_lst, fees_types_lst, fees_lst) + + except (Exception, psycopg2.DatabaseError) as error: + + sys.exit(error) + + finally: + + if conn is not None: + + conn.close() + + def mastodon(): # Load secrets from secrets file @@ -146,6 +190,8 @@ if __name__ == '__main__': bills = get_bills() + fees_desc_lst, fees_types_lst, fees_lst = get_fixed_fees() + toot_text = 'Finances of ' + mastodon_hostname + '\n\n' toot_text += 'Year: ' + str(now.year) + '\n' @@ -160,6 +206,14 @@ if __name__ == '__main__': toot_text += '%: ' + str(round((donations * 100)/bills,2)) + '\n\n' + i = 0 + + while i < len(fees_lst): + + toot_text += '\n' + str(fees_desc_lst[i]) + ': ' + str(fees_lst[i]) + ' (' + str(fees_types_lst[i]) + ')' + + i += 1 + print(toot_text) mastodon.status_post(toot_text, in_reply_to_id=None) diff --git a/db-setup.py b/db-setup.py index a28f7fb..859622a 100644 --- a/db-setup.py +++ b/db-setup.py @@ -151,13 +151,16 @@ if __name__ == '__main__': ##################################### - db = budget_db - db_user = budget_db_user table = 'incomes' sql = "create table " + table + " (datetime timestamptz primary key, donations decimal(7,2), owner decimal(7,2))" create_table(db, db_user, table, sql) ############################################################ + + table = 'fees' + sql = "create table " + table + " (fee_description varchar(20) primary key, payment_type varchar(10), fee decimal(7,2))" + create_table(db, db_user, table, sql) + ############################################################ print("Done!")