core.RelatedPostings: Add iter_with_balance method.
payment-report and accrual-report query to find the last date a series of postings had a non/zero balance. This method is a good building block for that.
This commit is contained in:
parent
5aa30e5456
commit
b28646aa12
2 changed files with 87 additions and 49 deletions
|
@ -121,8 +121,16 @@ class RelatedPostings(Sequence[data.Posting]):
|
||||||
def add(self, post: data.Posting) -> None:
|
def add(self, post: data.Posting) -> None:
|
||||||
self._postings.append(post)
|
self._postings.append(post)
|
||||||
|
|
||||||
def balance(self) -> Balance:
|
def iter_with_balance(self) -> Iterable[Tuple[data.Posting, Balance]]:
|
||||||
balance = MutableBalance()
|
balance = MutableBalance()
|
||||||
for post in self:
|
for post in self:
|
||||||
balance.add_amount(post.units)
|
balance.add_amount(post.units)
|
||||||
|
yield post, balance
|
||||||
|
|
||||||
|
def balance(self) -> Balance:
|
||||||
|
for _, balance in self.iter_with_balance():
|
||||||
|
pass
|
||||||
|
try:
|
||||||
return balance
|
return balance
|
||||||
|
except NameError:
|
||||||
|
return Balance()
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import collections
|
||||||
import datetime
|
import datetime
|
||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
|
@ -26,58 +27,87 @@ from . import testutil
|
||||||
from conservancy_beancount import data
|
from conservancy_beancount import data
|
||||||
from conservancy_beancount.reports import core
|
from conservancy_beancount.reports import core
|
||||||
|
|
||||||
_day_counter = itertools.count(1)
|
def date_seq(date=testutil.FY_MID_DATE, step=1):
|
||||||
def next_date():
|
while True:
|
||||||
return testutil.FY_MID_DATE + datetime.timedelta(next(_day_counter))
|
yield date
|
||||||
|
date = date + datetime.timedelta(days=step)
|
||||||
|
|
||||||
def txn_pair(acct, src_acct, dst_acct, amount, date=None, txn_meta={}, post_meta={}):
|
def accruals_and_payments(acct, src_acct, dst_acct, start_date, *amounts):
|
||||||
if date is None:
|
dates = date_seq(start_date)
|
||||||
date = next_date()
|
for amt, currency in amounts:
|
||||||
src_txn = testutil.Transaction(date=date, **txn_meta, postings=[
|
yield testutil.Transaction(date=next(dates), postings=[
|
||||||
(acct, amount, post_meta.copy()),
|
(acct, amt, currency),
|
||||||
(src_acct, -amount),
|
(dst_acct if amt < 0 else src_acct, -amt, currency),
|
||||||
])
|
|
||||||
dst_date = date + datetime.timedelta(days=1)
|
|
||||||
dst_txn = testutil.Transaction(date=dst_date, **txn_meta, postings=[
|
|
||||||
(acct, -amount, post_meta.copy()),
|
|
||||||
(dst_acct, amount),
|
|
||||||
])
|
|
||||||
return (src_txn, dst_txn)
|
|
||||||
|
|
||||||
def donation(amount, currency='USD', date=None, other_acct='Assets:Cash', **meta):
|
|
||||||
if date is None:
|
|
||||||
date = next_date()
|
|
||||||
return testutil.Transaction(date=date, postings=[
|
|
||||||
('Income:Donations', -amount, currency, meta),
|
|
||||||
(other_acct, amount, currency),
|
|
||||||
])
|
])
|
||||||
|
|
||||||
def test_balance():
|
@pytest.fixture
|
||||||
related = core.RelatedPostings()
|
def credit_card_cycle():
|
||||||
related.add(data.Posting.from_beancount(donation(10), 0))
|
return list(accruals_and_payments(
|
||||||
assert related.balance() == testutil.balance_map(USD=-10)
|
'Liabilities:CreditCard',
|
||||||
related.add(data.Posting.from_beancount(donation(15), 0))
|
'Assets:Checking',
|
||||||
assert related.balance() == testutil.balance_map(USD=-25)
|
'Expenses:Other',
|
||||||
related.add(data.Posting.from_beancount(donation(20), 0))
|
datetime.date(2020, 4, 1),
|
||||||
assert related.balance() == testutil.balance_map(USD=-45)
|
(-110, 'USD'),
|
||||||
|
(110, 'USD'),
|
||||||
|
(-120, 'USD'),
|
||||||
|
(120, 'USD'),
|
||||||
|
))
|
||||||
|
|
||||||
def test_balance_zero():
|
@pytest.fixture
|
||||||
related = core.RelatedPostings()
|
def two_accruals_three_payments():
|
||||||
related.add(data.Posting.from_beancount(donation(10), 0))
|
return list(accruals_and_payments(
|
||||||
related.add(data.Posting.from_beancount(donation(-10), 0))
|
'Assets:Receivable:Accounts',
|
||||||
assert related.balance().is_zero()
|
'Income:Donations',
|
||||||
|
'Assets:Checking',
|
||||||
|
datetime.date(2020, 4, 10),
|
||||||
|
(440, 'USD'),
|
||||||
|
(-230, 'USD'),
|
||||||
|
(550, 'EUR'),
|
||||||
|
(-210, 'USD'),
|
||||||
|
(-550, 'EUR'),
|
||||||
|
))
|
||||||
|
|
||||||
def test_balance_multiple_currencies():
|
def test_balance_empty():
|
||||||
related = core.RelatedPostings()
|
balance = core.RelatedPostings().balance()
|
||||||
related.add(data.Posting.from_beancount(donation(10, 'GBP'), 0))
|
assert not balance
|
||||||
related.add(data.Posting.from_beancount(donation(15, 'GBP'), 0))
|
assert balance.is_zero()
|
||||||
related.add(data.Posting.from_beancount(donation(20, 'EUR'), 0))
|
|
||||||
related.add(data.Posting.from_beancount(donation(25, 'EUR'), 0))
|
|
||||||
assert related.balance() == testutil.balance_map(EUR=-45, GBP=-25)
|
|
||||||
|
|
||||||
def test_balance_multiple_currencies_one_zero():
|
def test_balance_credit_card(credit_card_cycle):
|
||||||
related = core.RelatedPostings()
|
related = core.RelatedPostings()
|
||||||
related.add(data.Posting.from_beancount(donation(10, 'EUR'), 0))
|
assert related.balance() == testutil.balance_map()
|
||||||
related.add(data.Posting.from_beancount(donation(15, 'USD'), 0))
|
expected = Decimal()
|
||||||
related.add(data.Posting.from_beancount(donation(-10, 'EUR'), 0))
|
for txn in credit_card_cycle:
|
||||||
assert related.balance() == testutil.balance_map(EUR=0, USD=-15)
|
post = txn.postings[0]
|
||||||
|
expected += post.units.number
|
||||||
|
related.add(post)
|
||||||
|
assert related.balance() == testutil.balance_map(USD=expected)
|
||||||
|
assert expected == 0
|
||||||
|
|
||||||
|
def check_iter_with_balance(entries):
|
||||||
|
expect_posts = [txn.postings[0] for txn in entries]
|
||||||
|
expect_balances = []
|
||||||
|
balance_tally = collections.defaultdict(Decimal)
|
||||||
|
related = core.RelatedPostings()
|
||||||
|
for post in expect_posts:
|
||||||
|
number, currency = post.units
|
||||||
|
balance_tally[currency] += number
|
||||||
|
expect_balances.append(testutil.balance_map(balance_tally.items()))
|
||||||
|
related.add(post)
|
||||||
|
for (post, balance), exp_post, exp_balance in zip(
|
||||||
|
related.iter_with_balance(),
|
||||||
|
expect_posts,
|
||||||
|
expect_balances,
|
||||||
|
):
|
||||||
|
assert post is exp_post
|
||||||
|
assert balance == exp_balance
|
||||||
|
assert post is expect_posts[-1]
|
||||||
|
assert related.balance() == expect_balances[-1]
|
||||||
|
|
||||||
|
def test_iter_with_balance_empty():
|
||||||
|
assert not list(core.RelatedPostings().iter_with_balance())
|
||||||
|
|
||||||
|
def test_iter_with_balance_credit_card(credit_card_cycle):
|
||||||
|
check_iter_with_balance(credit_card_cycle)
|
||||||
|
|
||||||
|
def test_iter_with_balance_two_acccruals(two_accruals_three_payments):
|
||||||
|
check_iter_with_balance(two_accruals_three_payments)
|
||||||
|
|
Loading…
Reference in a new issue