From d3cd7eebaa43662d3d3e287f21ee42175d73f654 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 30 Mar 2020 19:20:00 +1100 Subject: [PATCH] Display transaction detail w/ commodity in two-column Dr/Cr layout --- ledger_pyreport/__init__.py | 11 +++-- .../jinja2/transaction_commodity.html | 49 +++++++++++++++++-- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/ledger_pyreport/__init__.py b/ledger_pyreport/__init__.py index fda81ad..462b234 100644 --- a/ledger_pyreport/__init__.py +++ b/ledger_pyreport/__init__.py @@ -22,6 +22,7 @@ from .model import * from datetime import datetime, timedelta from decimal import Decimal import flask +import itertools app = flask.Flask(__name__, template_folder='jinja2') @@ -199,12 +200,14 @@ def transaction(): transaction = next((t for t in l.transactions if str(t.id) == tid)) - 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) - 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) + total_dr = sum((p.amount for p in transaction.postings if p.amount > 0), Balance()).clean() + total_cr = sum((p.amount for p in transaction.postings if p.amount < 0), Balance()).clean() + totals = itertools.zip_longest(total_dr.amounts, total_cr.amounts) + return flask.render_template('transaction_commodity.html', ledger=l, transaction=transaction, totals=totals, total_dr=total_dr.exchange(report_currency, True), total_cr=total_cr.exchange(report_currency, True), report_currency=report_currency, cash=cash) else: + 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) # Template filters diff --git a/ledger_pyreport/jinja2/transaction_commodity.html b/ledger_pyreport/jinja2/transaction_commodity.html index 0aad43e..613c71d 100644 --- a/ledger_pyreport/jinja2/transaction_commodity.html +++ b/ledger_pyreport/jinja2/transaction_commodity.html @@ -41,8 +41,9 @@ {#Date#} Description Account - - Amount + Dr + + Cr @@ -51,10 +52,48 @@ {{ 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 %} + {% if posting.amount >= 0 %} + {{ posting.amount|abs|bc }} + {% if posting.amount.currency.price %}{{ '{' + posting.amount.currency.price|bc + '}' }}{% endif %} + + + {% else %} + + + {{ -posting.amount|abs|bc }} + {% if posting.amount.currency.price %}{{ '{' + posting.amount.currency.price|bc + '}' }}{% endif %} + {% endif %} {% endfor %} + + {% for dr_amount, cr_amount in totals %} + + {% if loop.first %}Total{% endif %} + + {% if dr_amount %} + {{ dr_amount|abs|bc }} + {% if dr_amount.currency.price %}{{ '{' + dr_amount.currency.price|bc + '}' }}{% endif %} + {% else %} + + + {% endif %} + {% if cr_amount %} + {{ -cr_amount|abs|bc }} + {% if cr_amount.currency.price %}{{ '{' + cr_amount.currency.price|bc + '}' }}{% endif %} + {% else %} + + + {% endif %} + + {% endfor %} + + + + + {{ total_dr|abs|bc }} + + {{ -total_cr|abs|bc }} + + {% endblock %}