Add commodity detail view to reports

This commit is contained in:
RunasSudo 2022-12-25 17:31:31 +11:00
parent 7b8f060308
commit 3e6e1f18ed
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
7 changed files with 153 additions and 15 deletions

View File

@ -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():

View File

@ -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

View File

@ -23,6 +23,11 @@
<div class="mb-2">
<a href="/general-journal/new" class="btn btn-primary"><i class="bi bi-plus-lg"></i> New transaction</a>
{% if commodity_detail %}
<a href="?commodity-detail=0" class="btn btn-outline-secondary">Hide commodity detail</a>
{% else %}
<a href="?commodity-detail=1" class="btn btn-outline-secondary">Show commodity detail</a>
{% endif %}
</div>
<table class="table">
@ -48,8 +53,13 @@
<td>{{ posting.description or '' }}</td>
<td class="text-end"><i>{{ 'Dr' if posting.quantity >= 0 else 'Cr' }}</i></td>
<td>{{ posting.account }}</td>
{% if commodity_detail %}
<td class="text-end">{{ posting.amount().format(True) if posting.quantity >= 0 else '' }}</td>
<td class="text-end">{{ (posting.amount()|abs).format(True) if posting.quantity < 0 else '' }}</td>
{% else %}
<td class="text-end">{{ posting.amount().as_cost().format() if posting.quantity >= 0 else '' }}</td>
<td class="text-end">{{ (posting.amount()|abs).as_cost().format() if posting.quantity < 0 else '' }}</td>
{% endif %}
</tr>
{% endfor %}
{% endfor %}

View File

@ -21,6 +21,14 @@
{% block content %}
<h1 class="h2 my-4">General ledger</h1>
<div class="mb-2">
{% if commodity_detail %}
<a href="?commodity-detail=0" class="btn btn-outline-secondary">Hide commodity detail</a>
{% else %}
<a href="?commodity-detail=1" class="btn btn-outline-secondary">Show commodity detail</a>
{% endif %}
</div>
<table class="table">
<thead>
<tr>
@ -44,8 +52,13 @@
<td>{{ posting.description or '' }}</td>
<td class="text-end"><i>{{ 'Dr' if posting.quantity >= 0 else 'Cr' }}</i></td>
<td>{{ posting.account }}</td>
{% if commodity_detail %}
<td class="text-end">{{ posting.amount().format(True) if posting.quantity >= 0 else '' }}</td>
<td class="text-end">{{ (posting.amount()|abs).format(True) if posting.quantity < 0 else '' }}</td>
{% else %}
<td class="text-end">{{ posting.amount().as_cost().format() if posting.quantity >= 0 else '' }}</td>
<td class="text-end">{{ (posting.amount()|abs).as_cost().format() if posting.quantity < 0 else '' }}</td>
{% endif %}
</tr>
{% endfor %}
{% endfor %}

View File

@ -21,6 +21,10 @@
{% block content %}
<h1 class="h2 my-4">Account transactions</h1>
<div class="mb-2">
<a href="?account={{ account }}&commodity-detail=1" class="btn btn-outline-secondary">Show commodity detail</a>
</div>
<table class="table">
<thead>
<tr>

View File

@ -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 <https://www.gnu.org/licenses/>.
#}
{% extends 'base.html' %}
{% block title %}Account transactions{% endblock %}
{% block content %}
<h1 class="h2 my-4">Account transactions</h1>
<div class="mb-2">
<a href="?account={{ account }}&commodity-detail=0" class="btn btn-outline-secondary">Hide commodity detail</a>
</div>
<table class="table table-sm table-borderless">
<thead>
<tr>
<th>Date</th>
<th>Description</th>
<th></th>
<th class="text-end">Amount</th>
<th class="text-end">Balance</th>
<th></th>
</tr>
</thead>
<tbody>
{% for transaction in transactions %}
<tr style="border-width:1px 0 0 0">
<td>{{ transaction.dt.strftime('%Y-%m-%d') }}</td>
<td>{{ transaction.description }}</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
{% 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 %}
<tr>
<td></td>
<td></td>
<td>{{ 'Dr' if posting.quantity >= 0 else 'Cr' }}</td>
<td class="text-end">{{ (posting.amount()|abs).format(True) }}</td>
<td class="text-end">{{ (amount|abs).format(True) }}</td>
<td>{{ 'Dr' if amount.quantity >= 0 else 'Cr' }}</td>
</tr>
{% else %}
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td class="text-end">{{ (amount|abs).format(True) }}</td>
<td>{{ 'Dr' if amount.quantity >= 0 else 'Cr' }}</td>
</tr>
{% endfor %}
{% endfor %}
{% set _ = running_total.clean() %}
{% endfor %}
</tbody>
</table>
{% endblock %}

View File

@ -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,6 +46,14 @@ 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)]
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'],