예제 #1
0
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. There are places
    where we rely on this function not committing 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,
                     budget_amounts={from_budget: amount},
                     budgeted_amount=amount,
                     description=desc,
                     account=account,
                     notes=notes,
                     planned_budget=from_budget)
    db_sess.add(t1)
    t2 = Transaction(date=txn_date,
                     budget_amounts={to_budget: (-1 * amount)},
                     budgeted_amount=(-1 * amount),
                     description=desc,
                     account=account,
                     notes=notes,
                     planned_budget=to_budget)
    db_sess.add(t2)
    t1.transfer = t2
    db_sess.add(t1)
    t2.transfer = t1
    db_sess.add(t2)
    db_sess.add(TxnReconcile(transaction=t1, note=desc))
    db_sess.add(TxnReconcile(transaction=t2, note=desc))
    return [t1, t2]
예제 #2
0
    def submit(self, data):
        """
        Handle form submission; create or update models in the DB. Raises an
        Exception for any errors.

        :param data: submitted form data
        :type data: dict
        :return: message describing changes to DB (i.e. link to created record)
        :rtype: str
        """
        # get the data
        trans_date = datetime.strptime(data['date'], '%Y-%m-%d').date()
        amt = Decimal(data['amount'])
        from_acct = db_session.query(Account).get(int(data['from_account']))
        if from_acct is None:
            raise RuntimeError("Error: no Account with ID %s" %
                               data['from_account'])
        to_acct = db_session.query(Account).get(int(data['to_account']))
        if to_acct is None:
            raise RuntimeError("Error: no Account with ID %s" %
                               data['to_account'])
        budget = db_session.query(Budget).get(int(data['budget']))
        if budget is None:
            raise RuntimeError("Error: no Budget with ID %s" % data['budget'])
        notes = data['notes'].strip()
        desc = 'Account Transfer - %s from %s (%d) to %s (%d)' % (
            amt, from_acct.name, from_acct.id, to_acct.name, to_acct.id)
        logger.info(desc)
        t1 = Transaction(date=trans_date,
                         budget_amounts={budget: amt},
                         budgeted_amount=amt,
                         description=desc,
                         account=from_acct,
                         notes=notes,
                         planned_budget=budget)
        db_session.add(t1)
        t2 = Transaction(date=trans_date,
                         budget_amounts={budget: (-1 * amt)},
                         budgeted_amount=(-1 * amt),
                         description=desc,
                         account=to_acct,
                         notes=notes,
                         planned_budget=budget)
        db_session.add(t2)
        t1.transfer = t2
        db_session.add(t1)
        t2.transfer = t1
        db_session.add(t2)
        db_session.commit()
        return 'Successfully saved Transactions %d and %d in database.' % (
            t1.id, t2.id)