Files
@ 404a88de1db0
Branch filter:
Location: NPO-Accounting/conservancy_beancount/conservancy_beancount/reports/fund.py
404a88de1db0
16.5 KiB
text/x-python
fund: Use Balances instead of PeriodPostings.
A few motivations for this:
* This makes the fund report more maintainable, because the data structure
is better suited to the task at hand, making it easier to follow what's
going on.
* This helps put Balances through a little more testing with a high-level
report.
* This makes the fund report a little faster, since totals are usually
calculated from a smaller number of Balance objects rather than all
Postings over a period.
A few motivations for this:
* This makes the fund report more maintainable, because the data structure
is better suited to the task at hand, making it easier to follow what's
going on.
* This helps put Balances through a little more testing with a high-level
report.
* This makes the fund report a little faster, since totals are usually
calculated from a smaller number of Balance objects rather than all
Postings over a period.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | """funds.py - Funds report from Beancount
This tool produces a report of account balances for restricted funds,
including income, expenses, and realized gain/loss over a given time period.
Restricted funds are designated with the "project" metadata.
Specify the date range you want to report with the ``--begin`` and ``--end``
options.
Select the accounts you want to report with the ``--account`` option. You can
specify this option multiple times. The report will include at least one sheet
for each account you specify. Subaccounts will be reported on that sheet as
well.
Select the postings you want to report by passing metadata search terms in
``name=value`` format.
Run ``ledger-report --help`` for abbreviations and other options.
Examples
--------
Generate a report of all restricted funds in ODS format over the last year::
fund-report
Query a specific restricted fund and get a quick balance on the terminal::
fund-report <PROJECT>
"""
# Copyright © 2020 Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import collections
import datetime
import enum
import functools
import locale
import logging
import sys
from typing import (
Dict,
Iterable,
Iterator,
Mapping,
Optional,
Sequence,
TextIO,
Tuple,
Union,
)
from ..beancount_types import (
MetaValue,
)
from decimal import Decimal
from pathlib import Path
import odf.table # type:ignore[import]
from beancount.parser import printer as bc_printer
from . import core
from . import rewrite
from .. import books
from .. import cliutil
from .. import config as configmod
from .. import data
Period = core.Period
EQUITY_ACCOUNTS = ['Income', 'Expenses', 'Equity']
INFO_ACCOUNTS = [
'Assets:Receivable',
'Assets:Prepaid',
'Liabilities:UnearnedIncome',
'Liabilities:Payable',
]
PROGNAME = 'fund-report'
UNRESTRICTED_FUND = 'Conservancy'
logger = logging.getLogger('conservancy_beancount.reports.fund')
class ODSReport(core.BaseODS[str, None]):
def __init__(self,
balances: core.Balances,
start_date: datetime.date,
stop_date: datetime.date,
) -> None:
super().__init__()
self.balances = balances
self.start_date = start_date
self.stop_date = stop_date
def section_key(self, row: str) -> None:
return None
def start_spreadsheet(self) -> None:
headers = [["Fund"], ["Balance as of", self.start_date.isoformat()]]
# If self.sheet has children, that means we've already written the
# first sheet and we're starting the second, expanded sheet.
if self.sheet.childNodes:
self.expanded = True
sheet_name = "With Breakdowns"
headers.extend([acct] for acct in EQUITY_ACCOUNTS)
else:
self.expanded = False
sheet_name = "Fund Report"
headers += [["Additions"], ["Releases from", "Restrictions"]]
headers.append(["Balance as of", self.stop_date.isoformat()])
if self.expanded:
headers += [
["Of which", "Receivable"],
["Of which", "Prepaid Expenses"],
["Of which", "Payable"],
["Of which", "Unearned Income"],
]
self.use_sheet(sheet_name)
for header in headers:
first_line = header[0]
if first_line == 'Fund':
width = 2.0
elif first_line == 'Balance as of':
width = 1.5
elif first_line == 'Of which':
width = 1.3
else:
width = 1.2
col_style = self.column_style(width)
self.sheet.addElement(odf.table.TableColumn(stylename=col_style))
center_bold = self.merge_styles(self.style_centertext, self.style_bold)
row = self.add_row(*(
self.multiline_cell(header, stylename=center_bold)
for header in headers
))
row.firstChild.setAttribute(
'stylename', self.merge_styles(self.style_endtext, self.style_bold),
)
self.lock_first_row()
self.lock_first_column()
self.add_row()
self.add_row(self.string_cell(
f"Fund Report From {self.start_date.isoformat()} To {self.stop_date.isoformat()}",
stylename=center_bold,
numbercolumnsspanned=6,
))
self.add_row()
self.sheet_totals = [core.MutableBalance() for _ in range(len(headers) - 1)]
def write_balance_row(self,
name: str,
balances: Iterable[core.Balance],
style: Union[None, str, odf.style.Style]=None,
) -> odf.table.TableRow:
return self.add_row(
self.string_cell(name, stylename=self.style_endtext),
*(self.balance_cell(bal, stylename=style) for bal in balances),
)
def write_balances(self,
name: str,
style: Union[None, str, odf.style.Style]=None,
fund: int=core.Fund.ANY,
) -> odf.table.TableRow:
if (fund & core.Fund.ANY) == core.Fund.ANY:
total = functools.partial(self.balances.total, post_meta=name)
else:
total = functools.partial(self.balances.total, fund=fund)
balances = [
-total(account=EQUITY_ACCOUNTS, period=Period.THRU_MIDDLE),
-total(account='Income', period=Period.PERIOD),
total(account='Expenses', period=Period.PERIOD),
-total(account='Equity', period=Period.PERIOD),
]
if not self.expanded:
threshold = Decimal('.5')
if all(bal.clean_copy(threshold).le_zero() for bal in balances):
return
equity = balances.pop()
balances[-1] -= equity
balances.append(-total(account=EQUITY_ACCOUNTS))
if self.expanded:
for account in INFO_ACCOUNTS:
norm_func = core.normalize_amount_func(account)
balances.append(norm_func(total(account=account)))
for sheet_tot, row_tot in zip(self.sheet_totals, balances):
sheet_tot += row_tot
return self.write_balance_row(name, balances, style)
def write_row(self, row: str) -> None:
if row != UNRESTRICTED_FUND:
self.write_balances(row)
def write(self, rows: Iterable[str]) -> None:
row_list = list(rows)
# Write the basic, auditor-style fund report.
super().write(iter(row_list))
self.write_balance_row("", self.sheet_totals, self.style_bottomline)
# Write the expanded fund report. start_spreadsheet() will see we've
# written the first sheet and adapt.
super().write(iter(row_list))
self.write_balances("Unrestricted", fund=core.Fund.UNRESTRICTED)
self.set_open_sheet(self.sheet)
class TextReport:
def __init__(self,
balances: core.Balances,
start_date: datetime.date,
stop_date: datetime.date,
out_file: TextIO) -> None:
self.balances = balances
self.start_date = start_date
self.stop_date = stop_date
self.out_file = out_file
accounts = self.balances.iter_accounts()
self.equity_accounts = [
account for _, account in
core.sort_and_filter_accounts(accounts, EQUITY_ACCOUNTS)
]
self.info_accounts = [
account for _, account in
core.sort_and_filter_accounts(accounts, INFO_ACCOUNTS)
]
def _format_total(self,
fund_name: str,
period: int=Period.ANY,
account: Optional[str]=None,
) -> Sequence[str]:
if account is None:
account_arg: Union[str, Sequence[str]] = EQUITY_ACCOUNTS
account_exact = False
else:
account_arg = account
account_exact = True
balance = self.balances.total(
post_meta=fund_name,
period=period,
account=account_arg,
account_exact=account_exact,
)
if balance.is_zero():
return []
elif account is None:
balance = -balance
else:
norm_func = core.normalize_amount_func(f'{account}:RootsOK')
balance = norm_func(balance)
return balance.format(None, sep='\0').split('\0')
def _account_balances(self, fund: str) -> Iterator[Tuple[str, Sequence[str]]]:
total_fmt = f'{fund} balance as of {{}}'
yield (total_fmt.format(self.start_date.isoformat()),
self._format_total(fund, Period.THRU_MIDDLE))
for account in self.equity_accounts:
total = self._format_total(fund, Period.PERIOD, account)
if total:
yield (account, total)
yield (total_fmt.format(self.stop_date.isoformat()),
self._format_total(fund))
for account in self.info_accounts:
total = self._format_total(fund, account=account)
if total:
yield (account, total)
def write(self, rows: Iterable[str]) -> None:
output = [
line
for fund in rows
for line in self._account_balances(fund)
]
acct_width = max(len(acct_s) for acct_s, _ in output) + 2
bal_width = max(len(s) for _, bal_s in output for s in bal_s)
bal_width = max(bal_width, 8)
line_fmt = f'{{:>{acct_width}}} {{:>{bal_width}}}'
print(line_fmt.replace('{:>', '{:^').format("ACCOUNT", "BALANCE"),
file=self.out_file)
fund_start = f' balance as of {self.start_date.isoformat()}'
fund_end = f' balance as of {self.stop_date.isoformat()}'
for acct_s, bal_seq in output:
is_end_bal = acct_s.endswith(fund_end)
if is_end_bal or acct_s.endswith(fund_start):
print(line_fmt.format('─' * acct_width, '─' * bal_width),
file=self.out_file)
bal_iter = iter(bal_seq)
print(line_fmt.format(acct_s, next(bal_iter)), file=self.out_file)
for bal_s in bal_iter:
print(line_fmt.format('', bal_s), file=self.out_file)
if is_end_bal:
print(line_fmt.format('═' * acct_width, '═' * bal_width),
file=self.out_file)
class ReportType(enum.Enum):
TEXT = TextReport
ODS = ODSReport
TXT = TEXT
SPREADSHEET = ODS
@classmethod
def from_arg(cls, s: str) -> 'ReportType':
try:
return cls[s.upper()]
except KeyError:
raise ValueError(f"no report type matches {s!r}") from None
def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace:
parser = argparse.ArgumentParser(prog=PROGNAME)
cliutil.add_version_argument(parser)
parser.add_argument(
'--begin', '--start', '-b',
dest='start_date',
metavar='DATE',
type=cliutil.date_arg,
help="""Date to start reporting entries, inclusive, in YYYY-MM-DD format.
The default is one year ago.
""")
parser.add_argument(
'--end', '--stop', '-e',
dest='stop_date',
metavar='DATE',
type=cliutil.date_arg,
help="""Date to stop reporting entries, exclusive, in YYYY-MM-DD format.
The default is a year after the start date.
""")
cliutil.add_rewrite_rules_argument(parser)
parser.add_argument(
'--report-type', '-t',
metavar='TYPE',
type=ReportType.from_arg,
help="""Type of report to generate. `text` gives a plain two-column text
report listing accounts and balances over the period, and is the default when
you search for a specific project/fund. `ods` produces a higher-level
spreadsheet, meant to provide an overview of all funds, and is the default when
you don't specify a project/fund.
""")
parser.add_argument(
'--output-file', '-O',
metavar='PATH',
type=Path,
help="""Write the report to this file, or stdout when PATH is `-`.
The default is stdout for text reports, and a generated filename for ODS
reports.
""")
cliutil.add_loglevel_argument(parser)
parser.add_argument(
'search_terms',
metavar='FILTER',
type=cliutil.SearchTerm.arg_parser('project', 'rt-id'),
nargs=argparse.ZERO_OR_MORE,
help="""Report on postings that match this criteria. The format is
NAME=TERM. TERM is a link or word that must exist in a posting's NAME
metadata to match. A single ticket number is a shortcut for
`rt-id=rt:NUMBER`. Any other word is a shortcut for `project=TERM`.
""")
args = parser.parse_args(arglist)
if args.report_type is None:
if any(term.meta_key == 'project' for term in args.search_terms):
args.report_type = ReportType.TEXT
else:
args.report_type = ReportType.ODS
return args
def main(arglist: Optional[Sequence[str]]=None,
stdout: TextIO=sys.stdout,
stderr: TextIO=sys.stderr,
config: Optional[configmod.Config]=None,
) -> int:
args = parse_arguments(arglist)
cliutil.set_loglevel(logger, args.loglevel)
if config is None:
config = configmod.Config()
config.load_file()
if args.stop_date is None:
if args.start_date is None:
args.stop_date = datetime.date.today()
else:
args.stop_date = cliutil.diff_year(args.start_date, 1)
if args.start_date is None:
args.start_date = cliutil.diff_year(args.stop_date, -1)
returncode = 0
books_loader = config.books_loader()
if books_loader is None:
entries, load_errors, options = books.Loader.load_none(config.config_file_path())
returncode = cliutil.ExitCode.NoConfiguration
else:
entries, load_errors, options = books_loader.load_fy_range(args.start_date, args.stop_date)
if load_errors:
returncode = cliutil.ExitCode.BeancountErrors
elif not entries:
returncode = cliutil.ExitCode.NoDataLoaded
for error in load_errors:
bc_printer.print_error(error, file=stderr)
data.Account.load_from_books(entries, options)
postings = iter(
post
for post in data.Posting.from_entries(entries)
if post.meta.date < args.stop_date
)
for rewrite_path in args.rewrite_rules:
try:
ruleset = rewrite.RewriteRuleset.from_yaml(rewrite_path)
except ValueError as error:
logger.critical("failed loading rewrite rules from %s: %s",
rewrite_path, error.args[0])
return cliutil.ExitCode.RewriteRulesError
postings = ruleset.rewrite(postings)
for search_term in args.search_terms:
postings = search_term.filter_postings(postings)
balances = core.Balances(postings, args.start_date, args.stop_date, 'project')
funds = sorted(balances.meta_values(), key=lambda s: locale.strxfrm(s.casefold()))
if not funds:
logger.warning("no matching postings found to report")
returncode = returncode or cliutil.ExitCode.NoDataFiltered
elif args.report_type is ReportType.TEXT:
out_file = cliutil.text_output(args.output_file, stdout)
report = TextReport(balances, args.start_date, args.stop_date, out_file)
report.write(funds)
else:
ods_report = ODSReport(balances, args.start_date, args.stop_date)
ods_report.set_common_properties(config.books_repo())
ods_report.write(funds)
if args.output_file is None:
out_dir_path = config.repository_path() or Path()
args.output_file = out_dir_path / 'FundReport_{}_{}.ods'.format(
args.start_date.isoformat(), args.stop_date.isoformat(),
)
logger.info("Writing report to %s", args.output_file)
ods_file = cliutil.bytes_output(args.output_file, stdout)
ods_report.save_file(ods_file)
return returncode
entry_point = cliutil.make_entry_point(__name__, PROGNAME)
if __name__ == '__main__':
exit(entry_point())
|