Exemple #1
0
def give():
    '''Give a loan or pay someone back'''

    current_user_id = session.get('logged_in_user')

    our_accounts = Accounts(current_user_id)

    if request.method == 'POST':

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

        # 'heavier' checks
        if not error:
            # valid amount?
            if is_float(amount):
                # valid date?
                if is_date(date):
                    # valid account?
                    if our_accounts.is_account(account_id=deduct_from_account):

                        # add our loans entry
                        our_loans = Loans(current_user_id)
                        our_loan_id = our_loans.add_loan(other_user_id=to_user, date=date, account_id=deduct_from_account,
                                     description=description, amount=-float(amount))

                        # add their loans entry
                        their_loans = Loans(to_user)
                        their_loan_id = their_loans.add_loan(other_user_id=current_user_id, date=date,
                                                           account_id=deduct_from_account, description=description,
                                                           amount=amount)

                        # generate slugs for the new loans
                        our_slugs = Slugs(current_user_id)
                        slug = our_slugs.add_slug(type='loan', object_id=our_loan_id, description=description)
                        their_slugs = Slugs(to_user)
                        their_slugs.add_slug(type='loan', object_id=their_loan_id, slug=slug)

                        their_accounts = Accounts(to_user)

                        # transfer money from/to respective accounts
                        our_accounts.modify_user_balance(account_id=deduct_from_account, amount=-float(amount))
                        their_accounts.modify_user_balance(amount=amount)

                        # fudge loan 'account' monies
                        our_accounts.modify_loan_balance(amount=amount, with_user_id=to_user)
                        their_accounts.modify_loan_balance(amount=-float(amount), with_user_id=current_user_id)

                        flash('Loan given')

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

    # fetch users from connections from us
    our_users = Users(current_user_id)
    users = our_users.get_connections()
    accounts = our_accounts.get_accounts()

    return render_template('admin_give_loan.html', **locals())
Exemple #2
0
def __edit_give_loan(loan_id, our_loans, loan, current_user_id):
    '''Editing of loan entries where we were giving money'''

    our_accounts = Accounts(current_user_id)

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

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

        # 'heavier' checks
        if not error:
            # valid amount?
            if is_float(amount):
                # valid date?
                if is_date(date):
                    # valid account?
                    if our_accounts.is_account(account_id=deduct_from_account):

                        their_accounts = Accounts(loan.other_user)

                        # first roll back user balances
                        our_accounts.modify_user_balance(account_id=loan.account, amount=loan.amount)
                        their_accounts.modify_user_balance(amount=-float(loan.amount))

                        # now roll back loan account monies
                        our_accounts.modify_loan_balance(amount=-float(loan.amount), with_user_id=loan.other_user)
                        their_accounts.modify_loan_balance(amount=loan.amount, with_user_id=current_user_id)

                        # the user might have changed...
                        if loan.other_user != to_user:
                            their_accounts = Accounts(to_user)

                        # transfer money from/to respective accounts
                        our_accounts.modify_user_balance(account_id=deduct_from_account, amount=-float(amount))
                        their_accounts.modify_user_balance(amount=amount)

                        # fudge loan 'account' monies
                        our_accounts.modify_loan_balance(amount=amount, with_user_id=to_user)
                        their_accounts.modify_loan_balance(amount=-float(amount), with_user_id=current_user_id)

                        # get slug as a unique identifier
                        slug = our_loans.get_loan_slug(loan_id=loan_id)

                        # the user might have changed...
                        their_loans = Loans(loan.other_user)
                        if loan.other_user != to_user:
                            # delete their original loan entry (and its slug)
                            their_loans.delete_loan(slug=slug)

                            # new user
                            their_loans = Loans(to_user)
                            their_loan_id = their_loans.add_loan(other_user_id=current_user_id, date=date,
                                                                 description=description, amount=amount)

                            # save their new slug
                            their_slugs = Slugs(to_user)
                            their_slugs.add_slug(type='loan', object_id=their_loan_id, slug=slug)
                        else:
                            # update their loans entry
                            their_loans.edit_loan(other_user_id=current_user_id, date=date, description=description,
                                                  amount=amount, slug=slug)

                        # update our loans entry
                        our_loans.edit_loan(other_user_id=to_user, date=date, description=description,
                                            amount=-float(amount), account_id=deduct_from_account, loan_id=loan_id)

                        flash('Loan edited')

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

    our_users = Users(current_user_id)
    users = our_users.get_connections()
    accounts = our_accounts.get_accounts()

    return render_template('admin_edit_give_loan.html', **locals())
Exemple #3
0
def add_expense():
    '''Add an expense entry'''

    error = None
    current_user_id = session.get('logged_in_user')

    our_accounts = Accounts(current_user_id)
    our_expenses = Expenses(current_user_id)
    users = Users(current_user_id)

    if request.method == 'POST':
        dict = __validate_expense_form()
        for key in dict.keys(): exec(key + " = dict['" + key + "']")

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

                            # is it a shared expense?
                            if 'is_shared' in request.form:
                                # fetch values and check they are actually provided
                                if 'split' in request.form: split = request.form['split']
                                else: error = 'You need to provide a % split'
                                if 'user' in request.form: shared_with_user = request.form['user']
                                else: error = 'You need to provide a user'

                                # 'heavier' checks
                                if not error:
                                    # valid percentage split?
                                    if is_percentage(split):
                                        # valid user sharing with?
                                        if users.is_connection(user_id=shared_with_user):

                                            # figure out percentage split
                                            loaned_amount = round((float(amount)*(100-float(split)))/100, 2)

                                            # create loans
                                            our_loans = Loans(current_user_id)
                                            our_loan_id = our_loans.add_loan(other_user_id=shared_with_user, date=date,
                                                         account_id=account_id, description=description,
                                                         amount=-float(loaned_amount))

                                            our_loans = Loans(shared_with_user)
                                            their_loan_id = our_loans.add_loan(other_user_id=current_user_id, date=date,
                                                         description=description, amount=loaned_amount)

                                            # generate slugs for the new loans
                                            our_slugs = Slugs(current_user_id)
                                            slug = our_slugs.add_slug(type='loan', object_id=our_loan_id,
                                                                      description=description)
                                            their_slugs = Slugs(shared_with_user)
                                            their_slugs.add_slug(type='loan', object_id=their_loan_id, slug=slug)

                                            flash('Loan given')

                                            # add new expense (loaner)
                                            our_expense_id = our_expenses.add_expense(date=date, category_id=category_id,
                                                                               account_id=account_id,
                                                                               amount=float(amount) - loaned_amount,
                                                                               description=description)

                                            # add new expenses (borrower)
                                            their_expenses = Expenses(shared_with_user)
                                            their_expense_id = their_expenses.add_expense(date=date, amount=loaned_amount,
                                                                                   description=description, pass_thru=True)

                                            # fudge loan 'account' monies
                                            our_accounts.modify_loan_balance(amount=loaned_amount,
                                                                             with_user_id=shared_with_user)
                                            their_accounts = Accounts(shared_with_user)
                                            their_accounts.modify_loan_balance(amount=-float(loaned_amount),
                                                                         with_user_id=current_user_id)

                                            # link loan and the expenses (through us)
                                            our_expenses.link_to_loan(expense_id=our_expense_id, loan_id=our_loan_id,
                                                                shared_with=shared_with_user, percentage=split,
                                                                original_amount=amount)
                                            their_expenses.link_to_loan(expense_id=their_expense_id, loan_id=our_loan_id,
                                                                shared_with=current_user_id, percentage=split,
                                                                original_amount=amount)

                                        else: error = 'Not a valid user sharing with'
                                    else: error = 'Not a valid % split'

                            else:
                                # add new expense
                                our_expenses.add_expense(date=date, category_id=category_id, account_id=account_id,
                                                   amount=amount, description=description)

                            if not error:
                                # debit from account
                                our_accounts.modify_user_balance(amount=-float(amount), account_id=account_id)

                                flash('Expense added')

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

    # fetch user's categories, accounts and users
    categories = our_expenses.get_categories()
    if not categories: error = 'You need to define at least one category'

    accounts = our_accounts.get_accounts()
    if not accounts: error = 'You need to define at least one account'

    # fetch users from connections from us
    users = users.get_connections()

    return render_template('admin_add_expense.html', **locals())
Exemple #4
0
def __edit_shared_expense_into_shared(current_user_id, our_expenses, expense, our_accounts, our_users, date, description,
                                      account_id, amount, error, category_id, categories, accounts, users, expense_id,
                                      key=None, dict=None):
    '''Edit a shared expense entry into a shared entry still'''

    # fetch values and check they are actually provided
    if 'split' in request.form: split = request.form['split']
    else: error = 'You need to provide a % split'
    if 'user' in request.form: shared_with_user = request.form['user']
    else: error = 'You need to provide a user'

    # 'heavier' checks
    if not error:
        # valid percentage split?
        if is_percentage(split):
            # valid user sharing with?
            if our_users.is_connection(user_id=shared_with_user):

                # figure out percentage split
                loaned_amount = round((float(amount) * (100-float(split)))/100, 2)
                loaned_then_amount = round((float(expense[0].amount) * (100-float(expense[3])))/100, 2)

                # is our original and current account the same?
                if expense[0].deduct_from == account_id:
                    # modify the difference between then and now
                    our_accounts.modify_user_balance(amount=expense[0].amount-float(amount), account_id=account_id)
                else:
                    # credit our original account back
                    our_accounts.modify_user_balance(amount=expense[0].amount, account_id=expense[0].deduct_from)

                    # debit from our current account
                    our_accounts.modify_user_balance(amount=-float(amount), account_id=account_id)

                our_loans = Loans(current_user_id)
                their_loans = Loans(expense[2])

                # get slug as a unique identifier
                slug = our_loans.get_loan_slug(loan_id=expense[1])

                # are we sharing the expense with the same user as before?
                if expense[2] == int(shared_with_user):

                    # edit the loan entries
                    our_loans.edit_loan(other_user_id=shared_with_user, account_id=account_id, description=description,
                                        amount=-float(loaned_amount), date=date, loan_id=expense[1])

                    their_loans.edit_loan(other_user_id=current_user_id, amount=loaned_amount, date=date, slug=slug)

                    # modify our loan account balance difference with the user
                    our_accounts.modify_loan_balance(amount=loaned_amount - loaned_then_amount,
                                                     with_user_id=shared_with_user)

                    # now for the user we share with
                    their_expenses = Expenses(shared_with_user)
                    their_accounts = Accounts(shared_with_user)

                    # the user now and then is the same, get their expense
                    their_expense = their_expenses.get_expense(loan_id=expense[1])

                    # modify their loan account balance
                    their_accounts.modify_loan_balance(amount=-loaned_amount + loaned_then_amount,
                                                       with_user_id=current_user_id)

                    # edit their expense amount
                    their_expenses.edit_expense(date=date, account_id=account_id, amount=loaned_amount,
                                                expense_id=their_expense[0].id, pass_thru=True)

                    # update loan links
                    our_expenses.modify_loan_link(loan_id=expense[1], percentage=split, original_amount=amount)
                    their_expenses.modify_loan_link(loan_id=expense[1], percentage=split, original_amount=amount)

                else:
                    # the other user we WERE sharing with
                    their_expenses = Expenses(expense[2])
                    their_accounts = Accounts(expense[2])

                    # credit our loan account
                    our_accounts.modify_loan_balance(amount=-float(loaned_then_amount), with_user_id=expense[2])

                    # fetch their expense
                    their_expense = their_expenses.get_expense(loan_id=expense[1])

                    # delete the shared user's expense
                    their_expenses.delete_expense(expense_id=their_expense[0].id)

                    # modify their loan status towards us
                    their_accounts.modify_loan_balance(amount=loaned_then_amount, with_user_id=current_user_id)

                    # unlink expenses to loan entries
                    our_expenses.unlink_loan(loan_id=expense[1])

                    # delete the original loans
                    our_loans.delete_loan(loan_id=expense[1])
                    their_loans.delete_loan(slug=slug)

                    flash('Loan reverted')

                    # create a loan from us to the new user
                    our_loan_id = our_loans.add_loan(other_user_id=shared_with_user, date=date, account_id=account_id,
                                                 description=description, amount=-float(loaned_amount))

                    # create a loan entry for the new user
                    their_loans = Loans(shared_with_user)
                    their_loan_id = their_loans.add_loan(other_user_id=current_user_id, date=date,
                                                       description=description, amount=loaned_amount)

                    # generate slugs for the new loans
                    our_slugs = Slugs(current_user_id)
                    slug = our_slugs.add_slug(type='loan', object_id=our_loan_id, description=description)
                    their_slugs = Slugs(shared_with_user)
                    their_slugs.add_slug(type='loan', object_id=their_loan_id, slug=slug)

                    flash('Loan given')

                    # modify loan monies for us
                    our_accounts.modify_loan_balance(amount=loaned_amount, with_user_id=shared_with_user)

                    # the CURRENT user we are sharing with
                    their_accounts = Accounts(shared_with_user)

                    # fudge loan 'account' monies for them
                    their_accounts.modify_loan_balance(amount=-float(loaned_amount), with_user_id=current_user_id)

                    their_expenses = Expenses(shared_with_user)

                    # add new expenses (borrower)
                    their_expense_id = their_expenses.add_expense(date=date, amount=loaned_amount,
                                                                  description=description, pass_thru=True)

                    # create new loan - expense links
                    our_expenses.link_to_loan(expense_id=expense_id, loan_id=our_loan_id, shared_with=shared_with_user,
                                              percentage=split, original_amount=amount)
                    their_expenses.link_to_loan(expense_id=their_expense_id, loan_id=our_loan_id, shared_with=current_user_id,
                                                percentage=split, original_amount=amount)

                # edit expense (loaner - us)
                our_expenses.edit_expense(date=date, category_id=category_id, account_id=account_id,
                                          amount=float(amount) - loaned_amount, description=description,
                                          expense_id=expense_id)

                flash('Expense edited')

                # do a GET otherwise category will fail
                return redirect(url_for('expenses.edit_expense', expense_id=expense_id))

            else: error = 'Not a valid user sharing with'
        else: error = 'Not a valid % split'

    # show the form
    return render_template('admin_edit_expense.html', **locals())
Exemple #5
0
def __edit_simple_expense_into_shared(current_user_id, our_expenses, expense, our_accounts, our_users, date, description,
                                      account_id, amount, error, category_id, categories, accounts, users, expense_id,
                                      key=None, dict=None):
    '''Edit a simple expense entry into a shared one'''

    # fetch values and check they are actually provided
    if 'split' in request.form: split = request.form['split']
    else: error = 'You need to provide a % split'
    if 'user' in request.form: shared_with_user = request.form['user']
    else: error = 'You need to provide a user'

    # 'heavier' checks
    if not error:
        # valid percentage split?
        if is_percentage(split):
            # valid user sharing with?
            if our_users.is_connection(user_id=shared_with_user):

                # figure out percentage split
                loaned_amount = round((float(amount)*(100-float(split)))/100, 2)

                # create loans
                our_loans = Loans(current_user_id)
                our_loan_id = our_loans.add_loan(other_user_id=shared_with_user, date=date,
                                                 account_id=account_id, description=description,
                                                 amount=-float(loaned_amount))

                their_loans = Loans(shared_with_user)
                their_loan_id = their_loans.add_loan(other_user_id=current_user_id, date=date,
                                                   description=description, amount=loaned_amount)

                # generate slugs for the new loans
                our_slugs = Slugs(current_user_id)
                slug = our_slugs.add_slug(type='loan', object_id=our_loan_id,
                                          description=description)
                their_slugs = Slugs(shared_with_user)
                their_slugs.add_slug(type='loan', object_id=their_loan_id, slug=slug)

                flash('Loan given')

                # is our original and current account the same?
                if expense[0].deduct_from == account_id:
                    # modify the difference between then and now
                    our_accounts.modify_user_balance(amount=expense[0].amount-float(amount), account_id=account_id)
                else:
                    # credit our original account back
                    our_accounts.modify_user_balance(amount=expense[0].amount, account_id=expense[0].deduct_from)

                    # debit from our current account
                    our_accounts.modify_user_balance(amount=-float(amount), account_id=account_id)

                # edit expense (loaner - us)
                our_expenses.edit_expense(date=date, category_id=category_id, account_id=account_id,
                                          amount=float(amount) - loaned_amount, description=description,
                                          expense_id=expense_id)

                their_expenses = Expenses(shared_with_user)
                their_accounts = Accounts(shared_with_user)

                # add new expenses (borrower)
                their_expense_id = their_expenses.add_expense(date=date, amount=loaned_amount,
                                                              description=description, pass_thru=True)

                # modify their loan account balance
                their_accounts.modify_loan_balance(amount=-float(loaned_amount), with_user_id=current_user_id)

                # modify our loan account balance
                our_accounts.modify_loan_balance(amount=loaned_amount, with_user_id=shared_with_user)

                # link loan and the expenses
                our_expenses.link_to_loan(expense_id=expense_id, loan_id=our_loan_id, shared_with=shared_with_user,
                                          percentage=split, original_amount=amount)
                their_expenses.link_to_loan(expense_id=their_expense_id, loan_id=our_loan_id,
                                            shared_with=current_user_id, percentage=split, original_amount=amount)

                flash('Expense edited')

                # do a GET otherwise category will fail
                return redirect(url_for('expenses.edit_expense', expense_id=expense_id))

            else: error = 'Not a valid user sharing with'
        else: error = 'Not a valid % split'

    # show the form
    return render_template('admin_edit_expense.html', **locals())
Exemple #6
0
def __edit_shared_expense_into_shared(current_user_id,
                                      our_expenses,
                                      expense,
                                      our_accounts,
                                      our_users,
                                      date,
                                      description,
                                      account_id,
                                      amount,
                                      error,
                                      category_id,
                                      categories,
                                      accounts,
                                      users,
                                      expense_id,
                                      key=None,
                                      dict=None):
    '''Edit a shared expense entry into a shared entry still'''

    # fetch values and check they are actually provided
    if 'split' in request.form: split = request.form['split']
    else: error = 'You need to provide a % split'
    if 'user' in request.form: shared_with_user = request.form['user']
    else: error = 'You need to provide a user'

    # 'heavier' checks
    if not error:
        # valid percentage split?
        if is_percentage(split):
            # valid user sharing with?
            if our_users.is_connection(user_id=shared_with_user):

                # figure out percentage split
                loaned_amount = round(
                    (float(amount) * (100 - float(split))) / 100, 2)
                loaned_then_amount = round((float(expense[0].amount) *
                                            (100 - float(expense[3]))) / 100,
                                           2)

                # is our original and current account the same?
                if expense[0].deduct_from == account_id:
                    # modify the difference between then and now
                    our_accounts.modify_user_balance(amount=expense[0].amount -
                                                     float(amount),
                                                     account_id=account_id)
                else:
                    # credit our original account back
                    our_accounts.modify_user_balance(
                        amount=expense[0].amount,
                        account_id=expense[0].deduct_from)

                    # debit from our current account
                    our_accounts.modify_user_balance(amount=-float(amount),
                                                     account_id=account_id)

                our_loans = Loans(current_user_id)
                their_loans = Loans(expense[2])

                # get slug as a unique identifier
                slug = our_loans.get_loan_slug(loan_id=expense[1])

                # are we sharing the expense with the same user as before?
                if expense[2] == int(shared_with_user):

                    # edit the loan entries
                    our_loans.edit_loan(other_user_id=shared_with_user,
                                        account_id=account_id,
                                        description=description,
                                        amount=-float(loaned_amount),
                                        date=date,
                                        loan_id=expense[1])

                    their_loans.edit_loan(other_user_id=current_user_id,
                                          amount=loaned_amount,
                                          date=date,
                                          slug=slug)

                    # modify our loan account balance difference with the user
                    our_accounts.modify_loan_balance(
                        amount=loaned_amount - loaned_then_amount,
                        with_user_id=shared_with_user)

                    # now for the user we share with
                    their_expenses = Expenses(shared_with_user)
                    their_accounts = Accounts(shared_with_user)

                    # the user now and then is the same, get their expense
                    their_expense = their_expenses.get_expense(
                        loan_id=expense[1])

                    # modify their loan account balance
                    their_accounts.modify_loan_balance(
                        amount=-loaned_amount + loaned_then_amount,
                        with_user_id=current_user_id)

                    # edit their expense amount
                    their_expenses.edit_expense(date=date,
                                                account_id=account_id,
                                                amount=loaned_amount,
                                                expense_id=their_expense[0].id,
                                                pass_thru=True)

                    # update loan links
                    our_expenses.modify_loan_link(loan_id=expense[1],
                                                  percentage=split,
                                                  original_amount=amount)
                    their_expenses.modify_loan_link(loan_id=expense[1],
                                                    percentage=split,
                                                    original_amount=amount)

                else:
                    # the other user we WERE sharing with
                    their_expenses = Expenses(expense[2])
                    their_accounts = Accounts(expense[2])

                    # credit our loan account
                    our_accounts.modify_loan_balance(
                        amount=-float(loaned_then_amount),
                        with_user_id=expense[2])

                    # fetch their expense
                    their_expense = their_expenses.get_expense(
                        loan_id=expense[1])

                    # delete the shared user's expense
                    their_expenses.delete_expense(
                        expense_id=their_expense[0].id)

                    # modify their loan status towards us
                    their_accounts.modify_loan_balance(
                        amount=loaned_then_amount,
                        with_user_id=current_user_id)

                    # unlink expenses to loan entries
                    our_expenses.unlink_loan(loan_id=expense[1])

                    # delete the original loans
                    our_loans.delete_loan(loan_id=expense[1])
                    their_loans.delete_loan(slug=slug)

                    flash('Loan reverted')

                    # create a loan from us to the new user
                    our_loan_id = our_loans.add_loan(
                        other_user_id=shared_with_user,
                        date=date,
                        account_id=account_id,
                        description=description,
                        amount=-float(loaned_amount))

                    # create a loan entry for the new user
                    their_loans = Loans(shared_with_user)
                    their_loan_id = their_loans.add_loan(
                        other_user_id=current_user_id,
                        date=date,
                        description=description,
                        amount=loaned_amount)

                    # generate slugs for the new loans
                    our_slugs = Slugs(current_user_id)
                    slug = our_slugs.add_slug(type='loan',
                                              object_id=our_loan_id,
                                              description=description)
                    their_slugs = Slugs(shared_with_user)
                    their_slugs.add_slug(type='loan',
                                         object_id=their_loan_id,
                                         slug=slug)

                    flash('Loan given')

                    # modify loan monies for us
                    our_accounts.modify_loan_balance(
                        amount=loaned_amount, with_user_id=shared_with_user)

                    # the CURRENT user we are sharing with
                    their_accounts = Accounts(shared_with_user)

                    # fudge loan 'account' monies for them
                    their_accounts.modify_loan_balance(
                        amount=-float(loaned_amount),
                        with_user_id=current_user_id)

                    their_expenses = Expenses(shared_with_user)

                    # add new expenses (borrower)
                    their_expense_id = their_expenses.add_expense(
                        date=date,
                        amount=loaned_amount,
                        description=description,
                        pass_thru=True)

                    # create new loan - expense links
                    our_expenses.link_to_loan(expense_id=expense_id,
                                              loan_id=our_loan_id,
                                              shared_with=shared_with_user,
                                              percentage=split,
                                              original_amount=amount)
                    their_expenses.link_to_loan(expense_id=their_expense_id,
                                                loan_id=our_loan_id,
                                                shared_with=current_user_id,
                                                percentage=split,
                                                original_amount=amount)

                # edit expense (loaner - us)
                our_expenses.edit_expense(date=date,
                                          category_id=category_id,
                                          account_id=account_id,
                                          amount=float(amount) - loaned_amount,
                                          description=description,
                                          expense_id=expense_id)

                flash('Expense edited')

                # do a GET otherwise category will fail
                return redirect(
                    url_for('expenses.edit_expense', expense_id=expense_id))

            else:
                error = 'Not a valid user sharing with'
        else:
            error = 'Not a valid % split'

    # show the form
    return render_template('admin_edit_expense.html', **locals())
Exemple #7
0
def __edit_simple_expense_into_shared(current_user_id,
                                      our_expenses,
                                      expense,
                                      our_accounts,
                                      our_users,
                                      date,
                                      description,
                                      account_id,
                                      amount,
                                      error,
                                      category_id,
                                      categories,
                                      accounts,
                                      users,
                                      expense_id,
                                      key=None,
                                      dict=None):
    '''Edit a simple expense entry into a shared one'''

    # fetch values and check they are actually provided
    if 'split' in request.form: split = request.form['split']
    else: error = 'You need to provide a % split'
    if 'user' in request.form: shared_with_user = request.form['user']
    else: error = 'You need to provide a user'

    # 'heavier' checks
    if not error:
        # valid percentage split?
        if is_percentage(split):
            # valid user sharing with?
            if our_users.is_connection(user_id=shared_with_user):

                # figure out percentage split
                loaned_amount = round(
                    (float(amount) * (100 - float(split))) / 100, 2)

                # create loans
                our_loans = Loans(current_user_id)
                our_loan_id = our_loans.add_loan(
                    other_user_id=shared_with_user,
                    date=date,
                    account_id=account_id,
                    description=description,
                    amount=-float(loaned_amount))

                their_loans = Loans(shared_with_user)
                their_loan_id = their_loans.add_loan(
                    other_user_id=current_user_id,
                    date=date,
                    description=description,
                    amount=loaned_amount)

                # generate slugs for the new loans
                our_slugs = Slugs(current_user_id)
                slug = our_slugs.add_slug(type='loan',
                                          object_id=our_loan_id,
                                          description=description)
                their_slugs = Slugs(shared_with_user)
                their_slugs.add_slug(type='loan',
                                     object_id=their_loan_id,
                                     slug=slug)

                flash('Loan given')

                # is our original and current account the same?
                if expense[0].deduct_from == account_id:
                    # modify the difference between then and now
                    our_accounts.modify_user_balance(amount=expense[0].amount -
                                                     float(amount),
                                                     account_id=account_id)
                else:
                    # credit our original account back
                    our_accounts.modify_user_balance(
                        amount=expense[0].amount,
                        account_id=expense[0].deduct_from)

                    # debit from our current account
                    our_accounts.modify_user_balance(amount=-float(amount),
                                                     account_id=account_id)

                # edit expense (loaner - us)
                our_expenses.edit_expense(date=date,
                                          category_id=category_id,
                                          account_id=account_id,
                                          amount=float(amount) - loaned_amount,
                                          description=description,
                                          expense_id=expense_id)

                their_expenses = Expenses(shared_with_user)
                their_accounts = Accounts(shared_with_user)

                # add new expenses (borrower)
                their_expense_id = their_expenses.add_expense(
                    date=date,
                    amount=loaned_amount,
                    description=description,
                    pass_thru=True)

                # modify their loan account balance
                their_accounts.modify_loan_balance(
                    amount=-float(loaned_amount), with_user_id=current_user_id)

                # modify our loan account balance
                our_accounts.modify_loan_balance(amount=loaned_amount,
                                                 with_user_id=shared_with_user)

                # link loan and the expenses
                our_expenses.link_to_loan(expense_id=expense_id,
                                          loan_id=our_loan_id,
                                          shared_with=shared_with_user,
                                          percentage=split,
                                          original_amount=amount)
                their_expenses.link_to_loan(expense_id=their_expense_id,
                                            loan_id=our_loan_id,
                                            shared_with=current_user_id,
                                            percentage=split,
                                            original_amount=amount)

                flash('Expense edited')

                # do a GET otherwise category will fail
                return redirect(
                    url_for('expenses.edit_expense', expense_id=expense_id))

            else:
                error = 'Not a valid user sharing with'
        else:
            error = 'Not a valid % split'

    # show the form
    return render_template('admin_edit_expense.html', **locals())
Exemple #8
0
def add_expense():
    '''Add an expense entry'''

    error = None
    current_user_id = session.get('logged_in_user')

    our_accounts = Accounts(current_user_id)
    our_expenses = Expenses(current_user_id)
    users = Users(current_user_id)

    if request.method == 'POST':
        dict = __validate_expense_form()
        for key in dict.keys():
            exec(key + " = dict['" + key + "']")

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

                            # is it a shared expense?
                            if 'is_shared' in request.form:
                                # fetch values and check they are actually provided
                                if 'split' in request.form:
                                    split = request.form['split']
                                else:
                                    error = 'You need to provide a % split'
                                if 'user' in request.form:
                                    shared_with_user = request.form['user']
                                else:
                                    error = 'You need to provide a user'

                                # 'heavier' checks
                                if not error:
                                    # valid percentage split?
                                    if is_percentage(split):
                                        # valid user sharing with?
                                        if users.is_connection(
                                                user_id=shared_with_user):

                                            # figure out percentage split
                                            loaned_amount = round(
                                                (float(amount) *
                                                 (100 - float(split))) / 100,
                                                2)

                                            # create loans
                                            our_loans = Loans(current_user_id)
                                            our_loan_id = our_loans.add_loan(
                                                other_user_id=shared_with_user,
                                                date=date,
                                                account_id=account_id,
                                                description=description,
                                                amount=-float(loaned_amount))

                                            our_loans = Loans(shared_with_user)
                                            their_loan_id = our_loans.add_loan(
                                                other_user_id=current_user_id,
                                                date=date,
                                                description=description,
                                                amount=loaned_amount)

                                            # generate slugs for the new loans
                                            our_slugs = Slugs(current_user_id)
                                            slug = our_slugs.add_slug(
                                                type='loan',
                                                object_id=our_loan_id,
                                                description=description)
                                            their_slugs = Slugs(
                                                shared_with_user)
                                            their_slugs.add_slug(
                                                type='loan',
                                                object_id=their_loan_id,
                                                slug=slug)

                                            flash('Loan given')

                                            # add new expense (loaner)
                                            our_expense_id = our_expenses.add_expense(
                                                date=date,
                                                category_id=category_id,
                                                account_id=account_id,
                                                amount=float(amount) -
                                                loaned_amount,
                                                description=description)

                                            # add new expenses (borrower)
                                            their_expenses = Expenses(
                                                shared_with_user)
                                            their_expense_id = their_expenses.add_expense(
                                                date=date,
                                                amount=loaned_amount,
                                                description=description,
                                                pass_thru=True)

                                            # fudge loan 'account' monies
                                            our_accounts.modify_loan_balance(
                                                amount=loaned_amount,
                                                with_user_id=shared_with_user)
                                            their_accounts = Accounts(
                                                shared_with_user)
                                            their_accounts.modify_loan_balance(
                                                amount=-float(loaned_amount),
                                                with_user_id=current_user_id)

                                            # link loan and the expenses (through us)
                                            our_expenses.link_to_loan(
                                                expense_id=our_expense_id,
                                                loan_id=our_loan_id,
                                                shared_with=shared_with_user,
                                                percentage=split,
                                                original_amount=amount)
                                            their_expenses.link_to_loan(
                                                expense_id=their_expense_id,
                                                loan_id=our_loan_id,
                                                shared_with=current_user_id,
                                                percentage=split,
                                                original_amount=amount)

                                        else:
                                            error = 'Not a valid user sharing with'
                                    else:
                                        error = 'Not a valid % split'

                            else:
                                # add new expense
                                our_expenses.add_expense(
                                    date=date,
                                    category_id=category_id,
                                    account_id=account_id,
                                    amount=amount,
                                    description=description)

                            if not error:
                                # debit from account
                                our_accounts.modify_user_balance(
                                    amount=-float(amount),
                                    account_id=account_id)

                                flash('Expense added')

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

    # fetch user's categories, accounts and users
    categories = our_expenses.get_categories()
    if not categories: error = 'You need to define at least one category'

    accounts = our_accounts.get_accounts()
    if not accounts: error = 'You need to define at least one account'

    # fetch users from connections from us
    users = users.get_connections()

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