Commodity detail for transaction detail

Combine account transactions endpoints w/wo commodity detail
This commit is contained in:
RunasSudo 2020-03-29 22:01:52 +11:00
parent 63a4b6bdb2
commit 516afe9aed
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
6 changed files with 99 additions and 43 deletions

View File

@ -142,6 +142,7 @@ def transactions():
pstart = datetime.strptime(flask.request.args['pstart'], '%Y-%m-%d') pstart = datetime.strptime(flask.request.args['pstart'], '%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)
report_currency = Currency(*config['report_currency']) report_currency = Currency(*config['report_currency'])
@ -154,38 +155,15 @@ def transactions():
l = accounting.add_unrealized_gains(accounting.trial_balance(l, date, pstart), report_currency).ledger l = accounting.add_unrealized_gains(accounting.trial_balance(l, date, pstart), report_currency).ledger
if not account: if not account:
# 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 and t.date >= pstart]
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=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)
else: elif commodity:
account = l.get_account(account) # Account Transactions with commodity detail
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)]
opening_balance = accounting.trial_balance(l, pstart, pstart).get_balance(account).exchange(report_currency, True)
closing_balance = accounting.trial_balance(l, date, pstart).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)
@app.route('/transactions_commodity')
def transactions_commodity():
date = datetime.strptime(flask.request.args['date'], '%Y-%m-%d')
pstart = datetime.strptime(flask.request.args['pstart'], '%Y-%m-%d')
account = flask.request.args.get('account', None)
cash = flask.request.args.get('cash', False)
report_currency = Currency(*config['report_currency'])
# General ledger
l = ledger.raw_transactions_at_date(date)
if cash:
l = accounting.ledger_to_cash(l, report_currency)
# Unrealized gains
l = accounting.add_unrealized_gains(accounting.trial_balance(l, date, pstart), report_currency).ledger
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 and t.date >= pstart and any(p.account == account for p in t.postings)]
@ -196,11 +174,21 @@ def transactions_commodity():
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=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)
else:
# Account Transactions
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)]
opening_balance = accounting.trial_balance(l, pstart, pstart).get_balance(account).exchange(report_currency, True)
closing_balance = accounting.trial_balance(l, date, pstart).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)
@app.route('/transaction') @app.route('/transaction')
def transaction(): def transaction():
tid = flask.request.args['tid'] tid = flask.request.args['tid']
cash = flask.request.args.get('cash', False) cash = flask.request.args.get('cash', False)
commodity = flask.request.args.get('commodity', False)
report_currency = Currency(*config['report_currency']) report_currency = Currency(*config['report_currency'])
@ -214,6 +202,9 @@ def transaction():
total_dr = sum((p.amount for p in transaction.postings if p.amount > 0), Balance()).exchange(report_currency, True) total_dr = sum((p.amount for p in transaction.postings if p.amount > 0), Balance()).exchange(report_currency, True)
total_cr = sum((p.amount for p in transaction.postings if p.amount < 0), Balance()).exchange(report_currency, True) total_cr = sum((p.amount for p in transaction.postings if p.amount < 0), Balance()).exchange(report_currency, True)
if commodity:
return flask.render_template('transaction_commodity.html', ledger=l, transaction=transaction, total_dr=total_dr, total_cr=total_cr, report_currency=report_currency, cash=cash)
else:
return flask.render_template('transaction.html', ledger=l, transaction=transaction, total_dr=total_dr, total_cr=total_cr, report_currency=report_currency, cash=cash) return flask.render_template('transaction.html', ledger=l, transaction=transaction, total_dr=total_dr, total_cr=total_cr, report_currency=report_currency, cash=cash)
# Template filters # Template filters

View File

@ -30,8 +30,8 @@
<th style="width: 5em;">Date</th> <th style="width: 5em;">Date</th>
<th>Description</th> <th>Description</th>
<th style="max-width: 8em;">Account</th> <th style="max-width: 8em;">Account</th>
<th class="h1" style="text-align: right; width: 5em;">Dr</th> <th style="text-align: right; width: 5em;">Dr</th>
<th class="h1" style="text-align: right; width: 5em;">Cr</th> <th style="text-align: right; width: 5em;">Cr</th>
</tr> </tr>
{% for transaction in transactions %} {% for transaction in transactions %}

View File

@ -20,6 +20,11 @@
{% block title %}{{ transaction.description }}{% endblock %} {% block title %}{{ transaction.description }}{% endblock %}
{% block links %}
{{ super() }}
<a href="/transaction?{{ {'tid': transaction.id, 'cash': 'on' if cash else '', 'commodity': 'on'}|urlencode }}">Show commodity detail</a>
{% endblock %}
{% block report %} {% block report %}
<h1 style="margin-bottom: 1em;">Transaction</h1> <h1 style="margin-bottom: 1em;">Transaction</h1>
@ -41,8 +46,8 @@
{#<th style="width: 5em;">Date</th>#} {#<th style="width: 5em;">Date</th>#}
<th>Description</th> <th>Description</th>
<th style="max-width: 8em;">Account</th> <th style="max-width: 8em;">Account</th>
<th class="h1" style="text-align: right; width: 5em;">Dr</th> <th style="text-align: right; width: 5em;">Dr</th>
<th class="h1" style="text-align: right; width: 5em;">Cr</th> <th style="text-align: right; width: 5em;">Cr</th>
</tr> </tr>
{% for posting in transaction.postings %} {% for posting in transaction.postings %}

View File

@ -0,0 +1,60 @@
{#
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' %}
{% block title %}{{ transaction.description }}{% endblock %}
{% block report %}
<h1 style="margin-bottom: 1em;">Transaction</h1>
<table class="ledger" style="margin-bottom: 1em;">
<tr>
<th style="width: 5em;">Date</th>
<th style="width: 5em;">Code</th>
<th>Description</th>
</tr>
<tr>
<td>{{ transaction.date.strftime('%Y-%m-%d') }}</td>
<td>{{ transaction.code }}</td>
<td>{{ transaction.description }}</td>
</tr>
</table>
<table class="ledger">
<tr>
{#<th style="width: 5em;">Date</th>#}
<th>Description</th>
<th style="max-width: 8em;">Account</th>
<th style="width: 1em;"></th>
<th style="text-align: right; width: 5em;">Amount</th>
<th style="width: 4em;"></th>
</tr>
{% for posting in transaction.postings %}
{% set trn_url = '/transaction?' + {'tid': transaction.id, 'cash': 'on' if cash else ''}|urlencode %}
<tr>
<td>{{ posting.comment }}</td>
<td>{{ (posting.account.name|e).__str__().replace(':', ':<wbr>')|safe }}</td>
<td>{% if posting.amount >= 0 %}Dr{% else %}Cr{% endif %}</td>
<td style="text-align: right;"><span title="{{ posting.amount.tostr(False) }}">{{ posting.amount|abs|bc }}</span></td>
<td>{% if posting.amount.currency.price %}<span title="{{ posting.amount.tostr(False) }}">{{ '{' + posting.amount.currency.price|bc + '}' }}</span>{% endif %}</td>
</tr>
{% endfor %}
</table>
{% endblock %}

View File

@ -22,7 +22,7 @@
{% block links %} {% block links %}
{{ super() }} {{ super() }}
{% if account %}<a href="/transactions_commodity?{{ {'date': date.strftime('%Y-%m-%d'), 'pstart': pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">Show commodity detail</a>{% endif %} {% 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 %}
{% endblock %} {% endblock %}
{% block report %} {% block report %}
@ -39,9 +39,9 @@
<th style="width: 5em;">Date</th> <th style="width: 5em;">Date</th>
<th>Description</th> <th>Description</th>
<th style="max-width: 8em;">{% if account %}Related {% endif %}Account</th> <th style="max-width: 8em;">{% if account %}Related {% endif %}Account</th>
<th class="h1" style="text-align: right; width: 5em;">Dr</th> <th style="text-align: right; width: 5em;">Dr</th>
<th class="h1" style="text-align: right; width: 5em;">Cr</th> <th style="text-align: right; width: 5em;">Cr</th>
{% if account %}<th class="h1" style="text-align: right; width: 6em;">Balance</th>{% endif %} {% if account %}<th style="text-align: right; width: 6em;">Balance</th>{% endif %}
</tr> </tr>
{% set ns = namespace(balance=None) %} {% set ns = namespace(balance=None) %}

View File

@ -29,12 +29,12 @@
<tr> <tr>
<th style="width: 5em;">Date</th> <th style="width: 5em;">Date</th>
<th>Description</th> <th>Description</th>
<th class="h1" style="width: 1em;"></th> <th style="width: 1em;"></th>
<th class="h1" style="text-align: right; width: 5em;">Amount</th> <th style="text-align: right; width: 5em;">Amount</th>
{#<th class="h1" style="width: 4em;"></th>#} {#<th style="width: 4em;"></th>#}
<th class="h1" style="text-align: right; width: 5em;">Balance</th> <th style="text-align: right; width: 5em;">Balance</th>
<th class="h1" style="width: 4em;"></th> <th style="width: 4em;"></th>
<th class="h1" style="width: 1em;"></th> <th style="width: 1em;"></th>
</tr> </tr>
{% set ns = namespace(balance=None) %} {% set ns = namespace(balance=None) %}