From a9ac523d1bb19cb7d72f39005c6a7a98f08a3f23 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Wed, 1 Apr 2020 14:40:55 +1100 Subject: [PATCH] Fix treatment of unrealised gains in comparative statements --- ledger_pyreport/__init__.py | 15 ++++++++------- ledger_pyreport/jinja2/balance.html | 2 ++ ledger_pyreport/model.py | 8 ++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ledger_pyreport/__init__.py b/ledger_pyreport/__init__.py index 21a97c6..3abf125 100644 --- a/ledger_pyreport/__init__.py +++ b/ledger_pyreport/__init__.py @@ -69,7 +69,7 @@ def trial(): l = ledger.raw_transactions_at_date(date) if cash: l = accounting.ledger_to_cash(l, report_currency) - trial_balances = [accounting.trial_balance(l, d, p, report_currency) for d, p in zip(dates, pstarts)] + trial_balances = [accounting.trial_balance(l.clone(), d, p, report_currency) for d, p in zip(dates, pstarts)] # Delete accounts with always zero balances accounts = sorted(l.accounts.values(), key=lambda a: a.name) @@ -93,7 +93,7 @@ def balance(): l = ledger.raw_transactions_at_date(date) if cash: l = accounting.ledger_to_cash(l, report_currency) - balance_sheets = [accounting.balance_sheet(accounting.trial_balance(l, d, p, report_currency)) for d, p in zip(dates, pstarts)] + balance_sheets = [accounting.balance_sheet(accounting.trial_balance(l.clone(), d, p, report_currency)) for d, p in zip(dates, pstarts)] # Delete accounts with always zero balances accounts = list(l.accounts.values()) @@ -126,7 +126,7 @@ def pandl(): l = ledger.raw_transactions_at_date(date_end) if cash: l = accounting.ledger_to_cash(l, report_currency) - pandls = [accounting.trial_balance(l, de, db, report_currency) for de, db in zip(dates_end, dates_beg)] + pandls = [accounting.trial_balance(l.clone(), de, db, report_currency) for de, db in zip(dates_end, dates_beg)] # Delete accounts with always zero balances accounts = list(l.accounts.values()) @@ -167,8 +167,9 @@ def transactions(): account = l.get_account(account) transactions = [t for t in l.transactions if t.date <= date_end and t.date >= date_beg and any(p.account == account for p in t.postings)] - opening_balance = accounting.trial_balance(l, date_beg - timedelta(days=1), date_beg, report_currency).get_balance(account).clean() - closing_balance = accounting.trial_balance(l, date_end, date_beg, report_currency).get_balance(account).clean() + # Use trial_balance_raw because ledger is already adjusted for unrealised gains, etc. + opening_balance = accounting.trial_balance_raw(l, date_beg - timedelta(days=1), date_beg).get_balance(account).clean() + closing_balance = accounting.trial_balance_raw(l, date_end, date_beg).get_balance(account).clean() def matching_posting(transaction, amount): return next((p for p in transaction.postings if p.account == account and p.amount.currency == amount.currency), None) @@ -179,8 +180,8 @@ def transactions(): account = l.get_account(account) transactions = [t for t in l.transactions if t.date <= date_end and t.date >= date_beg and any(p.account == account for p in t.postings)] - opening_balance = accounting.trial_balance(l, date_beg - timedelta(days=1), date_beg, report_currency).get_balance(account).exchange(report_currency, True) - closing_balance = accounting.trial_balance(l, date_end, date_beg, report_currency).get_balance(account).exchange(report_currency, True) + opening_balance = accounting.trial_balance_raw(l, date_beg - timedelta(days=1), date_beg).get_balance(account).exchange(report_currency, True) + closing_balance = accounting.trial_balance_raw(l, date_end, date_beg).get_balance(account).exchange(report_currency, True) return flask.render_template('transactions.html', date_beg=date_beg, date_end=date_end, period=describe_period(date_end, date_beg), account=account, ledger=l, transactions=transactions, opening_balance=opening_balance, closing_balance=closing_balance, report_currency=report_currency, cash=cash, timedelta=timedelta) diff --git a/ledger_pyreport/jinja2/balance.html b/ledger_pyreport/jinja2/balance.html index 103dee1..212fc35 100644 --- a/ledger_pyreport/jinja2/balance.html +++ b/ledger_pyreport/jinja2/balance.html @@ -41,6 +41,8 @@ {% if not amount.near_zero %} {% if account.name == config['current_year_earnings'] %} {{ amount|a('/pandl?' + {'date_end': balance_sheet.date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else '', 'scope': 'both'}|urlencode) }} + {% elif account.name == config['current_year_oci'] %} + {{ amount|a('/pandl?' + {'date_end': balance_sheet.date.strftime('%Y-%m-%d'), 'date_beg': balance_sheet.pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else '', 'scope': 'oci'}|urlencode) }} {% else %} {{ amount|a('/transactions?' + {'date_end': balance_sheet.date.strftime('%Y-%m-%d'), 'date_beg': balance_sheet.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode) }} {% endif %} diff --git a/ledger_pyreport/model.py b/ledger_pyreport/model.py index eb8bb3e..bb56b03 100644 --- a/ledger_pyreport/model.py +++ b/ledger_pyreport/model.py @@ -29,6 +29,14 @@ class Ledger: self.prices = [] + def clone(self): + result = Ledger(self.date) + result.root_account = self.root_account + result.accounts = self.accounts + result.transactions = self.transactions[:] + result.prices = self.prices + return result + def get_account(self, name): if name == '': return self.root_account