Implement cash flow statement (direct)
This commit is contained in:
parent
4c6776f545
commit
17e79acb4a
@ -136,6 +136,43 @@ def pandl():
|
||||
|
||||
return flask.render_template('pandl.html', period=describe_period(date_end, date_beg), ledger=l, pandls=pandls, accounts=accounts, config=config, report_currency=report_currency, cash=cash, scope=scope)
|
||||
|
||||
@app.route('/cashflow')
|
||||
def cashflow():
|
||||
date_beg = datetime.strptime(flask.request.args['date_beg'], '%Y-%m-%d')
|
||||
date_end = datetime.strptime(flask.request.args['date_end'], '%Y-%m-%d')
|
||||
compare = int(flask.request.args['compare'])
|
||||
method = flask.request.args['method']
|
||||
|
||||
dates_beg = [date_beg.replace(year=date_beg.year - i) for i in range(0, compare + 1)]
|
||||
dates_end = [date_end.replace(year=date_end.year - i) for i in range(0, compare + 1)]
|
||||
|
||||
report_currency = Currency(*config['report_currency'])
|
||||
l = ledger.raw_transactions_at_date(date_end)
|
||||
|
||||
cash_accounts = [a for a in l.accounts.values() if a.is_cash]
|
||||
|
||||
# Calculate opening and closing cash
|
||||
opening_balances = []
|
||||
closing_balances = []
|
||||
cashflows = []
|
||||
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))
|
||||
|
||||
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':
|
||||
# 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)
|
||||
|
||||
@app.route('/transactions')
|
||||
def transactions():
|
||||
date_beg = datetime.strptime(flask.request.args['date_beg'], '%Y-%m-%d')
|
||||
|
@ -166,3 +166,18 @@ def ledger_to_cash(ledger, currency):
|
||||
account_to_cash(account, currency)
|
||||
|
||||
return ledger
|
||||
|
||||
# Summarise related transactions
|
||||
def account_flows(ledger, date, pstart, accounts):
|
||||
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:
|
||||
continue
|
||||
|
||||
tb.balances[posting.account.name] = tb.get_balance(posting.account) - posting.amount
|
||||
|
||||
return tb
|
||||
|
76
ledger_pyreport/jinja2/cashflow_direct.html
Normal file
76
ledger_pyreport/jinja2/cashflow_direct.html
Normal file
@ -0,0 +1,76 @@
|
||||
{#
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
#}
|
||||
|
||||
{% extends 'base_report.html' %}
|
||||
|
||||
{% macro print_rows(account, invert=False, level=0) %}
|
||||
<tr>
|
||||
<td style="padding-left: calc(2px + {{ level }}em);">
|
||||
{% if cashflows|length == 1 %}
|
||||
<a href="/transactions?{{ {'date_end': cashflows[0].date.strftime('%Y-%m-%d'), 'date_beg': cashflows[0].pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ account.bits[-1] }}</a>
|
||||
{% else %}
|
||||
{{ account.bits[-1] }}
|
||||
{% endif %}
|
||||
</td>
|
||||
{% for cashflow in cashflows %}
|
||||
{% set amount = (-cashflow.get_balance(account) if invert else cashflow.get_balance(account)).exchange(report_currency, True) %}
|
||||
<td>{% 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 %}</td>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
|
||||
{% 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 %}
|
||||
<h1>Cash Flow Statement</h1>
|
||||
<h2>For the {{ period }}</h2>
|
||||
|
||||
<table class="ledger onedesc">
|
||||
{# Cash flows #}
|
||||
<tr>
|
||||
<th class="h2">Cash Inflows (Outflows)</th>
|
||||
{% for cashflow in cashflows %}<th class="h2">{{ cashflow.date.strftime('%Y') }} </th>{% endfor %}
|
||||
</tr>
|
||||
{% for account in ledger.root_account.children|sort(attribute='name') if account in accounts %}
|
||||
{{ print_rows(account, invert=invert) }}
|
||||
{% endfor %}
|
||||
<tr class="total">
|
||||
<td>Net Cash Inflow (Outflow)</td>
|
||||
{% for cashflow in cashflows %}<td>{{ cashflow.get_total(ledger.root_account).exchange(report_currency, True)|a }}</td>{% endfor %}
|
||||
</tr>
|
||||
<tr><td colspan="{{ cashflows|length + 1 }}"> </td></tr>
|
||||
|
||||
{# Summary #}
|
||||
<tr>
|
||||
<td>Opening Cash</td>
|
||||
{% for opening_balance in opening_balances %}<td>{{ opening_balance|a }}</td>{% endfor %}
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Net Cash Inflow (Outflow)</td>
|
||||
{% for cashflow in cashflows %}<td>{{ cashflow.get_total(ledger.root_account).exchange(report_currency, True)|a }}</td>{% endfor %}
|
||||
</tr>
|
||||
<tr class="total">
|
||||
<td>Closing Cash</td>
|
||||
{% for closing_balance in closing_balances %}<td>{{ closing_balance|a }}</td>{% endfor %}
|
||||
</tr>
|
||||
</table>
|
||||
{% endblock %}
|
@ -56,6 +56,19 @@
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div style="margin-bottom: 1em;">
|
||||
<form action="{{ url_for('cashflow') }}">
|
||||
<button type="submit">Cash flows</button>
|
||||
<label>Begin date: <input name="date_beg" data-inputgroup="pstart" value="{{ pstart.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
|
||||
<label>End date: <input name="date_end" data-inputgroup="date" value="{{ date.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
|
||||
<label>Compare <input name="compare" data-inputgroup="compare" value="0" style="width: 2em;" oninput="txtc(this)"> periods</label>
|
||||
<label>Method: <select name="method">
|
||||
<option value="indirect" selected>Indirect</option>
|
||||
<option value="direct">Direct</option>
|
||||
</select></label>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div style="margin-bottom: 1em;">
|
||||
<form action="{{ url_for('transactions') }}">
|
||||
<button type="submit">General ledger</button>
|
||||
|
Reference in New Issue
Block a user