diff --git a/ledger_pyreport/__init__.py b/ledger_pyreport/__init__.py index 090497a..fda81ad 100644 --- a/ledger_pyreport/__init__.py +++ b/ledger_pyreport/__init__.py @@ -142,6 +142,7 @@ def transactions(): pstart = datetime.strptime(flask.request.args['pstart'], '%Y-%m-%d') account = flask.request.args.get('account', None) cash = flask.request.args.get('cash', False) + commodity = flask.request.args.get('commodity', False) report_currency = Currency(*config['report_currency']) @@ -154,13 +155,27 @@ def transactions(): l = accounting.add_unrealized_gains(accounting.trial_balance(l, date, pstart), report_currency).ledger if not account: + # General Ledger 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_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) + elif commodity: + # Account Transactions with commodity detail + 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).clean() + closing_balance = accounting.trial_balance(l, date, pstart).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) + + 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)] @@ -169,38 +184,11 @@ def transactions(): 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) - 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).clean() - closing_balance = accounting.trial_balance(l, date, pstart).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) - - 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) - @app.route('/transaction') def transaction(): tid = flask.request.args['tid'] cash = flask.request.args.get('cash', False) + commodity = flask.request.args.get('commodity', False) report_currency = Currency(*config['report_currency']) @@ -214,7 +202,10 @@ def transaction(): 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) - return flask.render_template('transaction.html', ledger=l, transaction=transaction, total_dr=total_dr, total_cr=total_cr, report_currency=report_currency, cash=cash) + 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) # Template filters diff --git a/ledger_pyreport/jinja2/debug_noncash_transactions.html b/ledger_pyreport/jinja2/debug_noncash_transactions.html index a5202d0..259bcfc 100644 --- a/ledger_pyreport/jinja2/debug_noncash_transactions.html +++ b/ledger_pyreport/jinja2/debug_noncash_transactions.html @@ -30,8 +30,8 @@ Date Description Account - Dr - Cr + Dr + Cr {% for transaction in transactions %} diff --git a/ledger_pyreport/jinja2/transaction.html b/ledger_pyreport/jinja2/transaction.html index 3ffcca8..ecd9d7a 100644 --- a/ledger_pyreport/jinja2/transaction.html +++ b/ledger_pyreport/jinja2/transaction.html @@ -20,6 +20,11 @@ {% block title %}{{ transaction.description }}{% endblock %} +{% block links %} + {{ super() }} + Show commodity detail +{% endblock %} + {% block report %}

Transaction

@@ -41,8 +46,8 @@ {#Date#} Description Account - Dr - Cr + Dr + Cr {% for posting in transaction.postings %} diff --git a/ledger_pyreport/jinja2/transaction_commodity.html b/ledger_pyreport/jinja2/transaction_commodity.html new file mode 100644 index 0000000..0aad43e --- /dev/null +++ b/ledger_pyreport/jinja2/transaction_commodity.html @@ -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 . +#} + +{% extends 'base_report.html' %} + +{% block title %}{{ transaction.description }}{% endblock %} + +{% block report %} +

Transaction

+ + + + + + + + + + + + +
DateCodeDescription
{{ transaction.date.strftime('%Y-%m-%d') }}{{ transaction.code }}{{ transaction.description }}
+ + + + {##} + + + + + + + + {% for posting in transaction.postings %} + {% set trn_url = '/transaction?' + {'tid': transaction.id, 'cash': 'on' if cash else ''}|urlencode %} + + + + + + + + {% endfor %} +
DateDescriptionAccountAmount
{{ posting.comment }}{{ (posting.account.name|e).__str__().replace(':', ':')|safe }}{% if posting.amount >= 0 %}Dr{% else %}Cr{% endif %}{{ posting.amount|abs|bc }}{% if posting.amount.currency.price %}{{ '{' + posting.amount.currency.price|bc + '}' }}{% endif %}
+{% endblock %} diff --git a/ledger_pyreport/jinja2/transactions.html b/ledger_pyreport/jinja2/transactions.html index dff7a0b..9b6d211 100644 --- a/ledger_pyreport/jinja2/transactions.html +++ b/ledger_pyreport/jinja2/transactions.html @@ -22,7 +22,7 @@ {% block links %} {{ super() }} - {% if account %}Show commodity detail{% endif %} + {% if account %}Show commodity detail{% endif %} {% endblock %} {% block report %} @@ -39,9 +39,9 @@ Date Description {% if account %}Related {% endif %}Account - Dr - Cr - {% if account %}Balance{% endif %} + Dr + Cr + {% if account %}Balance{% endif %} {% set ns = namespace(balance=None) %} diff --git a/ledger_pyreport/jinja2/transactions_commodity.html b/ledger_pyreport/jinja2/transactions_commodity.html index 06bbddf..a5cebdc 100644 --- a/ledger_pyreport/jinja2/transactions_commodity.html +++ b/ledger_pyreport/jinja2/transactions_commodity.html @@ -29,12 +29,12 @@ Date Description - - Amount - {##} - Balance - - + + Amount + {##} + Balance + + {% set ns = namespace(balance=None) %}