diff --git a/ledger_pyreport/__init__.py b/ledger_pyreport/__init__.py index 94d7a48..8687a7b 100644 --- a/ledger_pyreport/__init__.py +++ b/ledger_pyreport/__init__.py @@ -155,6 +155,7 @@ def cashflow(): opening_balances = [] closing_balances = [] cashflows = [] + profits = [] for de, db in zip(dates_end, dates_beg): tb = accounting.trial_balance(l.clone(), db - timedelta(days=1), db, report_currency) opening_balances.append(sum((tb.get_balance(a) for a in cash_accounts), Balance()).exchange(report_currency, True)) @@ -162,16 +163,27 @@ def cashflow(): tb = accounting.trial_balance(l.clone(), de, db, report_currency) closing_balances.append(sum((tb.get_balance(a) for a in cash_accounts), Balance()).exchange(report_currency, True)) - cashflows.append(accounting.account_flows(tb.ledger, de, db, cash_accounts)) + if method == 'direct': + # Determine transactions affecting cash assets + cashflows.append(accounting.account_flows(tb.ledger, de, db, cash_accounts, True)) + else: + # Determine net profit (loss) + profits.append(-(tb.get_total(tb.ledger.get_account(config['income_account'])) + tb.get_total(tb.ledger.get_account(config['expenses_account'])) + tb.get_total(tb.ledger.get_account(config['oci_account']))).exchange(report_currency, True)) + + # Determine transactions affecting equity, liabilities and non-cash assets + noncash_accounts = [a for a in l.accounts.values() if a.is_equity or a.is_liability or (a.is_asset and not a.is_cash)] + cashflows.append(accounting.account_flows(tb.ledger, de, db, noncash_accounts, False)) + + # Delete accounts with always zero balances + accounts = list(l.accounts.values()) + for account in accounts[:]: + if all(p.get_balance(account) == 0 and p.get_total(account) == 0 for p in cashflows): + accounts.remove(account) if method == 'direct': - # Delete accounts with always zero balances - accounts = list(l.accounts.values()) - for account in accounts[:]: - if all(p.get_balance(account) == 0 and p.get_total(account) == 0 for p in cashflows): - accounts.remove(account) - return flask.render_template('cashflow_direct.html', period=describe_period(date_end, date_beg), ledger=l, cashflows=cashflows, opening_balances=opening_balances, closing_balances=closing_balances, accounts=accounts, config=config, report_currency=report_currency) + else: + return flask.render_template('cashflow_indirect.html', period=describe_period(date_end, date_beg), ledger=l, cashflows=cashflows, profits=profits, opening_balances=opening_balances, closing_balances=closing_balances, accounts=accounts, config=config, report_currency=report_currency) @app.route('/transactions') def transactions(): diff --git a/ledger_pyreport/accounting.py b/ledger_pyreport/accounting.py index 9a1bc33..01f9978 100644 --- a/ledger_pyreport/accounting.py +++ b/ledger_pyreport/accounting.py @@ -168,14 +168,14 @@ def ledger_to_cash(ledger, currency): return ledger # Summarise related transactions -def account_flows(ledger, date, pstart, accounts): +def account_flows(ledger, date, pstart, accounts, related): transactions = [t for t in ledger.transactions if any(p.account in accounts for p in t.postings) and t.date <= date and t.date >= pstart] tb = TrialBalance(ledger, date, pstart) for transaction in transactions: for posting in transaction.postings: - if posting.account in accounts: + if (posting.account in accounts) is related: continue tb.balances[posting.account.name] = tb.get_balance(posting.account) - posting.amount diff --git a/ledger_pyreport/jinja2/cashflow_indirect.html b/ledger_pyreport/jinja2/cashflow_indirect.html new file mode 100644 index 0000000..1db0021 --- /dev/null +++ b/ledger_pyreport/jinja2/cashflow_indirect.html @@ -0,0 +1,90 @@ +{# + ledger-pyreport + Copyright © 2020 Lee Yingtong Li (RunasSudo) + + 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 . +#} + +{% extends 'base_report.html' %} + +{% macro print_rows(account, invert=False, level=0) %} + + + {% if cashflows|length == 1 %} + {{ account.bits[-1] }} + {% else %} + {{ account.bits[-1] }} + {% endif %} + + {% for cashflow in cashflows %} + {% set amount = (-cashflow.get_balance(account) if invert else cashflow.get_balance(account)).exchange(report_currency, True) %} + {% if not amount.near_zero %}{{ amount|a('/transactions?' + {'date': cashflow.date.strftime('%Y-%m-%d'), 'pstart': cashflow.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode) }}{% endif %} + {% endfor %} + + + {% for child in account.children|sort(attribute='name') if child in accounts %} + {{ print_rows(child, invert, level + 1) }} + {% endfor %} +{% endmacro %} + +{% block title %}Cash Flow Statement for the {{ period }}{% endblock %} + +{% block report %} +

Cash Flow Statement

+

For the {{ period }}

+ + + {# Profit and loss #} + + + {% for cashflow in cashflows %}{% endfor %} + + + + {% for profit in profits %}{% endfor %} + + + + {# Cash flows #} + + + + {% for account in ledger.root_account.children|sort(attribute='name') if account in accounts %} + {{ print_rows(account, invert=invert) }} + {% endfor %} + + + {% for cashflow in cashflows %}{% endfor %} + + + + {% for cashflow in cashflows %}{% endfor %} + + + + {# Summary #} + + + {% for opening_balance in opening_balances %}{% endfor %} + + + + {% for cashflow in cashflows %}{% endfor %} + + + + {% for closing_balance in closing_balances %}{% endfor %} + +
{{ cashflow.date.strftime('%Y') }} 
Total Comprehensive Income{{ profit|a }}
 
Adjustments for Changes in Non-cash Accounts
Total Adjustments{{ cashflow.get_total(ledger.root_account).exchange(report_currency, True)|a }}
Net Cash Inflow (Outflow){{ (profits[loop.index0] + cashflow.get_total(ledger.root_account).exchange(report_currency, True))|a }}
 
Opening Cash{{ opening_balance|a }}
Net Cash Inflow (Outflow){{ (profits[loop.index0] + cashflow.get_total(ledger.root_account).exchange(report_currency, True))|a }}
Closing Cash{{ closing_balance|a }}
+{% endblock %}