Skip to content

Commit

Permalink
issue #104 - add properties to Account and OFXTransaction necessary f…
Browse files Browse the repository at this point in the history
…or payoff calculations
  • Loading branch information
jantman committed Aug 13, 2017
1 parent 89d4e7b commit 14a3d9b
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 1 deletion.
21 changes: 21 additions & 0 deletions biweeklybudget/models/account.py
Expand Up @@ -45,6 +45,7 @@
from biweeklybudget.models.base import Base, ModelAsDict
from biweeklybudget.models.account_balance import AccountBalance
from biweeklybudget.models.transaction import Transaction
from biweeklybudget.models.ofx_transaction import OFXTransaction
from biweeklybudget.utils import dtnow
import json
import enum
Expand Down Expand Up @@ -306,3 +307,23 @@ def billing_period_class_args_deserialized(self):
return json.loads(self.billing_period_class_args)
except Exception:
return {}

@property
def latest_ofx_interest_charge(self):
"""
Return the most recent interest charge from OFX data.
:return: latest interest charge
:rtype: biweeklybudget.models.OFXTransaction
"""
sess = inspect(self).session
t = sess.query(OFXTransaction).filter(
or_(
OFXTransaction.name.op('regexp')(self.re_interest_charge),
OFXTransaction.description.op('regexp')(
self.re_interest_charge
),
),
OFXTransaction.account.__eq__(self),
).order_by(OFXTransaction.date_posted.desc()).first()
return t
17 changes: 16 additions & 1 deletion biweeklybudget/models/ofx_transaction.py
Expand Up @@ -37,7 +37,7 @@

from sqlalchemy import (
Column, String, PrimaryKeyConstraint, Text, Numeric, Boolean, ForeignKey,
Integer
Integer, inspect
)
from sqlalchemy.sql.expression import null
from sqlalchemy_utc import UtcDateTime
Expand All @@ -48,6 +48,7 @@
from decimal import Decimal

from biweeklybudget.models.base import Base, ModelAsDict
from biweeklybudget.models.ofx_statement import OFXStatement
from biweeklybudget.settings import RECONCILE_BEGIN_DATE

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -213,3 +214,17 @@ def unreconciled(db):
OFXTransaction.date_posted.__ge__(cutoff_date),
OFXTransaction.account.has(reconcile_trans=True)
)

@property
def first_statement_by_date(self):
"""
Return the first OFXStatement on or after `self.date_posted`.
:return: first OFXStatement on or after `self.date_posted`
:rtype: biweeklybudget.models.OFXStatement
"""
sess = inspect(self).session
return sess.query(OFXStatement).filter(
OFXStatement.account.__eq__(self.account),
OFXStatement.as_of.__ge__(self.date_posted)
).order_by(OFXStatement.as_of.asc()).first()
36 changes: 36 additions & 0 deletions biweeklybudget/tests/acceptance/models/__init__.py
@@ -0,0 +1,36 @@
"""
The latest version of this package is available at:
<http://github.com/jantman/biweeklybudget>
################################################################################
Copyright 2016 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
This file is part of biweeklybudget, also known as biweeklybudget.
biweeklybudget is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
biweeklybudget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with biweeklybudget. If not, see <http://www.gnu.org/licenses/>.
The Copyright and Authors attributions contained herein may not be removed or
otherwise altered, except to add the Author attribution of a contributor to
this work. (Additional Terms pursuant to Section 7b of the AGPL v3)
################################################################################
While not legally required, I sincerely request that anyone who finds
bugs please submit them at <https://github.com/jantman/biweeklybudget> or
to me via email, and that you send any contributions or improvements
either as a pull request on GitHub, or to me via email.
################################################################################
AUTHORS:
Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
################################################################################
"""
67 changes: 67 additions & 0 deletions biweeklybudget/tests/acceptance/models/test_account.py
@@ -0,0 +1,67 @@
"""
The latest version of this package is available at:
<http://github.com/jantman/biweeklybudget>
################################################################################
Copyright 2016 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
This file is part of biweeklybudget, also known as biweeklybudget.
biweeklybudget is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
biweeklybudget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with biweeklybudget. If not, see <http://www.gnu.org/licenses/>.
The Copyright and Authors attributions contained herein may not be removed or
otherwise altered, except to add the Author attribution of a contributor to
this work. (Additional Terms pursuant to Section 7b of the AGPL v3)
################################################################################
While not legally required, I sincerely request that anyone who finds
bugs please submit them at <https://github.com/jantman/biweeklybudget> or
to me via email, and that you send any contributions or improvements
either as a pull request on GitHub, or to me via email.
################################################################################
AUTHORS:
Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
################################################################################
"""

import pytest
from decimal import Decimal

from biweeklybudget.tests.acceptance_helpers import AcceptanceHelper
from biweeklybudget.models.account import Account
from biweeklybudget.models.ofx_transaction import OFXTransaction


@pytest.mark.acceptance
class TestLastOFXInterestCharge(AcceptanceHelper):

def test_credit_one(self, testdb):
acct = testdb.query(Account).get(3)
latest_interest = acct.latest_ofx_interest_charge
assert latest_interest is not None
assert isinstance(latest_interest, OFXTransaction)
assert latest_interest.account_id == 3
assert latest_interest.fitid == 'T3'
assert latest_interest.account_amount == Decimal('16.25')
assert latest_interest.statement_id == 4

def test_credit_two(self, testdb):
acct = testdb.query(Account).get(4)
latest_interest = acct.latest_ofx_interest_charge
assert latest_interest is not None
assert isinstance(latest_interest, OFXTransaction)
assert latest_interest.account_id == 4
assert latest_interest.fitid == '001'
assert latest_interest.account_amount == Decimal('28.53')
assert latest_interest.statement_id == 6
53 changes: 53 additions & 0 deletions biweeklybudget/tests/acceptance/models/test_ofx_transaction.py
@@ -0,0 +1,53 @@
"""
################################################################################
Copyright 2016 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
This file is part of biweeklybudget, also known as biweeklybudget.
biweeklybudget is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
biweeklybudget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with biweeklybudget. If not, see <http://www.gnu.org/licenses/>.
The Copyright and Authors attributions contained herein may not be removed or
otherwise altered, except to add the Author attribution of a contributor to
this work. (Additional Terms pursuant to Section 7b of the AGPL v3)
################################################################################
While not legally required, I sincerely request that anyone who finds
bugs please submit them at <https://github.com/jantman/biweeklybudget> or
to me via email, and that you send any contributions or improvements
either as a pull request on GitHub, or to me via email.
################################################################################
AUTHORS:
Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
################################################################################
"""

import pytest
from decimal import Decimal

from biweeklybudget.tests.acceptance_helpers import AcceptanceHelper
from biweeklybudget.models.ofx_transaction import OFXTransaction


@pytest.mark.acceptance
class TestFirstStatementByDate(AcceptanceHelper):

def test_credit_one(self, testdb):
trans = testdb.query(OFXTransaction).get((3, 'T2'))
assert trans is not None
assert trans.amount == Decimal('52.00')
assert trans.statement_id == 4
assert trans.statement.filename == '/stmt/CreditOne/0'
res = trans.first_statement_by_date
assert res is not None
assert res.filename == '/stmt/CreditOne/3'
13 changes: 13 additions & 0 deletions biweeklybudget/tests/fixtures/sampledata.py
Expand Up @@ -427,6 +427,19 @@ def _credit_one(self):
as_of=(self.dt - timedelta(days=30, hours=13)),
ledger_bal=-876.54,
ledger_bal_as_of=(self.dt - timedelta(days=30, hours=13)),
),
OFXStatement(
account=acct,
filename='/stmt/CreditOne/3',
file_mtime=(self.dt - timedelta(days=1)),
currency='USD',
bankid='CreditOne',
acct_type='Credit',
acctid='CreditOneAcctId',
type='Credit',
as_of=(self.dt - timedelta(days=1)),
ledger_bal=-435.29,
ledger_bal_as_of=(self.dt - timedelta(days=1)),
)
]
transactions = {
Expand Down

0 comments on commit 14a3d9b

Please sign in to comment.