From bc52ccc349eaf44584d3ae7e83313e838924fbe7 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 30 Mar 2020 19:51:02 +1100 Subject: [PATCH] Better align values in "with commodity" reports --- ledger_pyreport/__init__.py | 23 +++++++++++ .../jinja2/transaction_commodity.html | 40 ++++++++----------- .../jinja2/transactions_commodity.html | 37 +++++++++-------- 3 files changed, 57 insertions(+), 43 deletions(-) diff --git a/ledger_pyreport/__init__.py b/ledger_pyreport/__init__.py index 799c66f..ea04df6 100644 --- a/ledger_pyreport/__init__.py +++ b/ledger_pyreport/__init__.py @@ -233,6 +233,29 @@ def filter_currency_positive(amt): else: return flask.Markup('{:,.2f} {}'.format(amt.amount, amt.currency.name).replace(',', ' ')) +@app.template_filter('bt') +def filter_currency_table_positive(amt, show_price, link=None): + result = [] + if amt.currency.is_prefix: + amt_str = filter_currency_positive(amt) + cur_str = '' + else: + amt_str = '{:,.2f}'.format(amt.amount).replace(',', ' ') + cur_str = amt.currency.name + + amt_full = amt.tostr(False) + + result.append('{}'.format(link, amt_full, amt_str) if link else '{}'.format(amt_full, amt_str)) + result.append('{}'.format(amt_full, cur_str)) + + if show_price: + if amt.currency.price: + result.append('{{{}}}'.format(amt_full, filter_currency_positive(amt.currency.price))) + else: + result.append('') + + return flask.Markup(''.join(result)) + # Debug views @app.route('/debug/noncash_transactions') diff --git a/ledger_pyreport/jinja2/transaction_commodity.html b/ledger_pyreport/jinja2/transaction_commodity.html index 613c71d..ac19a9e 100644 --- a/ledger_pyreport/jinja2/transaction_commodity.html +++ b/ledger_pyreport/jinja2/transaction_commodity.html @@ -41,10 +41,12 @@ {#Date#} Description Account - Dr - - Cr - + Dr {# Amount #} + {# Currency #} + {# Price #} + Cr + + {% for posting in transaction.postings %} @@ -53,15 +55,11 @@ {{ posting.comment }} {{ (posting.account.name|e).__str__().replace(':', ':')|safe }} {% if posting.amount >= 0 %} - {{ posting.amount|abs|bc }} - {% if posting.amount.currency.price %}{{ '{' + posting.amount.currency.price|bc + '}' }}{% endif %} - - + {{ posting.amount|bt(True) }} + {% else %} - - - {{ -posting.amount|abs|bc }} - {% if posting.amount.currency.price %}{{ '{' + posting.amount.currency.price|bc + '}' }}{% endif %} + + {{ -posting.amount|bt(True) }} {% endif %} {% endfor %} @@ -71,18 +69,14 @@ {% if loop.first %}Total{% endif %} {% if dr_amount %} - {{ dr_amount|abs|bc }} - {% if dr_amount.currency.price %}{{ '{' + dr_amount.currency.price|bc + '}' }}{% endif %} + {{ dr_amount|bt(True) }} {% else %} - - + {% endif %} {% if cr_amount %} - {{ -cr_amount|abs|bc }} - {% if cr_amount.currency.price %}{{ '{' + cr_amount.currency.price|bc + '}' }}{% endif %} + {{ -cr_amount|bt(True) }} {% else %} - - + {% endif %} {% endfor %} @@ -90,10 +84,8 @@ - {{ total_dr|abs|bc }} - - {{ -total_cr|abs|bc }} - + {{ total_dr|bt(True) }} + {{ -total_cr|bt(True) }} {% endblock %} diff --git a/ledger_pyreport/jinja2/transactions_commodity.html b/ledger_pyreport/jinja2/transactions_commodity.html index 2e4cad6..9dd25a1 100644 --- a/ledger_pyreport/jinja2/transactions_commodity.html +++ b/ledger_pyreport/jinja2/transactions_commodity.html @@ -29,25 +29,25 @@ Date Description - - Amount - {##} - Balance - + {# Dr/Cr #} + Amount + {# Currency #} + Balance + {# Currency #} + {# Price #} {% set ns = namespace(balance=None) %} - {% set prevlink = '/transactions_commodity?' + {'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': (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 %} {% for amount in opening_balance.amounts %} {% if loop.first %}{{ pstart.strftime('%Y-%m-%d') }}{% endif %} {% if loop.first %}Opening Balance{% endif %} - {##} - {{ amount|abs|bc }} - {% if amount.currency.price %}{{ '{' + amount.currency.price|bc + '}' }}{% endif %} + + {{ amount|bt(True, prevlink) }} {% if amount >= 0 %}Dr{% else %}Cr{% endif %} {% else %} @@ -56,8 +56,10 @@ Opening Balance + 0.00 + Dr {% endfor %} @@ -75,13 +77,11 @@ {% if loop.first %}{% if transaction.id %}{% endif %}{{ transaction.description }}{% if transaction.id %}{% endif %}{% endif %} {% if posting %} {% if posting.amount >= 0 %}Dr{% else %}Cr{% endif %} - {{ posting.amount|abs|bc }} - {#{% if posting.amount.currency.price %}{{ '{' + posting.amount.currency.price|bc + '}' }}{% endif %}#} + {{ posting.amount|abs|bt(False) }} {% else %} - + {% endif %} - {{ amount|abs|bc }} - {% if amount.currency.price %}{{ '{' + amount.currency.price|bc + '}' }}{% endif %} + {{ amount|abs|bt(True) }} {% if amount >= 0 %}Dr{% else %}Cr{% endif %} {% endfor %} @@ -93,10 +93,9 @@ {% if loop.first %}{{ date.strftime('%Y-%m-%d') }}{% endif %} {% if loop.first %}Closing Balance{% endif %} - {##} - {{ amount|abs|bc }} - {% if amount.currency.price %}{{ '{' + amount.currency.price|bc + '}' }}{% endif %} + + {{ amount|bt(True) }} {% if amount >= 0 %}Dr{% else %}Cr{% endif %} {% endfor %} @@ -106,9 +105,9 @@ - {% set closing_balance = closing_balance.exchange(report_currency, True) %} - {{ closing_balance|abs|bc }} + {% set closing_balance = closing_balance.exchange(report_currency, True) %} + {{ closing_balance|bt(True) }} {% if closing_balance >= 0 %}Dr{% else %}Cr{% endif %}