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,
 | 
					    Union,
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import odf.style  # type:ignore[import]
 | 
				
			||||||
import odf.table  # type:ignore[import]
 | 
					import odf.table  # type:ignore[import]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from beancount.parser import printer as bc_printer
 | 
					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)
 | 
					        self.period_name = date.strftime(date_fmt)
 | 
				
			||||||
        date = balances.prior_range.stop - one_day
 | 
					        date = balances.prior_range.stop - one_day
 | 
				
			||||||
        self.opening_name = date.strftime(date_fmt)
 | 
					        self.opening_name = date.strftime(date_fmt)
 | 
				
			||||||
 | 
					        self.last_totals_row = odf.table.TableRow()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def section_key(self, row: Sequence[None]) -> None:
 | 
					    def section_key(self, row: Sequence[None]) -> None:
 | 
				
			||||||
        raise NotImplementedError("balance_sheet.Report.section_key")
 | 
					        raise NotImplementedError("balance_sheet.Report.section_key")
 | 
				
			||||||
| 
						 | 
					@ -313,6 +315,33 @@ class Report(core.BaseODS[Sequence[None], None]):
 | 
				
			||||||
                    total_bal += balance
 | 
					                    total_bal += balance
 | 
				
			||||||
        return retval
 | 
					        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:
 | 
					    def write_financial_position(self) -> None:
 | 
				
			||||||
        self.start_sheet("Financial Position")
 | 
					        self.start_sheet("Financial Position")
 | 
				
			||||||
        balance_kwargs: Sequence[KWArgs] = [
 | 
					        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)
 | 
					        asset_totals = self.write_classifications_by_account('Assets', balance_kwargs)
 | 
				
			||||||
        self.add_row()
 | 
					        self.write_totals_row(
 | 
				
			||||||
        self.add_row(
 | 
					            "Total Assets", asset_totals, stylename=self.style_bottomline,
 | 
				
			||||||
            self.string_cell("Total Assets"),
 | 
					 | 
				
			||||||
            *(self.balance_cell(balance, stylename=self.style_bottomline)
 | 
					 | 
				
			||||||
              for balance in asset_totals),
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        self.add_row()
 | 
					        self.add_row()
 | 
				
			||||||
        self.add_row()
 | 
					        self.add_row()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        liabilities = self.write_classifications_by_account('Liabilities', balance_kwargs)
 | 
					        liabilities = self.write_classifications_by_account('Liabilities', balance_kwargs)
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row(
 | 
				
			||||||
            self.string_cell("Total Liabilities"),
 | 
					            "Total Liabilities", liabilities, stylename=self.style_totline,
 | 
				
			||||||
            *(self.balance_cell(balance, stylename=self.style_totline)
 | 
					 | 
				
			||||||
              for balance in liabilities),
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        self.add_row()
 | 
					        self.add_row()
 | 
				
			||||||
        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)
 | 
					                balance = -self.balances.total(account=EQUITY_ACCOUNTS, fund=fund, **kwargs)
 | 
				
			||||||
                row.addElement(self.balance_cell(balance))
 | 
					                row.addElement(self.balance_cell(balance))
 | 
				
			||||||
                total_bal += balance
 | 
					                total_bal += balance
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row(
 | 
				
			||||||
            self.string_cell("Total Net Assets"),
 | 
					            "Total Net Assets", equity_totals, stylename=self.style_subtotline,
 | 
				
			||||||
            *(self.balance_cell(balance, stylename=self.style_subtotline)
 | 
					 | 
				
			||||||
              for balance in equity_totals),
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        self.add_row()
 | 
					        self.write_totals_row(
 | 
				
			||||||
        self.add_row(
 | 
					            "Total Liabilities and Net Assets",
 | 
				
			||||||
            self.string_cell("Total Liabilities and Net Assets"),
 | 
					            liabilities, equity_totals,
 | 
				
			||||||
            *(self.balance_cell(ltot + etot, stylename=self.style_bottomline)
 | 
					            stylename=self.style_bottomline,
 | 
				
			||||||
              for ltot, etot in zip(liabilities, equity_totals)),
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def write_activities(self) -> None:
 | 
					    def write_activities(self) -> None:
 | 
				
			||||||
| 
						 | 
					@ -380,11 +401,7 @@ class Report(core.BaseODS[Sequence[None], None]):
 | 
				
			||||||
        income_totals = self.write_classifications_by_account(
 | 
					        income_totals = self.write_classifications_by_account(
 | 
				
			||||||
            'Income', bal_kwargs, (self.C_SATISFIED,),
 | 
					            'Income', bal_kwargs, (self.C_SATISFIED,),
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row("", income_totals, stylename=self.style_subtotline)
 | 
				
			||||||
            odf.table.TableCell(),
 | 
					 | 
				
			||||||
            *(self.balance_cell(total, stylename=self.style_subtotline)
 | 
					 | 
				
			||||||
              for total in income_totals),
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        self.add_row()
 | 
					        self.add_row()
 | 
				
			||||||
        self.add_row(
 | 
					        self.add_row(
 | 
				
			||||||
            self.string_cell("Net Assets released from restrictions:"),
 | 
					            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 = [core.MutableBalance() for _ in bal_kwargs]
 | 
				
			||||||
        other_totals[0] += released
 | 
					        other_totals[0] += released
 | 
				
			||||||
        other_totals[1] -= released
 | 
					        other_totals[1] -= released
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row(self.C_SATISFIED, other_totals)
 | 
				
			||||||
            self.string_cell(self.C_SATISFIED),
 | 
					        self.write_totals_row(
 | 
				
			||||||
            *(self.balance_cell(bal) for bal in other_totals),
 | 
					            "Total Support and Revenue",
 | 
				
			||||||
        )
 | 
					            income_totals, other_totals,
 | 
				
			||||||
        self.add_row()
 | 
					            stylename=self.style_totline,
 | 
				
			||||||
        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)),
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        period_expenses = core.MutableBalance()
 | 
					        period_expenses = core.MutableBalance()
 | 
				
			||||||
| 
						 | 
					@ -424,13 +437,12 @@ class Report(core.BaseODS[Sequence[None], None]):
 | 
				
			||||||
            prior_bal = self.balances.total(
 | 
					            prior_bal = self.balances.total(
 | 
				
			||||||
                account='Expenses', period=Period.PRIOR, post_type=type_value,
 | 
					                account='Expenses', period=Period.PRIOR, post_type=type_value,
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            self.add_row(
 | 
					            self.write_totals_row(text, [
 | 
				
			||||||
                self.string_cell(text),
 | 
					                period_bal,
 | 
				
			||||||
                self.balance_cell(period_bal),
 | 
					                self.NO_BALANCE,
 | 
				
			||||||
                self.balance_cell(self.NO_BALANCE),
 | 
					                period_bal,
 | 
				
			||||||
                self.balance_cell(period_bal),
 | 
					                prior_bal,
 | 
				
			||||||
                self.balance_cell(prior_bal),
 | 
					            ], leading_rows=0)
 | 
				
			||||||
            )
 | 
					 | 
				
			||||||
            period_expenses += period_bal
 | 
					            period_expenses += period_bal
 | 
				
			||||||
            prior_expenses += prior_bal
 | 
					            prior_expenses += prior_bal
 | 
				
			||||||
        period_bal = self.balances.total(account='Expenses', period=Period.PERIOD)
 | 
					        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
 | 
					            prior_bal = prior_expenses
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            logger.warning("Prior functional expenses do not match total; math in column E is wrong")
 | 
					            logger.warning("Prior functional expenses do not match total; math in column E is wrong")
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row("Total Expenses", [
 | 
				
			||||||
            self.string_cell("Total Expenses"),
 | 
					            period_bal,
 | 
				
			||||||
            self.balance_cell(period_bal, stylename=self.style_totline),
 | 
					            self.NO_BALANCE,
 | 
				
			||||||
            self.balance_cell(self.NO_BALANCE, stylename=self.style_totline),
 | 
					            period_bal,
 | 
				
			||||||
            self.balance_cell(period_bal, stylename=self.style_totline),
 | 
					            prior_bal,
 | 
				
			||||||
            self.balance_cell(prior_bal, stylename=self.style_totline),
 | 
					        ], stylename=self.style_totline, leading_rows=0)
 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        other_totals[0] -= period_bal
 | 
					        other_totals[0] -= period_bal
 | 
				
			||||||
        other_totals[2] -= period_bal
 | 
					        other_totals[2] -= period_bal
 | 
				
			||||||
        other_totals[3] -= prior_bal
 | 
					        other_totals[3] -= prior_bal
 | 
				
			||||||
        self.add_row()
 | 
					        self.write_totals_row("Change in Net Assets", income_totals, other_totals)
 | 
				
			||||||
        self.add_row(
 | 
					 | 
				
			||||||
            self.string_cell("Change in Net Assets"),
 | 
					 | 
				
			||||||
            *(self.balance_cell(inctot + otot)
 | 
					 | 
				
			||||||
              for inctot, otot in zip(income_totals, other_totals)),
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for kwargs in bal_kwargs:
 | 
					        for kwargs in bal_kwargs:
 | 
				
			||||||
            if kwargs['period'] is Period.PERIOD:
 | 
					            if kwargs['period'] is Period.PERIOD:
 | 
				
			||||||
| 
						 | 
					@ -470,17 +476,11 @@ class Report(core.BaseODS[Sequence[None], None]):
 | 
				
			||||||
            -self.balances.total(account=EQUITY_ACCOUNTS, **kwargs)
 | 
					            -self.balances.total(account=EQUITY_ACCOUNTS, **kwargs)
 | 
				
			||||||
            for kwargs in bal_kwargs
 | 
					            for kwargs in bal_kwargs
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        self.add_row()
 | 
					        self.write_totals_row("Beginning Net Assets", equity_totals)
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row(
 | 
				
			||||||
            self.string_cell("Beginning Net Assets"),
 | 
					            "Ending Net Assets",
 | 
				
			||||||
            *(self.balance_cell(beg_bal) for beg_bal in equity_totals),
 | 
					            income_totals, other_totals, equity_totals,
 | 
				
			||||||
        )
 | 
					            stylename=self.style_bottomline,
 | 
				
			||||||
 | 
					 | 
				
			||||||
        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)),
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def write_functional_expenses(self) -> None:
 | 
					    def write_functional_expenses(self) -> None:
 | 
				
			||||||
| 
						 | 
					@ -498,11 +498,10 @@ class Report(core.BaseODS[Sequence[None], None]):
 | 
				
			||||||
            {'period': Period.PERIOD},
 | 
					            {'period': Period.PERIOD},
 | 
				
			||||||
            {'period': Period.PRIOR},
 | 
					            {'period': Period.PRIOR},
 | 
				
			||||||
        ])
 | 
					        ])
 | 
				
			||||||
        self.add_row()
 | 
					        self.write_totals_row(
 | 
				
			||||||
        self.add_row(
 | 
					            "Total Expenses",
 | 
				
			||||||
            self.string_cell("Total Expenses"),
 | 
					            totals,
 | 
				
			||||||
            *(self.balance_cell(tot_bal, stylename=self.style_bottomline)
 | 
					            stylename=self.style_bottomline,
 | 
				
			||||||
              for tot_bal in totals),
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def write_cash_flows(self) -> None:
 | 
					    def write_cash_flows(self) -> None:
 | 
				
			||||||
| 
						 | 
					@ -517,16 +516,11 @@ class Report(core.BaseODS[Sequence[None], None]):
 | 
				
			||||||
            "Cash Flows from Operating Activities",
 | 
					            "Cash Flows from Operating Activities",
 | 
				
			||||||
            stylename=self.style_bold,
 | 
					            stylename=self.style_bold,
 | 
				
			||||||
        ))
 | 
					        ))
 | 
				
			||||||
        self.add_row()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        equity_totals = [
 | 
					        equity_totals = [
 | 
				
			||||||
            -self.balances.total(account=EQUITY_ACCOUNTS, **kwargs)
 | 
					            -self.balances.total(account=EQUITY_ACCOUNTS, **kwargs)
 | 
				
			||||||
            for kwargs in bal_kwargs
 | 
					            for kwargs in bal_kwargs
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row("Change in Net Assets", equity_totals, leading_rows=1)
 | 
				
			||||||
            self.string_cell("Change in Net Assets"),
 | 
					 | 
				
			||||||
            *(self.balance_cell(bal) for bal in equity_totals),
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        self.add_row(self.string_cell(
 | 
					        self.add_row(self.string_cell(
 | 
				
			||||||
            "(Increase) decrease in operating assets:",
 | 
					            "(Increase) decrease in operating assets:",
 | 
				
			||||||
        ))
 | 
					        ))
 | 
				
			||||||
| 
						 | 
					@ -539,35 +533,25 @@ class Report(core.BaseODS[Sequence[None], None]):
 | 
				
			||||||
        liabilities = self.write_classifications_by_account(
 | 
					        liabilities = self.write_classifications_by_account(
 | 
				
			||||||
            'Liabilities', bal_kwargs, (), self.SPACE, norm_func,
 | 
					            'Liabilities', bal_kwargs, (), self.SPACE, norm_func,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        totals = [
 | 
					        period_totals = [
 | 
				
			||||||
            sum(bals, core.MutableBalance())
 | 
					            sum(bals, core.MutableBalance())
 | 
				
			||||||
            for bals in zip(equity_totals, asset_totals, liabilities)
 | 
					            for bals in zip(equity_totals, asset_totals, liabilities)
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row(
 | 
				
			||||||
            self.string_cell("Net cash provided by operating activites"),
 | 
					            "Net cash provided by operating activites",
 | 
				
			||||||
            *(self.balance_cell(tot_bal, stylename=self.style_totline)
 | 
					            period_totals,
 | 
				
			||||||
              for tot_bal in totals),
 | 
					            stylename=self.style_totline,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        self.add_row()
 | 
					        self.write_totals_row("Net Increase in Cash", period_totals)
 | 
				
			||||||
 | 
					        begin_totals = [
 | 
				
			||||||
        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.balances.total(classification=self.C_CASH, period=period)
 | 
					            self.balances.total(classification=self.C_CASH, period=period)
 | 
				
			||||||
            for period in [Period.BEFORE_PERIOD, Period.OPENING]
 | 
					            for period in [Period.BEFORE_PERIOD, Period.OPENING]
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        self.add_row(
 | 
					        self.write_totals_row("Beginning Cash", begin_totals)
 | 
				
			||||||
            self.string_cell("Beginning Cash"),
 | 
					        self.write_totals_row(
 | 
				
			||||||
            *(self.balance_cell(bal) for bal in balances),
 | 
					            "Ending Cash",
 | 
				
			||||||
        )
 | 
					            period_totals, begin_totals,
 | 
				
			||||||
        self.add_row()
 | 
					            stylename=self.style_bottomline,
 | 
				
			||||||
        self.add_row(
 | 
					 | 
				
			||||||
            self.string_cell("Ending Cash"),
 | 
					 | 
				
			||||||
            *(self.balance_cell(tot + bal, stylename=self.style_bottomline)
 | 
					 | 
				
			||||||
              for tot, bal in zip(totals, balances)),
 | 
					 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue