Exemplo n.º 1
0
def index():
    '''Budget dashboard'''

    current_user_id = session.get('logged_in_user')

    # get uncategorized expenses
    exp = Expenses(current_user_id)
    uncategorized_expenses = exp.get_entries(category_name="Uncategorized")

    # get latest expenses
    exp = Expenses(current_user_id)
    latest_expenses = exp.get_entries(limit=5)

    # get latest income
    inc = Income(current_user_id)
    latest_income = inc.get_entries(limit=5)

    # get accounts
    acc = Accounts(current_user_id)
    accounts = acc.get_accounts_and_loans()

    # split, get totals
    assets, liabilities, loans, assets_total, liabilities_total = [], [], [], 0, 0
    for a in accounts:
        if a[0].type == 'asset':
            assets.append(a)
            assets_total += float(a[0].balance)
        elif a[0].type == 'liability':
            liabilities.append(a)
            liabilities_total += float(a[0].balance)
        elif a[0].type == 'loan':
            # if we owe someone, it is our liability
            if float(a[0].balance) < 0:
                liabilities.append(a)
                liabilities_total += float(a[0].balance)
            else:
                assets.append(a)

    # monthly totals
    t, totals_list, highest_bar = Totals(current_user_id), [], 0.
    # object to dict
    for total in t.get_totals():
        bar = {}
        bar['month'] = total.month
        if (float(total.expenses) > 0): bar['expenses'] = float(total.expenses)
        if (float(total.income) > 0): bar['income'] = float(total.income)
        totals_list.append(bar)

        if (total.expenses > highest_bar): highest_bar = total.expenses
        if (total.income > highest_bar): highest_bar = total.income

    # calculate height for each bar
    for total in totals_list:
        if 'expenses' in total: total['expenses-height'] = (total['expenses'] / highest_bar) * 100
        if 'income' in total: total['income-height'] = (total['income'] / highest_bar) * 100

    return render_template('admin_dashboard.html', **locals())
Exemplo n.º 2
0
def add_income():
    '''Add an income entry'''

    current_user_id = session.get('logged_in_user')

    inc = Income(current_user_id)
    acc = Accounts(current_user_id)

    if request.method == 'POST':

        dict = __validate_income_form()
        for key in dict.keys(): exec(key + " = dict['" + key + "']")

        # 'heavier' checks
        if not error:
            # valid date?
            if is_date(date):
                # valid amount?
                if is_float(amount):
                    # valid category?
                    if inc.is_category(id=category_id):
                        # valid account?
                        if acc.is_account(account_id=account_id):

                            # add new income
                            inc.add_income(account_id=account_id, amount=amount, category_id=category_id, date=date,
                                           description=description)

                            # credit to account
                            acc.modify_account_balance(account_id, amount)

                            flash('Income added')

                        else: error = 'Not a valid account'
                    else: error = 'Not a valid category'
                else: error = 'Not a valid amount'
            else: error = 'Not a valid date'

    # fetch user's categories and accounts
    categories = inc.get_categories()
    accounts = acc.get_accounts()

    return render_template('admin_add_income.html', **locals())
Exemplo n.º 3
0
def delete_income(income_id):
    '''Delete income entry'''

    current_user_id = session.get('logged_in_user')
    incomes = Income(current_user_id)

    # is it valid?
    income = incomes.get_income(income_id)
    if income:
        # revert
        accounts = Accounts(current_user_id)
        accounts.modify_account_balance(amount=-float(income.amount), account_id=income.credit_to)

        incomes.delete_income(income_id)

        flash('Income deleted')
    else:
        flash('Not a valid income entry', 'error')


    return redirect(url_for('income.index'))
Exemplo n.º 4
0
def search():
    '''Search income'''

    model = Income(session.get('logged_in_user'))

    # query
    query = request.form['q'] if 'q' in request.form else ""

    # fetch entries
    entries = model.get_entries()

    # filter
    entries = entries.filter(IncomeTable.description.like("%"+query+"%"))

    # categories
    categories = model.get_categories()

    # date ranges for the template
    date_ranges = get_date_ranges()

    return render_template('admin_search_income.html', **locals())
Exemplo n.º 5
0
def edit_income(income_id):
    '''Edit income entry'''

    current_user_id = session.get('logged_in_user')

    inc = Income(current_user_id)

    # is it valid?
    income = inc.get_income(income_id)
    if income:
        # fetch user's categories and accounts
        categories = inc.get_categories()

        acc = Accounts(current_user_id)
        accounts = acc.get_accounts()

        if request.method == 'POST': # POST

            dict = __validate_income_form()
            for key in dict.keys(): exec(key + " = dict['" + key + "']")

            # 'heavier' checks
            if not error:
                # valid date?
                if is_date(date):
                    # valid amount?
                    if is_float(amount):
                        # valid category?
                        if inc.is_category(id=category_id):
                            # valid account?
                            if acc.is_account(account_id=account_id):

                                # debit the original account
                                acc.modify_account_balance(income.credit_to, -float(income.amount))

                                # credit the 'new' account
                                acc.modify_account_balance(account_id, amount)

                                # edit income entry
                                inc.edit_income(account_id=account_id, amount=amount, category_id=category_id,
                                                             date=date, description=description, income_id=income.id)

                                flash('Income edited')

                                return redirect(url_for('income.edit_income', income_id=income_id))

                            else: error = 'Not a valid account'
                        else: error = 'Not a valid category'
                    else: error = 'Not a valid amount'
                else: error = 'Not a valid date'

        return render_template('admin_edit_income.html', **locals())

    else: return redirect(url_for('income.index'))