def generateBudgetsReport(userID, year=None): # Create data structure to hold users category spending data budgetsReport = [] # Default to getting current years budgets if not year: year = datetime.now().year # Get every budgets spent/remaining for the user budgetsReport = tendie_dashboard.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 = tendie_budgets.getBudgetID(record["name"], userID) results = db.execute( "SELECT expenses.description, expenses.category, expenses.expenseDate, expenses.payer, expenses.amount FROM expenses WHERE user_id = :usersID AND date_part('year', date(expensedate)) = :year AND category IN (SELECT categories.name FROM budgetcategories INNER JOIN categories on budgetcategories.category_id = categories.id WHERE budgetcategories.budgets_id = :budgetID)", { "usersID": userID, "year": year, "budgetID": budgetID }).fetchall() expenseDetails = convertSQLToDict(results) record["expenses"] = expenseDetails return budgetsReport
def index(): """Show dashboard of budget/expenses""" # User reached route via GET if request.method == "GET": # TODO reduce or completely remove the redundant use of javascript code in dashboard.js and reports.js # Initialize metrics to None to render the appropriate UX if data does not exist yet for the user expenses_year = None expenses_month = None expenses_week = None expenses_last5 = None spending_week = [] spending_month = [] # Get the users spend categories (for quick expense modal) categories = tendie_categories.getSpendCategories(session["user_id"]) # Get the users payers (for quick expense modal) payers = tendie_account.getPayers(session["user_id"]) # Get todays date (for quick expense modal) date = datetime.today().strftime('%Y-%m-%d') # Get the users income income = tendie_account.getIncome(session["user_id"]) # Get current years total expenses for the user expenses_year = tendie_dashboard.getTotalSpend_Year(session["user_id"]) # Get current months total expenses for the user expenses_month = tendie_dashboard.getTotalSpend_Month( session["user_id"]) # Get current week total expenses for the user expenses_week = tendie_dashboard.getTotalSpend_Week(session["user_id"]) # Get last 5 expenses for the user expenses_last5 = tendie_dashboard.getLastFiveExpenses( session["user_id"]) # Get every budgets spent/remaining for the user budgets = tendie_dashboard.getBudgets(session["user_id"]) # Get weekly spending for the user weeks = tendie_dashboard.getLastFourWeekNames() spending_week = tendie_dashboard.getWeeklySpending( weeks, session["user_id"]) # Get monthly spending for the user (for the current year) spending_month = tendie_dashboard.getMonthlySpending( session["user_id"]) # Get spending trends for the user spending_trends = tendie_dashboard.getSpendingTrends( session["user_id"]) # Get payer spending for the user payersChart = tendie_reports.generatePayersReport(session["user_id"]) return render_template("index.html", categories=categories, payers=payers, date=date, income=income, expenses_year=expenses_year, expenses_month=expenses_month, expenses_week=expenses_week, expenses_last5=expenses_last5, budgets=budgets, spending_week=spending_week, spending_month=spending_month, spending_trends=spending_trends, payersChart=payersChart) # User reached route via POST else: # Get all of the expenses provided from the HTML form formData = list(request.form.items()) # Add expenses to the DB for user expenses = tendie_expenses.addExpenses(formData, session["user_id"]) # Redirect to results page and render a summary of the submitted expenses return render_template("expensed.html", results=expenses)