Example #1
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'))
Example #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())
Example #3
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())
Example #4
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'))