balance_sheet: Default classification sort by root account.

This provides more consistent presentation across sheets,
especially the financial position+cash flows.
This commit is contained in:
Brett Smith 2020-08-18 00:56:27 -04:00
parent f55b698718
commit b7f9903d1f

View file

@ -51,6 +51,7 @@ from .. import config as configmod
from .. import data from .. import data
from .. import ranges from .. import ranges
EQUITY_ACCOUNTS = frozenset(['Equity', 'Income', 'Expenses'])
PROGNAME = 'balance-sheet-report' PROGNAME = 'balance-sheet-report'
logger = logging.getLogger('conservancy_beancount.tools.balance_sheet') logger = logging.getLogger('conservancy_beancount.tools.balance_sheet')
@ -156,8 +157,13 @@ class Balances:
def classifications(self, def classifications(self,
account: str, account: str,
sort_period: int=Period.PERIOD, sort_period: Optional[int]=None,
) -> Sequence[data.Account]: ) -> Sequence[data.Account]:
if sort_period is None:
if account in EQUITY_ACCOUNTS:
sort_period = Period.PERIOD
else:
sort_period = Period.ANY
class_bals: Mapping[data.Account, core.MutableBalance] \ class_bals: Mapping[data.Account, core.MutableBalance] \
= collections.defaultdict(core.MutableBalance) = collections.defaultdict(core.MutableBalance)
for key, balance in self.balances.items(): for key, balance in self.balances.items():
@ -183,7 +189,6 @@ class Balances:
class Report(core.BaseODS[Sequence[None], None]): class Report(core.BaseODS[Sequence[None], None]):
C_CASH = 'Cash' C_CASH = 'Cash'
C_SATISFIED = 'Satisfaction of program restrictions' C_SATISFIED = 'Satisfaction of program restrictions'
EQUITY_ACCOUNTS = frozenset(['Equity', 'Income', 'Expenses'])
NO_BALANCE = core.Balance() NO_BALANCE = core.Balance()
SPACE = ' ' * 4 SPACE = ' ' * 4
@ -238,7 +243,7 @@ class Report(core.BaseODS[Sequence[None], None]):
def walk_classifications_by_account( def walk_classifications_by_account(
self, self,
account: str, account: str,
sort_period: int=Period.PERIOD, sort_period: Optional[int]=None,
) -> Iterator[Tuple[str, Optional[data.Account]]]: ) -> Iterator[Tuple[str, Optional[data.Account]]]:
return self.walk_classifications(self.balances.classifications( return self.walk_classifications(self.balances.classifications(
account, sort_period, account, sort_period,
@ -266,7 +271,7 @@ class Report(core.BaseODS[Sequence[None], None]):
period_assets = core.MutableBalance() period_assets = core.MutableBalance()
self.add_row(self.string_cell("Assets", stylename=self.style_bold)) self.add_row(self.string_cell("Assets", stylename=self.style_bold))
self.add_row() self.add_row()
for text, classification in self.walk_classifications_by_account('Assets', Period.ANY): for text, classification in self.walk_classifications_by_account('Assets'):
text_cell = self.string_cell(text) text_cell = self.string_cell(text)
if classification is None: if classification is None:
self.add_row(text_cell) self.add_row(text_cell)
@ -298,7 +303,7 @@ class Report(core.BaseODS[Sequence[None], None]):
self.add_row() self.add_row()
self.add_row(self.string_cell("Liabilities", stylename=self.style_bold)) self.add_row(self.string_cell("Liabilities", stylename=self.style_bold))
self.add_row() self.add_row()
for text, classification in self.walk_classifications_by_account('Liabilities', Period.ANY): for text, classification in self.walk_classifications_by_account('Liabilities'):
text_cell = self.string_cell(text) text_cell = self.string_cell(text)
if classification is None: if classification is None:
self.add_row(text_cell) self.add_row(text_cell)
@ -330,11 +335,11 @@ class Report(core.BaseODS[Sequence[None], None]):
preposition = "Without" if fund is Fund.UNRESTRICTED else "With" preposition = "Without" if fund is Fund.UNRESTRICTED else "With"
period_bal = -sum( period_bal = -sum(
(self.balances.total(account=account, fund=fund) (self.balances.total(account=account, fund=fund)
for account in self.EQUITY_ACCOUNTS), core.MutableBalance(), for account in EQUITY_ACCOUNTS), core.MutableBalance(),
) )
prior_bal = period_bal + sum( prior_bal = period_bal + sum(
(self.balances.total(account=account, fund=fund, period=Period.PERIOD) (self.balances.total(account=account, fund=fund, period=Period.PERIOD)
for account in self.EQUITY_ACCOUNTS), core.MutableBalance(), 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"),
@ -497,7 +502,7 @@ class Report(core.BaseODS[Sequence[None], None]):
kwargs['period'] = Period.OPENING kwargs['period'] = Period.OPENING
beginnings = [ beginnings = [
-sum((self.balances.total(account=account, **kwargs) -sum((self.balances.total(account=account, **kwargs)
for account in self.EQUITY_ACCOUNTS), core.MutableBalance()) for account in EQUITY_ACCOUNTS), core.MutableBalance())
for kwargs in bal_kwargs for kwargs in bal_kwargs
] ]
self.add_row() self.add_row()
@ -608,7 +613,7 @@ class Report(core.BaseODS[Sequence[None], None]):
totals = [ totals = [
-sum((self.balances.total(account=account, **kwargs) -sum((self.balances.total(account=account, **kwargs)
for account in self.EQUITY_ACCOUNTS), core.MutableBalance()) for account in EQUITY_ACCOUNTS), core.MutableBalance())
for kwargs in bal_kwargs for kwargs in bal_kwargs
] ]
self.add_row( self.add_row(