Changeset - fe52fe50a188
[Not reviewed]
0 1 0
Brett Smith - 4 years ago 2020-10-06 18:29:19
brettcsmith@brettcsmith.org
balance_sheet: Fix unwanted hierarchy traversal in Trial Balances.

The balances reported on the trial balances sheet included an account's
subaccounts. e.g., the balance for Expenses:A would include the balance
for Expenses:A:B. We don't want that traversal for this report, so
inhibit it and report only exact account balances.
1 file changed with 21 insertions and 4 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/balance_sheet.py
Show inline comments
...
 
@@ -145,18 +145,31 @@ class Balances:
 
    def total(self,
 
              account: Union[None, str, Collection[str]]=None,
 
              classification: Optional[str]=None,
 
              period: int=Period.ANY,
 
              fund: int=Fund.ANY,
 
              post_type: Optional[str]=None,
 
              *,
 
              account_exact: bool=False,
 
    ) -> core.Balance:
 
        if isinstance(account, str):
 
            account = (account,)
 
        acct_pred: Callable[[data.Account], bool]
 
        if account is None:
 
            acct_pred = lambda acct: True
 
        elif account_exact:
 
            # At this point, between this isinstance() above and the earlier
 
            # `account is None` check, we've collapsed the type of `account` to
 
            # `Collection[str]`. Unfortunately the logic is too involved for
 
            # mypy to follow, so ignore the type problem.
 
            acct_pred = lambda acct: acct in account  # type:ignore[operator]
 
        else:
 
            acct_pred = lambda acct: acct.is_under(*account) is not None  # type:ignore[misc]
 
        retval = core.MutableBalance()
 
        for key, balance in self.balances.items():
 
            if not (account is None or key.account.is_under(*account)):
 
            if not acct_pred(key.account):
 
                pass
 
            elif not (classification is None
 
                      or key.classification.is_under(classification)):
 
                pass
 
            elif not period & key.period:
 
                pass
...
 
@@ -596,16 +609,20 @@ class Report(core.BaseODS[Sequence[None], None]):
 

	
 
        for acct_root in ['Assets', 'Liabilities', 'Income', 'Expenses', 'Equity']:
 
            norm_func = core.normalize_amount_func(f'{acct_root}:Dummy')
 
            want_balance = acct_root not in EQUITY_ACCOUNTS
 
            self.add_row()
 
            for account in self.balances.iter_accounts(acct_root):
 
                period_bal = self.balances.total(account=account, period=Period.PERIOD)
 
                prior_bal = self.balances.total(account=account, period=Period.PRIOR)
 
                period_bal = self.balances.total(
 
                    account=account, period=Period.PERIOD, account_exact=True,
 
                )
 
                prior_bal = self.balances.total(
 
                    account=account, period=Period.PRIOR, account_exact=True,
 
                )
 
                if want_balance:
 
                    close_bal = self.balances.total(account=account)
 
                    close_bal = self.balances.total(account=account, account_exact=True)
 
                    close_cell = self.balance_cell(norm_func(close_bal))
 
                    open_cell = self.balance_cell(norm_func(close_bal - period_bal))
 
                else:
 
                    close_cell = odf.table.TableCell()
 
                    open_cell = odf.table.TableCell()
 
                self.add_row(
0 comments (0 inline, 0 general)