Tweak general ledger/account transactions report

Correctly show period
Refer to "begin date" and "end date"
This commit is contained in:
RunasSudo 2020-04-01 11:58:35 +11:00
parent ad28244852
commit 0d7e68592b
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
8 changed files with 35 additions and 35 deletions

View File

@ -139,8 +139,8 @@ def pandl():
@app.route('/transactions') @app.route('/transactions')
def transactions(): def transactions():
date = datetime.strptime(flask.request.args['date'], '%Y-%m-%d') date_beg = datetime.strptime(flask.request.args['date_beg'], '%Y-%m-%d')
pstart = datetime.strptime(flask.request.args['pstart'], '%Y-%m-%d') date_end = datetime.strptime(flask.request.args['date_end'], '%Y-%m-%d')
account = flask.request.args.get('account', None) account = flask.request.args.get('account', None)
cash = flask.request.args.get('cash', False) cash = flask.request.args.get('cash', False)
commodity = flask.request.args.get('commodity', False) commodity = flask.request.args.get('commodity', False)
@ -148,42 +148,42 @@ def transactions():
report_currency = Currency(*config['report_currency']) report_currency = Currency(*config['report_currency'])
# General ledger # General ledger
l = ledger.raw_transactions_at_date(date) l = ledger.raw_transactions_at_date(date_end)
if cash: if cash:
l = accounting.ledger_to_cash(l, report_currency) l = accounting.ledger_to_cash(l, report_currency)
# Unrealized gains # Unrealized gains
l = accounting.add_unrealized_gains(accounting.trial_balance(l, date, pstart), report_currency).ledger l = accounting.add_unrealized_gains(accounting.trial_balance(l, date_end, date_beg), report_currency).ledger
if not account: if not account:
# General Ledger # General Ledger
transactions = [t for t in l.transactions if t.date <= date and t.date >= pstart] transactions = [t for t in l.transactions if t.date <= date_end and t.date >= date_beg]
total_dr = sum((p.amount for t in transactions for p in t.postings if p.amount > 0), Balance()).exchange(report_currency, True) total_dr = sum((p.amount for t in transactions for p in t.postings if p.amount > 0), Balance()).exchange(report_currency, True)
total_cr = sum((p.amount for t in transactions for p in t.postings if p.amount < 0), Balance()).exchange(report_currency, True) total_cr = sum((p.amount for t in transactions for p in t.postings if p.amount < 0), Balance()).exchange(report_currency, True)
return flask.render_template('transactions.html', date=date, pstart=pstart, period=describe_period(date, pstart), account=None, ledger=l, transactions=transactions, total_dr=total_dr, total_cr=total_cr, report_currency=report_currency, cash=cash) return flask.render_template('transactions.html', date_beg=date_beg, date_end=date_end, period=describe_period(date_end, date_beg), account=None, ledger=l, transactions=transactions, total_dr=total_dr, total_cr=total_cr, report_currency=report_currency, cash=cash)
elif commodity: elif commodity:
# Account Transactions with commodity detail # Account Transactions with commodity detail
account = l.get_account(account) account = l.get_account(account)
transactions = [t for t in l.transactions if t.date <= date and t.date >= pstart and any(p.account == account for p in t.postings)] 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, pstart - timedelta(days=1), pstart).get_balance(account).clean() opening_balance = accounting.trial_balance(l, date_beg - timedelta(days=1), date_beg).get_balance(account).clean()
closing_balance = accounting.trial_balance(l, date, pstart).get_balance(account).clean() closing_balance = accounting.trial_balance(l, date_end, date_beg).get_balance(account).clean()
def matching_posting(transaction, amount): def matching_posting(transaction, amount):
return next((p for p in transaction.postings if p.account == account and p.amount.currency == amount.currency), None) return next((p for p in transaction.postings if p.account == account and p.amount.currency == amount.currency), None)
return flask.render_template('transactions_commodity.html', date=date, pstart=pstart, period=describe_period(date, pstart), account=account, ledger=l, transactions=transactions, opening_balance=opening_balance, closing_balance=closing_balance, report_currency=report_currency, cash=cash, timedelta=timedelta, matching_posting=matching_posting) return flask.render_template('transactions_commodity.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, matching_posting=matching_posting)
else: else:
# Account Transactions # Account Transactions
account = l.get_account(account) account = l.get_account(account)
transactions = [t for t in l.transactions if t.date <= date and t.date >= pstart and any(p.account == account for p in t.postings)] 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, pstart - timedelta(days=1), pstart).get_balance(account).exchange(report_currency, True) opening_balance = accounting.trial_balance(l, date_beg - timedelta(days=1), date_beg).get_balance(account).exchange(report_currency, True)
closing_balance = accounting.trial_balance(l, date, pstart).get_balance(account).exchange(report_currency, True) closing_balance = accounting.trial_balance(l, date_end, date_beg).get_balance(account).exchange(report_currency, True)
return flask.render_template('transactions.html', date=date, pstart=pstart, period=describe_period(date, pstart), account=account, ledger=l, transactions=transactions, opening_balance=opening_balance, closing_balance=closing_balance, report_currency=report_currency, cash=cash, timedelta=timedelta) 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)
@app.route('/transaction') @app.route('/transaction')
def transaction(): def transaction():

View File

@ -23,9 +23,9 @@
<td style="padding-left: calc(2px + {{ level }}em);"> <td style="padding-left: calc(2px + {{ level }}em);">
{% if balance_sheets|length == 1 %} {% if balance_sheets|length == 1 %}
{% if account.name == config['current_year_earnings'] %} {% if account.name == config['current_year_earnings'] %}
<a href="/pandl?{{ {'date_end': balance_sheets[0].date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else ''}|urlencode }}"> <a href="/pandl?{{ {'date_end': balance_sheets[0].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 }}">
{% else %} {% else %}
<a href="/transactions?{{ {'date': balance_sheets[0].date.strftime('%Y-%m-%d'), 'pstart': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}"> <a href="/transactions?{{ {'date_end': balance_sheets[0].date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">
{% endif %} {% endif %}
{{ account.bits[-1] }} {{ account.bits[-1] }}
</a> </a>
@ -38,9 +38,9 @@
<td> <td>
{% if not amount.near_zero %} {% if not amount.near_zero %}
{% if account.name == config['current_year_earnings'] %} {% 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 ''}|urlencode) }} {{ 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) }}
{% else %} {% else %}
{{ amount|a('/transactions?' + {'date': balance_sheet.date.strftime('%Y-%m-%d'), 'pstart': balance_sheet.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode) }} {{ 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 %} {% endif %}
{% endif %} {% endif %}

View File

@ -54,8 +54,8 @@
<div style="margin-bottom: 1em;"> <div style="margin-bottom: 1em;">
<form action="{{ url_for('transactions') }}"> <form action="{{ url_for('transactions') }}">
<button type="submit">General ledger</button> <button type="submit">General ledger</button>
<label>Date: <input name="date" data-inputgroup="date" value="{{ date.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label> <label>Begin date: <input name="date_beg" data-inputgroup="pstart" value="{{ pstart.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
<label>Period start: <input name="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><input name="cash" data-inputgroup="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label> <label><input name="cash" data-inputgroup="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>
</form> </form>
</div> </div>

View File

@ -22,7 +22,7 @@
<tr> <tr>
<td style="padding-left: calc(2px + {{ level }}em);"> <td style="padding-left: calc(2px + {{ level }}em);">
{% if pandls|length == 1 %} {% if pandls|length == 1 %}
<a href="/transactions?{{ {'date': pandls[0].date.strftime('%Y-%m-%d'), 'pstart': pandls[0].pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ account.bits[-1] }}</a> <a href="/transactions?{{ {'date_end': pandls[0].date.strftime('%Y-%m-%d'), 'date_beg': pandls[0].pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ account.bits[-1] }}</a>
{% else %} {% else %}
{{ account.bits[-1] }} {{ account.bits[-1] }}
{% endif %} {% endif %}

View File

@ -18,11 +18,11 @@
{% extends 'base_report.html' %} {% extends 'base_report.html' %}
{% block title %}{% if account %}Account Transactions{% else %}General Ledger{% endif %} as at {{ date.strftime('%d %B %Y') }}{% endblock %} {% block title %}{% if account %}Account Transactions{% else %}General Ledger{% endif %} for the {{ period }}{% endblock %}
{% block links %} {% block links %}
{{ super() }} {{ super() }}
{% if account %}<a href="/transactions?{{ {'date': date.strftime('%Y-%m-%d'), 'pstart': pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else '', 'commodity': 'on'}|urlencode }}">Show commodity detail</a>{% endif %} {% if account %}<a href="/transactions?{{ {'date_end': date_end.strftime('%Y-%m-%d'), 'date_beg': date_beg.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else '', 'commodity': 'on'}|urlencode }}">Show commodity detail</a>{% endif %}
{% endblock %} {% endblock %}
{% block report %} {% block report %}
@ -46,9 +46,9 @@
{% set ns = namespace(balance=None) %} {% set ns = namespace(balance=None) %}
{% if account %} {% if account %}
{% set prevlink = '/transactions?' + {'date': (pstart - timedelta(days=1)).strftime('%Y-%m-%d'), 'pstart': pstart.replace(year=pstart.year-1).strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode %} {% set prevlink = '/transactions?' + {'date_end': (date_beg - timedelta(days=1)).strftime('%Y-%m-%d'), 'date_beg': date_beg.replace(year=date_beg.year-1).strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode %}
<tr class="total"> <tr class="total">
<td><a href="{{ prevlink }}">{{ pstart.strftime('%Y-%m-%d') }}</a></td> <td><a href="{{ prevlink }}">{{ date_beg.strftime('%Y-%m-%d') }}</a></td>
<td><a href="{{ prevlink }}">Opening Balance</a></td> <td><a href="{{ prevlink }}">Opening Balance</a></td>
<td></td> <td></td>
<td style="text-align: right;"></td> <td style="text-align: right;"></td>
@ -73,7 +73,7 @@
<tr> <tr>
<td>{% if loop.first %}{% if transaction.id %}<a href="{{ trn_url }}">{% endif %}{{ transaction.date.strftime('%Y-%m-%d') }}{% if transaction.id %}</a>{% endif %}{% endif %}</td> <td>{% if loop.first %}{% if transaction.id %}<a href="{{ trn_url }}">{% endif %}{{ transaction.date.strftime('%Y-%m-%d') }}{% if transaction.id %}</a>{% endif %}{% endif %}</td>
<td>{% if loop.first %}{% if transaction.id %}<a href="{{ trn_url }}">{% endif %}{{ transaction.description }}{% if transaction.id %}</a>{% endif %}{% endif %}</td> <td>{% if loop.first %}{% if transaction.id %}<a href="{{ trn_url }}">{% endif %}{{ transaction.description }}{% if transaction.id %}</a>{% endif %}{% endif %}</td>
<td><a href="/transactions?{{ {'date': date.strftime('%Y-%m-%d'), 'pstart': pstart.strftime('%Y-%m-%d'), 'account': posting.account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ (posting.account.name|e).__str__().replace(':', ':<wbr>')|safe }}</a></td> <td><a href="/transactions?{{ {'date_end': date_end.strftime('%Y-%m-%d'), 'date_beg': date_beg.strftime('%Y-%m-%d'), 'account': posting.account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ (posting.account.name|e).__str__().replace(':', ':<wbr>')|safe }}</a></td>
{% if account %} {% if account %}
{# Reverse Dr/Cr so it's from the "perspective" of this account #} {# Reverse Dr/Cr so it's from the "perspective" of this account #}
{% set ns.balance = ns.balance - amount %} {% set ns.balance = ns.balance - amount %}
@ -98,7 +98,7 @@
{% if account %} {% if account %}
<tr class="total"> <tr class="total">
<td>{{ date.strftime('%Y-%m-%d') }}</td> <td>{{ date_end.strftime('%Y-%m-%d') }}</td>
<td>Closing Balance</td> <td>Closing Balance</td>
<td></td> <td></td>
<td style="text-align: right;"></td> <td style="text-align: right;"></td>
@ -113,7 +113,7 @@
</tr> </tr>
{% else %} {% else %}
<tr class="total"> <tr class="total">
<td>{{ date.strftime('%Y-%m-%d') }}</td> <td>{{ date_end.strftime('%Y-%m-%d') }}</td>
<td>Total</td> <td>Total</td>
<td></td> <td></td>
<td style="text-align: right;">{{ total_dr|b }}</td> <td style="text-align: right;">{{ total_dr|b }}</td>

View File

@ -18,7 +18,7 @@
{% extends 'base_report.html' %} {% extends 'base_report.html' %}
{% block title %}Account Transactions as at {{ date.strftime('%d %B %Y') }}{% endblock %} {% block title %}Account Transactions for the {{ period }}{% endblock %}
{% block report %} {% block report %}
<h1>Account Transactions</h1> <h1>Account Transactions</h1>
@ -39,10 +39,10 @@
</tr> </tr>
{% set ns = namespace(balance=None) %} {% set ns = namespace(balance=None) %}
{% set prevlink = '/transactions?' + {'date': (pstart - timedelta(days=1)).strftime('%Y-%m-%d'), 'pstart': pstart.replace(year=pstart.year-1).strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else '', 'commodity': 'on'}|urlencode %} {% set prevlink = '/transactions?' + {'date_end': (date_beg - timedelta(days=1)).strftime('%Y-%m-%d'), 'date_beg': date_beg.replace(year=date_beg.year-1).strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else '', 'commodity': 'on'}|urlencode %}
{% for amount in opening_balance.amounts %} {% for amount in opening_balance.amounts %}
<tr class="total"> <tr class="total">
<td>{% if loop.first %}<a href="{{ prevlink }}">{{ pstart.strftime('%Y-%m-%d') }}</a>{% endif %}</td> <td>{% if loop.first %}<a href="{{ prevlink }}">{{ date_beg.strftime('%Y-%m-%d') }}</a>{% endif %}</td>
<td>{% if loop.first %}<a href="{{ prevlink }}">Opening Balance</a>{% endif %}</td> <td>{% if loop.first %}<a href="{{ prevlink }}">Opening Balance</a>{% endif %}</td>
<td></td> <td></td>
<td></td> <td></td>
@ -52,7 +52,7 @@
</tr> </tr>
{% else %} {% else %}
<tr class="total"> <tr class="total">
<td><a href="{{ prevlink }}">{{ pstart.strftime('%Y-%m-%d') }}</a></td> <td><a href="{{ prevlink }}">{{ date_beg.strftime('%Y-%m-%d') }}</a></td>
<td><a href="{{ prevlink }}">Opening Balance</a></td> <td><a href="{{ prevlink }}">Opening Balance</a></td>
<td></td> <td></td>
<td></td> <td></td>
@ -90,7 +90,7 @@
{% for amount in closing_balance.amounts %} {% for amount in closing_balance.amounts %}
<tr class="total explicit-rules" style="{% if loop.first %}border-top: 1pt solid black;{% endif %}{% if loop.last %}border-bottom: 1pt solid black;{% endif %}"> <tr class="total explicit-rules" style="{% if loop.first %}border-top: 1pt solid black;{% endif %}{% if loop.last %}border-bottom: 1pt solid black;{% endif %}">
<td>{% if loop.first %}{{ date.strftime('%Y-%m-%d') }}{% endif %}</td> <td>{% if loop.first %}{{ date_end.strftime('%Y-%m-%d') }}{% endif %}</td>
<td>{% if loop.first %}Closing Balance{% endif %}</td> <td>{% if loop.first %}Closing Balance{% endif %}</td>
<td></td> <td></td>
<td></td> <td></td>

View File

@ -33,7 +33,7 @@
{% for account in accounts %} {% for account in accounts %}
{# Display in "cost basis" as we have already accounted for unrealised gains #} {# Display in "cost basis" as we have already accounted for unrealised gains #}
{% set balance = trial_balance.get_balance(account).exchange(report_currency, True) %} {% set balance = trial_balance.get_balance(account).exchange(report_currency, True) %}
{% set trn_url = "/transactions?" + {'date': trial_balance.date.strftime('%Y-%m-%d'), 'pstart': trial_balance.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode %} {% set trn_url = "/transactions?" + {'date_end': trial_balance.date.strftime('%Y-%m-%d'), 'date_beg': trial_balance.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode %}
{% if not balance.near_zero %} {% if not balance.near_zero %}
<tr> <tr>
<td><a href="{{ trn_url }}">{{ account.name }}</a></td> <td><a href="{{ trn_url }}">{{ account.name }}</a></td>

View File

@ -34,7 +34,7 @@
<td>{{ account.name }}</td> <td>{{ account.name }}</td>
{% for trial_balance in trial_balances %} {% for trial_balance in trial_balances %}
{% set balance = trial_balance.get_balance(account).exchange(report_currency, True) %} {% set balance = trial_balance.get_balance(account).exchange(report_currency, True) %}
<td>{% if not balance.near_zero %}<a href="/transactions?{{ {'date': trial_balance.date.strftime('%Y-%m-%d'), 'pstart': trial_balance.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ balance|abs|b }} {% if balance >= 0 %}Dr{% else %}Cr{% endif %}</a>{% endif %}</td> <td>{% if not balance.near_zero %}<a href="/transactions?{{ {'date_end': trial_balance.date.strftime('%Y-%m-%d'), 'date_beg': trial_balance.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ balance|abs|b }} {% if balance >= 0 %}Dr{% else %}Cr{% endif %}</a>{% endif %}</td>
{% endfor %} {% endfor %}
</tr> </tr>
{% endfor %} {% endfor %}