Skip to content

Commit

Permalink
issue #117 - refactor budget transfer into reusable method
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Oct 19, 2017
1 parent aa1803d commit 3f6273d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 30 deletions.
63 changes: 62 additions & 1 deletion biweeklybudget/budget_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,74 @@
"""
import logging

from biweeklybudget.biweeklypayperiod import BiweeklyPayPeriod
from biweeklybudget.models.budget_model import Budget
from biweeklybudget.models.account import Account
from biweeklybudget.models.transaction import Transaction
from biweeklybudget.models.txn_reconcile import TxnReconcile
from biweeklybudget.utils import dtnow

logger = logging.getLogger(__name__)


def do_budget_transfer(db_sess, txn_date, amount, account,
from_budget, to_budget, notes=None):
"""
Transfer a given amount from ``from_budget`` to ``to_budget`` on
``txn_date``. This method does NOT commit database changes.
:param db_sess: active database session to use for queries
:type db_sess: sqlalchemy.orm.session.Session
:param txn_date: date to make the transfer Transactions on
:type txn_date: datetime.date
:param amount: amount of money to transfer
:type amount: float
:param account:
:type account: biweeklybudget.models.account.Account
:param from_budget:
:type from_budget: biweeklybudget.models.budget_model.Budget
:param to_budget:
:type to_budget: biweeklybudget.models.budget_model.Budget
:param notes: Notes to add to the Transaction
:type notes: str
:return: list of Transactions created for the transfer
:rtype: :py:obj:`list` of :py:class:`~.Transaction` objects
"""
desc = 'Budget Transfer - %s from %s (%d) to %s (%d)' % (
amount, from_budget.name, from_budget.id, to_budget.name,
to_budget.id
)
logger.info(desc)
t1 = Transaction(
date=txn_date,
actual_amount=amount,
budgeted_amount=amount,
description=desc,
account=account,
budget=from_budget,
notes=notes
)
db_sess.add(t1)
t2 = Transaction(
date=txn_date,
actual_amount=(-1 * amount),
budgeted_amount=(-1 * amount),
description=desc,
account=account,
budget=to_budget,
notes=notes
)
db_sess.add(t2)
db_sess.add(TxnReconcile(
transaction=t1,
note=desc
))
db_sess.add(TxnReconcile(
transaction=t2,
note=desc
))
return [t1, t2]


class BudgetBalancer(object):
"""
Class to encapsulate logic for balancing budgets in a pay period.
Expand Down
33 changes: 4 additions & 29 deletions biweeklybudget/flaskapp/views/budgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from biweeklybudget.models.account import Account
from biweeklybudget.models.transaction import Transaction
from biweeklybudget.models.txn_reconcile import TxnReconcile
from biweeklybudget.budget_balancer import do_budget_transfer

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -289,37 +290,11 @@ def submit(self, data):
to_budget.id
)
logger.info(desc)
t1 = Transaction(
date=trans_date,
actual_amount=amt,
budgeted_amount=amt,
description=desc,
account=acct,
budget=from_budget,
notes=notes
)
db_session.add(t1)
t2 = Transaction(
date=trans_date,
actual_amount=(-1 * amt),
budgeted_amount=(-1 * amt),
description=desc,
account=acct,
budget=to_budget,
notes=notes
)
db_session.add(t2)
db_session.add(TxnReconcile(
transaction=t1,
note=desc
))
db_session.add(TxnReconcile(
transaction=t2,
note=desc
))
res = do_budget_transfer(db_session, trans_date, amt, acct,
from_budget, to_budget, notes=notes)
db_session.commit()
return 'Successfully saved Transactions %d and %d in database.' % (
t1.id, t2.id
res[0].id, res[1].id
)


Expand Down

0 comments on commit 3f6273d

Please sign in to comment.