balance_sheet: Refactor out Report.write_totals_row method.
This commit is contained in:
parent
b7dee6a88a
commit
6159870681
1 changed files with 80 additions and 96 deletions
|
@ -44,6 +44,7 @@ from typing import (
|
|||
Union,
|
||||
)
|
||||
|
||||
import odf.style # type:ignore[import]
|
||||
import odf.table # type:ignore[import]
|
||||
|
||||
from beancount.parser import printer as bc_printer
|
||||
|
@ -208,6 +209,7 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
self.period_name = date.strftime(date_fmt)
|
||||
date = balances.prior_range.stop - one_day
|
||||
self.opening_name = date.strftime(date_fmt)
|
||||
self.last_totals_row = odf.table.TableRow()
|
||||
|
||||
def section_key(self, row: Sequence[None]) -> None:
|
||||
raise NotImplementedError("balance_sheet.Report.section_key")
|
||||
|
@ -313,6 +315,33 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
total_bal += balance
|
||||
return retval
|
||||
|
||||
def write_totals_row(
|
||||
self,
|
||||
text: str,
|
||||
*balances: Sequence[core.Balance],
|
||||
stylename: Union[None, str, odf.style.Style]=None,
|
||||
leading_rows: Optional[int]=None,
|
||||
) -> odf.table.TableRow:
|
||||
if leading_rows is None:
|
||||
if (self.sheet.lastChild is self.last_totals_row
|
||||
or stylename is self.style_bottomline):
|
||||
leading_rows = 1
|
||||
else:
|
||||
leading_rows = 0
|
||||
expect_len = self.col_count - 1
|
||||
assert all(len(seq) == expect_len for seq in balances), \
|
||||
"called write_totals_row with the wrong length of balance columns"
|
||||
for _ in range(leading_rows):
|
||||
self.add_row()
|
||||
self.last_totals_row = self.add_row(
|
||||
self.string_cell(text),
|
||||
*(self.balance_cell(
|
||||
sum(sum_bals, core.MutableBalance()),
|
||||
stylename=stylename,
|
||||
) for sum_bals in zip(*balances)),
|
||||
)
|
||||
return self.last_totals_row
|
||||
|
||||
def write_financial_position(self) -> None:
|
||||
self.start_sheet("Financial Position")
|
||||
balance_kwargs: Sequence[KWArgs] = [
|
||||
|
@ -321,20 +350,15 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
]
|
||||
|
||||
asset_totals = self.write_classifications_by_account('Assets', balance_kwargs)
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Total Assets"),
|
||||
*(self.balance_cell(balance, stylename=self.style_bottomline)
|
||||
for balance in asset_totals),
|
||||
self.write_totals_row(
|
||||
"Total Assets", asset_totals, stylename=self.style_bottomline,
|
||||
)
|
||||
self.add_row()
|
||||
self.add_row()
|
||||
|
||||
liabilities = self.write_classifications_by_account('Liabilities', balance_kwargs)
|
||||
self.add_row(
|
||||
self.string_cell("Total Liabilities"),
|
||||
*(self.balance_cell(balance, stylename=self.style_totline)
|
||||
for balance in liabilities),
|
||||
self.write_totals_row(
|
||||
"Total Liabilities", liabilities, stylename=self.style_totline,
|
||||
)
|
||||
self.add_row()
|
||||
self.add_row()
|
||||
|
@ -349,16 +373,13 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
balance = -self.balances.total(account=EQUITY_ACCOUNTS, fund=fund, **kwargs)
|
||||
row.addElement(self.balance_cell(balance))
|
||||
total_bal += balance
|
||||
self.add_row(
|
||||
self.string_cell("Total Net Assets"),
|
||||
*(self.balance_cell(balance, stylename=self.style_subtotline)
|
||||
for balance in equity_totals),
|
||||
self.write_totals_row(
|
||||
"Total Net Assets", equity_totals, stylename=self.style_subtotline,
|
||||
)
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Total Liabilities and Net Assets"),
|
||||
*(self.balance_cell(ltot + etot, stylename=self.style_bottomline)
|
||||
for ltot, etot in zip(liabilities, equity_totals)),
|
||||
self.write_totals_row(
|
||||
"Total Liabilities and Net Assets",
|
||||
liabilities, equity_totals,
|
||||
stylename=self.style_bottomline,
|
||||
)
|
||||
|
||||
def write_activities(self) -> None:
|
||||
|
@ -380,11 +401,7 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
income_totals = self.write_classifications_by_account(
|
||||
'Income', bal_kwargs, (self.C_SATISFIED,),
|
||||
)
|
||||
self.add_row(
|
||||
odf.table.TableCell(),
|
||||
*(self.balance_cell(total, stylename=self.style_subtotline)
|
||||
for total in income_totals),
|
||||
)
|
||||
self.write_totals_row("", income_totals, stylename=self.style_subtotline)
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Net Assets released from restrictions:"),
|
||||
|
@ -397,15 +414,11 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
other_totals = [core.MutableBalance() for _ in bal_kwargs]
|
||||
other_totals[0] += released
|
||||
other_totals[1] -= released
|
||||
self.add_row(
|
||||
self.string_cell(self.C_SATISFIED),
|
||||
*(self.balance_cell(bal) for bal in other_totals),
|
||||
)
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Total Support and Revenue"),
|
||||
*(self.balance_cell(inctot + otot, stylename=self.style_totline)
|
||||
for inctot, otot in zip(income_totals, other_totals)),
|
||||
self.write_totals_row(self.C_SATISFIED, other_totals)
|
||||
self.write_totals_row(
|
||||
"Total Support and Revenue",
|
||||
income_totals, other_totals,
|
||||
stylename=self.style_totline,
|
||||
)
|
||||
|
||||
period_expenses = core.MutableBalance()
|
||||
|
@ -424,13 +437,12 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
prior_bal = self.balances.total(
|
||||
account='Expenses', period=Period.PRIOR, post_type=type_value,
|
||||
)
|
||||
self.add_row(
|
||||
self.string_cell(text),
|
||||
self.balance_cell(period_bal),
|
||||
self.balance_cell(self.NO_BALANCE),
|
||||
self.balance_cell(period_bal),
|
||||
self.balance_cell(prior_bal),
|
||||
)
|
||||
self.write_totals_row(text, [
|
||||
period_bal,
|
||||
self.NO_BALANCE,
|
||||
period_bal,
|
||||
prior_bal,
|
||||
], leading_rows=0)
|
||||
period_expenses += period_bal
|
||||
prior_expenses += prior_bal
|
||||
period_bal = self.balances.total(account='Expenses', period=Period.PERIOD)
|
||||
|
@ -443,23 +455,17 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
prior_bal = prior_expenses
|
||||
else:
|
||||
logger.warning("Prior functional expenses do not match total; math in column E is wrong")
|
||||
self.add_row(
|
||||
self.string_cell("Total Expenses"),
|
||||
self.balance_cell(period_bal, stylename=self.style_totline),
|
||||
self.balance_cell(self.NO_BALANCE, stylename=self.style_totline),
|
||||
self.balance_cell(period_bal, stylename=self.style_totline),
|
||||
self.balance_cell(prior_bal, stylename=self.style_totline),
|
||||
)
|
||||
self.write_totals_row("Total Expenses", [
|
||||
period_bal,
|
||||
self.NO_BALANCE,
|
||||
period_bal,
|
||||
prior_bal,
|
||||
], stylename=self.style_totline, leading_rows=0)
|
||||
|
||||
other_totals[0] -= period_bal
|
||||
other_totals[2] -= period_bal
|
||||
other_totals[3] -= prior_bal
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Change in Net Assets"),
|
||||
*(self.balance_cell(inctot + otot)
|
||||
for inctot, otot in zip(income_totals, other_totals)),
|
||||
)
|
||||
self.write_totals_row("Change in Net Assets", income_totals, other_totals)
|
||||
|
||||
for kwargs in bal_kwargs:
|
||||
if kwargs['period'] is Period.PERIOD:
|
||||
|
@ -470,17 +476,11 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
-self.balances.total(account=EQUITY_ACCOUNTS, **kwargs)
|
||||
for kwargs in bal_kwargs
|
||||
]
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Beginning Net Assets"),
|
||||
*(self.balance_cell(beg_bal) for beg_bal in equity_totals),
|
||||
)
|
||||
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Ending Net Assets"),
|
||||
*(self.balance_cell(inctot + otot + eqtot, stylename=self.style_bottomline)
|
||||
for inctot, otot, eqtot in zip(income_totals, other_totals, equity_totals)),
|
||||
self.write_totals_row("Beginning Net Assets", equity_totals)
|
||||
self.write_totals_row(
|
||||
"Ending Net Assets",
|
||||
income_totals, other_totals, equity_totals,
|
||||
stylename=self.style_bottomline,
|
||||
)
|
||||
|
||||
def write_functional_expenses(self) -> None:
|
||||
|
@ -498,11 +498,10 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
{'period': Period.PERIOD},
|
||||
{'period': Period.PRIOR},
|
||||
])
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Total Expenses"),
|
||||
*(self.balance_cell(tot_bal, stylename=self.style_bottomline)
|
||||
for tot_bal in totals),
|
||||
self.write_totals_row(
|
||||
"Total Expenses",
|
||||
totals,
|
||||
stylename=self.style_bottomline,
|
||||
)
|
||||
|
||||
def write_cash_flows(self) -> None:
|
||||
|
@ -517,16 +516,11 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
"Cash Flows from Operating Activities",
|
||||
stylename=self.style_bold,
|
||||
))
|
||||
self.add_row()
|
||||
|
||||
equity_totals = [
|
||||
-self.balances.total(account=EQUITY_ACCOUNTS, **kwargs)
|
||||
for kwargs in bal_kwargs
|
||||
]
|
||||
self.add_row(
|
||||
self.string_cell("Change in Net Assets"),
|
||||
*(self.balance_cell(bal) for bal in equity_totals),
|
||||
)
|
||||
self.write_totals_row("Change in Net Assets", equity_totals, leading_rows=1)
|
||||
self.add_row(self.string_cell(
|
||||
"(Increase) decrease in operating assets:",
|
||||
))
|
||||
|
@ -539,35 +533,25 @@ class Report(core.BaseODS[Sequence[None], None]):
|
|||
liabilities = self.write_classifications_by_account(
|
||||
'Liabilities', bal_kwargs, (), self.SPACE, norm_func,
|
||||
)
|
||||
totals = [
|
||||
period_totals = [
|
||||
sum(bals, core.MutableBalance())
|
||||
for bals in zip(equity_totals, asset_totals, liabilities)
|
||||
]
|
||||
self.add_row(
|
||||
self.string_cell("Net cash provided by operating activites"),
|
||||
*(self.balance_cell(tot_bal, stylename=self.style_totline)
|
||||
for tot_bal in totals),
|
||||
self.write_totals_row(
|
||||
"Net cash provided by operating activites",
|
||||
period_totals,
|
||||
stylename=self.style_totline,
|
||||
)
|
||||
self.add_row()
|
||||
|
||||
self.add_row(
|
||||
self.string_cell("Net Increase in Cash"),
|
||||
*(self.balance_cell(tot_bal) for tot_bal in totals),
|
||||
)
|
||||
self.add_row()
|
||||
balances = [
|
||||
self.write_totals_row("Net Increase in Cash", period_totals)
|
||||
begin_totals = [
|
||||
self.balances.total(classification=self.C_CASH, period=period)
|
||||
for period in [Period.BEFORE_PERIOD, Period.OPENING]
|
||||
]
|
||||
self.add_row(
|
||||
self.string_cell("Beginning Cash"),
|
||||
*(self.balance_cell(bal) for bal in balances),
|
||||
)
|
||||
self.add_row()
|
||||
self.add_row(
|
||||
self.string_cell("Ending Cash"),
|
||||
*(self.balance_cell(tot + bal, stylename=self.style_bottomline)
|
||||
for tot, bal in zip(totals, balances)),
|
||||
self.write_totals_row("Beginning Cash", begin_totals)
|
||||
self.write_totals_row(
|
||||
"Ending Cash",
|
||||
period_totals, begin_totals,
|
||||
stylename=self.style_bottomline,
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue