Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
112 lines
3.8 KiB
Python
112 lines
3.8 KiB
Python
"""Reports built on top of the journal."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime as dt
|
|
from dataclasses import dataclass
|
|
from typing import Dict, List, Optional, Tuple
|
|
|
|
from .accounts import AccountKind
|
|
from .journal import Journal
|
|
from .money import Money
|
|
from .utils import first_of_month, group_by
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class IncomeStatement:
|
|
income: Money
|
|
expenses: Money
|
|
by_account: Dict[str, Money]
|
|
|
|
@property
|
|
def net(self) -> Money:
|
|
return self.income - self.expenses
|
|
|
|
|
|
def monthly_totals(
|
|
journal: Journal,
|
|
code: str,
|
|
start: Optional[dt.date] = None,
|
|
end: Optional[dt.date] = None,
|
|
currency: Optional[str] = None,
|
|
) -> Dict[dt.date, Money]:
|
|
"""Net movement of an account per calendar month, keyed by the first day of the month, oldest first."""
|
|
currency = currency or journal.base_currency
|
|
transactions = journal.transactions(start, end, code)
|
|
groups = group_by(transactions, lambda tx: tx.date.month)
|
|
totals: Dict[dt.date, Money] = {}
|
|
for items in groups.values():
|
|
month = first_of_month(items[0].date)
|
|
total = Money.zero(currency)
|
|
for tx in items:
|
|
total = total + tx.amount_for(code, currency)
|
|
totals[month] = total
|
|
return totals
|
|
|
|
|
|
def income_statement(
|
|
journal: Journal,
|
|
start: dt.date,
|
|
end: dt.date,
|
|
currency: Optional[str] = None,
|
|
) -> IncomeStatement:
|
|
"""Income and expenses in ``[start, end]``; both totals are positive numbers."""
|
|
currency = currency or journal.base_currency
|
|
income = Money.zero(currency)
|
|
expenses = Money.zero(currency)
|
|
by_account: Dict[str, Money] = {}
|
|
for account in journal.chart:
|
|
if account.kind not in (AccountKind.INCOME, AccountKind.EXPENSE):
|
|
continue
|
|
moved = journal.movement(account.code, start, end, currency)
|
|
if moved.is_zero():
|
|
continue
|
|
if account.kind is AccountKind.INCOME:
|
|
by_account[account.code] = -moved
|
|
income = income - moved
|
|
else:
|
|
by_account[account.code] = moved
|
|
expenses = expenses + moved
|
|
return IncomeStatement(income, expenses, by_account)
|
|
|
|
|
|
def balance_sheet(journal: Journal, as_of: dt.date, currency: Optional[str] = None) -> Dict[str, Money]:
|
|
"""Assets, liabilities and equity (including earnings not yet closed to equity)."""
|
|
totals = journal.kind_totals(as_of, currency)
|
|
assets = totals[AccountKind.ASSET]
|
|
liabilities = -totals[AccountKind.LIABILITY]
|
|
equity = -(totals[AccountKind.EQUITY] + totals[AccountKind.INCOME] + totals[AccountKind.EXPENSE])
|
|
return {"assets": assets, "liabilities": liabilities, "equity": equity}
|
|
|
|
|
|
def trial_balance_rows(
|
|
journal: Journal,
|
|
as_of: Optional[dt.date] = None,
|
|
currency: Optional[str] = None,
|
|
) -> List[Tuple[str, str, Money, Money]]:
|
|
"""Rows of ``(code, name, debit, credit)`` for every account with a balance."""
|
|
currency = currency or journal.base_currency
|
|
rows = []
|
|
for code, value in journal.trial_balance(as_of, currency).items():
|
|
name = journal.chart.get(code).name
|
|
zero = Money.zero(currency)
|
|
if value.amount > 0:
|
|
rows.append((code, name, value, zero))
|
|
else:
|
|
rows.append((code, name, zero, -value))
|
|
return rows
|
|
|
|
|
|
def top_expenses(
|
|
journal: Journal,
|
|
start: dt.date,
|
|
end: dt.date,
|
|
limit: int = 3,
|
|
) -> List[Tuple[str, Money]]:
|
|
"""The biggest expense accounts in the period, largest first (ties by account code)."""
|
|
statement = income_statement(journal, start, end)
|
|
expense_codes = [a.code for a in journal.chart.by_kind(AccountKind.EXPENSE)]
|
|
ranked = [(code, statement.by_account[code]) for code in expense_codes if code in statement.by_account]
|
|
ranked.sort(key=lambda item: (-item[1].amount, item[0]))
|
|
return ranked[:limit]
|