balance_sheet: Balance.total() accepts multiple account names.

This simplifies the code to total all equity.
This commit is contained in:
Brett Smith 2020-08-18 01:22:29 -04:00
parent e05f55659a
commit aa1f2ea35a
2 changed files with 13 additions and 13 deletions

View file

@ -27,6 +27,7 @@ from pathlib import Path
from typing import ( from typing import (
Any, Any,
Collection,
Dict, Dict,
Hashable, Hashable,
Iterable, Iterable,
@ -38,6 +39,7 @@ from typing import (
Sequence, Sequence,
TextIO, TextIO,
Tuple, Tuple,
Union,
) )
import odf.table # type:ignore[import] import odf.table # type:ignore[import]
@ -127,15 +129,17 @@ class Balances:
self.balances[key] += post.at_cost() self.balances[key] += post.at_cost()
def total(self, def total(self,
account: Optional[str]=None, account: Union[None, str, Collection[str]]=None,
classification: Optional[str]=None, classification: Optional[str]=None,
period: int=Period.ANY, period: int=Period.ANY,
fund: int=Fund.ANY, fund: int=Fund.ANY,
post_type: Optional[str]=None, post_type: Optional[str]=None,
) -> core.Balance: ) -> core.Balance:
if isinstance(account, str):
account = (account,)
retval = core.MutableBalance() retval = core.MutableBalance()
for key, balance in self.balances.items(): for key, balance in self.balances.items():
if not (account is None or key.account.is_under(account)): if not (account is None or key.account.is_under(*account)):
pass pass
elif not (classification is None elif not (classification is None
or key.classification.is_under(classification)): or key.classification.is_under(classification)):
@ -328,13 +332,9 @@ class Report(core.BaseODS[Sequence[None], None]):
self.add_row() self.add_row()
for fund in [Fund.UNRESTRICTED, Fund.RESTRICTED]: for fund in [Fund.UNRESTRICTED, Fund.RESTRICTED]:
preposition = "Without" if fund is Fund.UNRESTRICTED else "With" preposition = "Without" if fund is Fund.UNRESTRICTED else "With"
period_bal = -sum( period_bal = -self.balances.total(account=EQUITY_ACCOUNTS, fund=fund)
(self.balances.total(account=account, fund=fund) prior_bal = period_bal + self.balances.total(
for account in EQUITY_ACCOUNTS), core.MutableBalance(), account=EQUITY_ACCOUNTS, fund=fund, period=Period.PERIOD,
)
prior_bal = period_bal + sum(
(self.balances.total(account=account, fund=fund, period=Period.PERIOD)
for account in EQUITY_ACCOUNTS), core.MutableBalance(),
) )
self.add_row( self.add_row(
self.string_cell(f"{preposition} donor restrictions"), self.string_cell(f"{preposition} donor restrictions"),
@ -496,8 +496,7 @@ class Report(core.BaseODS[Sequence[None], None]):
else: else:
kwargs['period'] = Period.OPENING kwargs['period'] = Period.OPENING
beginnings = [ beginnings = [
-sum((self.balances.total(account=account, **kwargs) -self.balances.total(account=EQUITY_ACCOUNTS, **kwargs)
for account in EQUITY_ACCOUNTS), core.MutableBalance())
for kwargs in bal_kwargs for kwargs in bal_kwargs
] ]
self.add_row() self.add_row()
@ -607,8 +606,7 @@ class Report(core.BaseODS[Sequence[None], None]):
self.add_row() self.add_row()
totals = [ totals = [
-sum((self.balances.total(account=account, **kwargs) -self.balances.total(account=EQUITY_ACCOUNTS, **kwargs)
for account in EQUITY_ACCOUNTS), core.MutableBalance())
for kwargs in bal_kwargs for kwargs in bal_kwargs
] ]
self.add_row( self.add_row(

View file

@ -94,6 +94,8 @@ def income_expense_balances():
({'fund': Fund.RESTRICTED, 'post_type': 'program'}, 10), ({'fund': Fund.RESTRICTED, 'post_type': 'program'}, 10),
({'period': Period.PRIOR, 'fund': Fund.RESTRICTED, 'post_type': 'program'}, '4.80'), ({'period': Period.PRIOR, 'fund': Fund.RESTRICTED, 'post_type': 'program'}, '4.80'),
({'period': Period.PERIOD, 'fund': Fund.RESTRICTED, 'post_type': 'ø'}, None), ({'period': Period.PERIOD, 'fund': Fund.RESTRICTED, 'post_type': 'ø'}, None),
({'account': ('Income', 'Expenses')}, 30),
({'account': ('Income', 'Expenses'), 'fund': Fund.UNRESTRICTED}, 15),
]) ])
def test_balance_total(income_expense_balances, kwargs, expected): def test_balance_total(income_expense_balances, kwargs, expected):
actual = income_expense_balances.total(**kwargs) actual = income_expense_balances.total(**kwargs)