From 3e6e1f18ed4f3573e34925deb447a371b00201d6 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 25 Dec 2022 17:31:31 +1100 Subject: [PATCH] Add commodity detail view to reports --- drcr/general_journal/views.py | 6 +- drcr/models.py | 21 ++++- .../general_journal/general_journal.html | 14 +++- drcr/templates/general_ledger.html | 17 +++- drcr/templates/transactions.html | 4 + .../transactions_commodity_detail.html | 78 +++++++++++++++++++ drcr/views.py | 28 +++++-- 7 files changed, 153 insertions(+), 15 deletions(-) create mode 100644 drcr/templates/transactions_commodity_detail.html diff --git a/drcr/general_journal/views.py b/drcr/general_journal/views.py index 84eead1..91f340c 100644 --- a/drcr/general_journal/views.py +++ b/drcr/general_journal/views.py @@ -26,7 +26,11 @@ from datetime import datetime @app.route('/general-journal') def general_journal(): - return render_template('general_journal/general_journal.html', transactions=sorted(GeneralJournalTransaction.query.all(), key=lambda t: t.dt)) + return render_template( + 'general_journal/general_journal.html', + commodity_detail=request.args.get('commodity-detail', '0') == '1', + transactions=sorted(GeneralJournalTransaction.query.all(), key=lambda t: t.dt) + ) @app.route('/general-journal/new', methods=['GET', 'POST']) def general_journal_new(): diff --git a/drcr/models.py b/drcr/models.py index 284a1eb..9bd1473 100644 --- a/drcr/models.py +++ b/drcr/models.py @@ -76,8 +76,8 @@ class Amount: def __neg__(self): return Amount(-self.quantity, self.commodity) - def format(self): - if self.commodity == '$': + def format(self, force_commodity=False): + if self.commodity == '$' and not force_commodity: return Markup('{:,.{dps}f}'.format(self.quantity / (10**AMOUNT_DPS), dps=AMOUNT_DPS).replace(',', ' ')) elif len(self.commodity) == 1: return Markup('{0}{1:,.{dps}f}'.format(self.commodity, self.quantity / (10**AMOUNT_DPS), dps=AMOUNT_DPS).replace(',', ' ')) @@ -111,6 +111,23 @@ class Amount: else: raise Exception('No cost base for commodity {}'.format(self.commodity)) +class Balance: + """A collection of Amount's""" + + def __init__(self): + self.amounts = [] + + def add(self, rhs): + amount = next((a for a in self.amounts if a.commodity == rhs.commodity), None) + if amount is None: + self.amounts.append(rhs) + else: + amount.quantity += rhs.quantity + + def clean(self): + """Remove zero amounts""" + self.amounts = [a for a in self.amounts if a.quantity != 0] + class TrialBalancer: """ Applies transactions to generate a trial balance diff --git a/drcr/templates/general_journal/general_journal.html b/drcr/templates/general_journal/general_journal.html index 291eeb2..1ab0308 100644 --- a/drcr/templates/general_journal/general_journal.html +++ b/drcr/templates/general_journal/general_journal.html @@ -23,6 +23,11 @@
New transaction + {% if commodity_detail %} + Hide commodity detail + {% else %} + Show commodity detail + {% endif %}
@@ -48,8 +53,13 @@ - - + {% if commodity_detail %} + + + {% else %} + + + {% endif %} {% endfor %} {% endfor %} diff --git a/drcr/templates/general_ledger.html b/drcr/templates/general_ledger.html index 11c7613..d4edcbe 100644 --- a/drcr/templates/general_ledger.html +++ b/drcr/templates/general_ledger.html @@ -21,6 +21,14 @@ {% block content %}

General ledger

+
+ {% if commodity_detail %} + Hide commodity detail + {% else %} + Show commodity detail + {% endif %} +
+
{{ posting.description or '' }} {{ 'Dr' if posting.quantity >= 0 else 'Cr' }} {{ posting.account }}{{ posting.amount().as_cost().format() if posting.quantity >= 0 else '' }}{{ (posting.amount()|abs).as_cost().format() if posting.quantity < 0 else '' }}{{ posting.amount().format(True) if posting.quantity >= 0 else '' }}{{ (posting.amount()|abs).format(True) if posting.quantity < 0 else '' }}{{ posting.amount().as_cost().format() if posting.quantity >= 0 else '' }}{{ (posting.amount()|abs).as_cost().format() if posting.quantity < 0 else '' }}
@@ -44,8 +52,13 @@ - - + {% if commodity_detail %} + + + {% else %} + + + {% endif %} {% endfor %} {% endfor %} diff --git a/drcr/templates/transactions.html b/drcr/templates/transactions.html index 99f514b..7201448 100644 --- a/drcr/templates/transactions.html +++ b/drcr/templates/transactions.html @@ -21,6 +21,10 @@ {% block content %}

Account transactions

+
+ Show commodity detail +
+
{{ posting.description or '' }} {{ 'Dr' if posting.quantity >= 0 else 'Cr' }} {{ posting.account }}{{ posting.amount().as_cost().format() if posting.quantity >= 0 else '' }}{{ (posting.amount()|abs).as_cost().format() if posting.quantity < 0 else '' }}{{ posting.amount().format(True) if posting.quantity >= 0 else '' }}{{ (posting.amount()|abs).format(True) if posting.quantity < 0 else '' }}{{ posting.amount().as_cost().format() if posting.quantity >= 0 else '' }}{{ (posting.amount()|abs).as_cost().format() if posting.quantity < 0 else '' }}
diff --git a/drcr/templates/transactions_commodity_detail.html b/drcr/templates/transactions_commodity_detail.html new file mode 100644 index 0000000..19220a2 --- /dev/null +++ b/drcr/templates/transactions_commodity_detail.html @@ -0,0 +1,78 @@ +{# DrCr: Web-based double-entry bookkeeping framework + Copyright (C) 2022 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.html' %} +{% block title %}Account transactions{% endblock %} + +{% block content %} +

Account transactions

+ + + +
+ + + + + + + + + + + + {% for transaction in transactions %} + + + + + + + + + {% for posting in transaction.postings if posting.account == account %} + {% set _ = running_total.add(posting.amount()) %} + {% endfor %} + {% for amount in running_total.amounts %} + {# FIXME: Assumes at most one posting per commodity #} + {% for posting in transaction.postings if posting.commodity == amount.commodity and posting.account == account %} + + + + + + + + + {% else %} + + + + + + + + + {% endfor %} + {% endfor %} + {% set _ = running_total.clean() %} + {% endfor %} + +
DateDescriptionAmountBalance
{{ transaction.dt.strftime('%Y-%m-%d') }}{{ transaction.description }}
{{ 'Dr' if posting.quantity >= 0 else 'Cr' }}{{ (posting.amount()|abs).format(True) }}{{ (amount|abs).format(True) }}{{ 'Dr' if amount.quantity >= 0 else 'Cr' }}
{{ (amount|abs).format(True) }}{{ 'Dr' if amount.quantity >= 0 else 'Cr' }}
+{% endblock %} diff --git a/drcr/views.py b/drcr/views.py index df48dac..a885c8b 100644 --- a/drcr/views.py +++ b/drcr/views.py @@ -16,7 +16,7 @@ from flask import render_template, request -from .models import Amount, TrialBalancer +from .models import Amount, Balance, TrialBalancer from .webapp import all_transactions, app @app.route('/') @@ -25,7 +25,11 @@ def index(): @app.route('/general-ledger') def general_ledger(): - return render_template('general_ledger.html', transactions=sorted(all_transactions(), key=lambda t: t.dt)) + return render_template( + 'general_ledger.html', + commodity_detail=request.args.get('commodity-detail', '0') == '1', + transactions=sorted(all_transactions(), key=lambda t: t.dt) + ) @app.route('/trial-balance') def trial_balance(): @@ -42,9 +46,17 @@ def account_transactions(): # FIXME: Filter in SQL transactions = [t for t in all_transactions() if any(p.account == request.args['account'] for p in t.postings)] - return render_template( - 'transactions.html', - account=request.args['account'], - running_total=Amount(0, '$'), - transactions=sorted(transactions, key=lambda t: t.dt) - ) + if request.args.get('commodity-detail', '0') == '1': + return render_template( + 'transactions_commodity_detail.html', + account=request.args['account'], + running_total=Balance(), + transactions=sorted(transactions, key=lambda t: t.dt) + ) + else: + return render_template( + 'transactions.html', + account=request.args['account'], + running_total=Amount(0, '$'), + transactions=sorted(transactions, key=lambda t: t.dt) + )