diff --git a/drcr/reports.py b/drcr/reports.py index 47546fc..cb79160 100644 --- a/drcr/reports.py +++ b/drcr/reports.py @@ -65,10 +65,11 @@ class Section: return None class Entry: - def __init__(self, text=None, amount=None, *, id=None, heading=False, bordered=False): + def __init__(self, text=None, amount=None, *, id=None, link=None, heading=False, bordered=False): self.text = text self.amount = amount self.id = id + self.link = link self.heading = heading self.bordered = bordered @@ -90,12 +91,9 @@ class Subtotal: ) class Calculated(Entry): - def __init__(self, text=None, calc=None, *, id=None, heading=False, bordered=False): - self.text = text + def __init__(self, text=None, calc=None, **kwargs): + super().__init__(text=text, **kwargs) self.calc = calc - self.id = id - self.heading = heading - self.bordered = bordered self.amount = None @@ -114,6 +112,13 @@ def validate_accounts(accounts, account_configurations): if n != 1: raise Exception('Account "{}" mapped to {} account types (expected 1)'.format(account, n)) +def entries_for_kind(account_configurations, accounts, kind, neg=False): + return [ + Entry(text=account_name, amount=-amount if neg else amount, link='/account-transactions?account=' + account_name) + for account_name, amount in accounts.items() + if kind in account_configurations.get(account_name, []) and amount.quantity != 0 + ] + def balance_sheet_report(): # Get trial balance balancer = TrialBalancer() @@ -130,32 +135,21 @@ def balance_sheet_report(): entries=[ Section( title='Assets', - entries=[ - Entry(text=account_name, amount=amount) - for account_name, amount in accounts.items() - if 'drcr.asset' in account_configurations.get(account_name, []) - ] + [Subtotal('Total assets', bordered=True)] + entries=entries_for_kind(account_configurations, accounts, 'drcr.asset') + [Subtotal('Total assets', bordered=True)] ), Spacer(), Section( title='Liabilities', - entries=[ - Entry(text=account_name, amount=-amount) - for account_name, amount in accounts.items() - if 'drcr.liability' in account_configurations.get(account_name, []) - ] + [Subtotal('Total liabilities', bordered=True)] + entries=entries_for_kind(account_configurations, accounts, 'drcr.liability', True) + [Subtotal('Total liabilities', bordered=True)] ), Spacer(), Section( title='Equity', - entries=[ - Entry(text=account_name, amount=-amount) - for account_name, amount in accounts.items() - if 'drcr.equity' in account_configurations.get(account_name, []) - ] + [ + entries=entries_for_kind(account_configurations, accounts, 'drcr.equity', True) + [ Calculated( 'Current year surplus (deficit)', - lambda _: income_statement_report().by_id('net_surplus').amount + lambda _: income_statement_report().by_id('net_surplus').amount, + link='/income-statement' ), Subtotal('Total equity', bordered=True) ] @@ -182,20 +176,12 @@ def income_statement_report(): entries=[ Section( title='Income', - entries=[ - Entry(text=account_name, amount=-amount) - for account_name, amount in accounts.items() - if 'drcr.income' in account_configurations.get(account_name, []) - ] + [Subtotal('Total income', id='total_income', bordered=True)] + entries=entries_for_kind(account_configurations, accounts, 'drcr.income', True) + [Subtotal('Total income', id='total_income', bordered=True)] ), Spacer(), Section( title='Expenses', - entries=[ - Entry(text=account_name, amount=amount) - for account_name, amount in accounts.items() - if 'drcr.expense' in account_configurations.get(account_name, []) - ] + [Subtotal('Total expenses', id='total_expenses', bordered=True)] + entries=entries_for_kind(account_configurations, accounts, 'drcr.expense') + [Subtotal('Total expenses', id='total_expenses', bordered=True)] ), Spacer(), Calculated( diff --git a/drcr/templates/report.html b/drcr/templates/report.html index 79b24d0..020cfd5 100644 --- a/drcr/templates/report.html +++ b/drcr/templates/report.html @@ -40,7 +40,13 @@   {% else %} - <{{ 'th' if entry.heading else 'td' }}>{{ entry.text }} + <{{ 'th' if entry.heading else 'td' }}> + {% if entry.link %} + {{ entry.text }} + {% else %} + {{ entry.text }} + {% endif %} + <{{ 'th' if entry.heading else 'td' }} class="text-end">{{ entry.amount.format_accounting() }} {% endif %}