2022-12-24 12:31:45 +11:00
|
|
|
# DrCr: Web-based double-entry bookkeeping framework
|
2024-04-03 21:08:52 +11:00
|
|
|
# Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo)
|
2022-12-24 12:31:45 +11:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2023-01-07 19:01:05 +11:00
|
|
|
from flask import abort, redirect, render_template, request, url_for
|
2022-12-24 12:31:45 +11:00
|
|
|
|
2022-12-24 17:39:18 +11:00
|
|
|
from .. import AMOUNT_DPS
|
2023-01-02 16:59:12 +11:00
|
|
|
from ..database import db
|
2024-11-09 23:41:42 +11:00
|
|
|
from ..models import Amount, Posting, Transaction, TrialBalancer, queue_invalidate_running_balances, reporting_commodity
|
2024-04-06 01:31:31 +11:00
|
|
|
from ..webapp import all_accounts, all_transactions, app
|
2023-01-02 18:08:30 +11:00
|
|
|
from .models import BalanceAssertion
|
2023-01-02 20:07:03 +11:00
|
|
|
from ..statements.models import StatementLineReconciliation
|
2022-12-24 17:39:18 +11:00
|
|
|
|
|
|
|
from datetime import datetime
|
2022-12-24 12:31:45 +11:00
|
|
|
|
2023-01-02 18:08:30 +11:00
|
|
|
@app.route('/journal')
|
|
|
|
def journal():
|
2024-04-04 01:53:46 +11:00
|
|
|
transactions = db.session.scalars(db.select(Transaction).options(db.selectinload(Transaction.postings)).order_by(Transaction.dt.desc(), Transaction.id.desc())).all()
|
2023-01-05 01:15:39 +11:00
|
|
|
|
2022-12-25 17:31:31 +11:00
|
|
|
return render_template(
|
2023-01-02 18:08:30 +11:00
|
|
|
'journal/journal.html',
|
2023-01-07 19:01:05 +11:00
|
|
|
commodity_detail=request.args.get('commodity_detail', '0') == '1',
|
2023-01-05 01:15:39 +11:00
|
|
|
transactions=transactions
|
2022-12-25 17:31:31 +11:00
|
|
|
)
|
2022-12-24 18:20:42 +11:00
|
|
|
|
2023-01-02 18:08:30 +11:00
|
|
|
@app.route('/journal/new-transaction', methods=['GET', 'POST'])
|
|
|
|
def journal_new_transaction():
|
2022-12-24 18:20:42 +11:00
|
|
|
if request.method == 'GET':
|
2024-04-06 02:00:30 +11:00
|
|
|
return render_template('journal/journal_edit_transaction.html', transaction=None, all_accounts=all_accounts())
|
2022-12-24 18:20:42 +11:00
|
|
|
|
|
|
|
# New transaction
|
2023-01-02 18:08:30 +11:00
|
|
|
transaction = Transaction(
|
2022-12-24 18:20:42 +11:00
|
|
|
dt=datetime.strptime(request.form['dt'], '%Y-%m-%d'),
|
|
|
|
description=request.form['description'],
|
|
|
|
postings=[]
|
|
|
|
)
|
|
|
|
|
2022-12-24 19:45:34 +11:00
|
|
|
for account, sign, amount_str in zip(request.form.getlist('account'), request.form.getlist('sign'), request.form.getlist('amount')):
|
|
|
|
amount = Amount.parse(amount_str)
|
2022-12-24 18:20:42 +11:00
|
|
|
if sign == 'cr':
|
2022-12-24 19:45:34 +11:00
|
|
|
amount = -amount
|
2022-12-24 18:20:42 +11:00
|
|
|
|
2023-01-02 18:08:30 +11:00
|
|
|
posting = Posting(
|
2022-12-24 18:20:42 +11:00
|
|
|
account=account,
|
2022-12-24 19:45:34 +11:00
|
|
|
quantity=amount.quantity,
|
|
|
|
commodity=amount.commodity
|
2022-12-24 18:20:42 +11:00
|
|
|
)
|
|
|
|
transaction.postings.append(posting)
|
2024-11-09 23:41:42 +11:00
|
|
|
|
|
|
|
# Invalidate future running balances
|
|
|
|
queue_invalidate_running_balances(account, transaction.dt)
|
2022-12-24 18:20:42 +11:00
|
|
|
|
|
|
|
transaction.assert_valid()
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.add(transaction)
|
2024-11-09 23:41:42 +11:00
|
|
|
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.commit()
|
2022-12-24 18:20:42 +11:00
|
|
|
|
2023-05-28 13:49:42 +10:00
|
|
|
return redirect(request.form.get('referrer', '') or url_for('journal'))
|
2022-12-24 17:39:18 +11:00
|
|
|
|
2023-01-02 18:08:30 +11:00
|
|
|
@app.route('/journal/edit-transaction', methods=['GET', 'POST'])
|
|
|
|
def journal_edit_transaction():
|
|
|
|
transaction = db.session.get(Transaction, request.args['id'])
|
2022-12-24 20:41:33 +11:00
|
|
|
if not transaction:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
2024-04-06 02:00:30 +11:00
|
|
|
return render_template('journal/journal_edit_transaction.html', transaction=transaction, all_accounts=all_accounts())
|
2022-12-24 20:41:33 +11:00
|
|
|
|
2023-01-29 19:12:28 +11:00
|
|
|
if request.form.get('action', None) == 'delete':
|
|
|
|
# Delete transaction
|
|
|
|
db.session.delete(transaction)
|
2023-05-28 11:59:00 +10:00
|
|
|
|
|
|
|
# Delete reconciliations if required
|
|
|
|
for posting in transaction.postings:
|
|
|
|
for reconciliation in StatementLineReconciliation.query.filter(StatementLineReconciliation.posting == posting):
|
|
|
|
db.session.delete(reconciliation)
|
|
|
|
|
2023-01-29 19:12:28 +11:00
|
|
|
db.session.commit()
|
2023-05-28 13:02:12 +10:00
|
|
|
return redirect(request.form.get('referrer', '') or url_for('journal'))
|
2023-01-29 19:12:28 +11:00
|
|
|
|
2022-12-24 20:41:33 +11:00
|
|
|
# Edit transaction
|
|
|
|
transaction.dt = datetime.strptime(request.form['dt'], '%Y-%m-%d')
|
|
|
|
transaction.description = request.form['description']
|
2023-01-02 18:08:30 +11:00
|
|
|
|
2023-01-02 20:07:03 +11:00
|
|
|
new_postings = []
|
2022-12-24 20:41:33 +11:00
|
|
|
for account, sign, amount_str in zip(request.form.getlist('account'), request.form.getlist('sign'), request.form.getlist('amount')):
|
|
|
|
amount = Amount.parse(amount_str)
|
|
|
|
if sign == 'cr':
|
|
|
|
amount = -amount
|
|
|
|
|
2023-01-02 18:08:30 +11:00
|
|
|
posting = Posting(
|
2022-12-24 20:41:33 +11:00
|
|
|
account=account,
|
|
|
|
quantity=amount.quantity,
|
|
|
|
commodity=amount.commodity
|
|
|
|
)
|
2023-01-02 20:07:03 +11:00
|
|
|
new_postings.append(posting)
|
2024-11-09 23:41:42 +11:00
|
|
|
|
|
|
|
# Invalidate future running balances
|
|
|
|
queue_invalidate_running_balances(account, transaction.dt)
|
2023-01-02 20:07:03 +11:00
|
|
|
|
|
|
|
# Fix up reconciliations
|
|
|
|
for old_posting in transaction.postings:
|
|
|
|
for reconciliation in StatementLineReconciliation.query.filter(StatementLineReconciliation.posting == old_posting):
|
|
|
|
# See if there is a corresponding new posting
|
|
|
|
new_posting = next((p for p in new_postings if p.account == old_posting.account and p.quantity == old_posting.quantity and p.commodity == old_posting.commodity), None)
|
|
|
|
if new_posting is not None:
|
|
|
|
# Match up reconciliation
|
|
|
|
reconciliation.posting = new_posting
|
|
|
|
else:
|
|
|
|
# No matching reconciliation
|
|
|
|
db.session.delete(reconciliation)
|
2022-12-24 20:41:33 +11:00
|
|
|
|
2023-01-02 20:07:03 +11:00
|
|
|
transaction.postings = new_postings # This queues the old postings for deletion
|
2022-12-24 20:41:33 +11:00
|
|
|
transaction.assert_valid()
|
|
|
|
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.commit()
|
2022-12-24 20:41:33 +11:00
|
|
|
|
2023-05-28 13:02:12 +10:00
|
|
|
return redirect(request.form.get('referrer', '') or url_for('journal'))
|
2022-12-24 20:41:33 +11:00
|
|
|
|
2022-12-24 17:39:18 +11:00
|
|
|
@app.route('/balance-assertions')
|
|
|
|
def balance_assertions():
|
2024-04-04 01:53:46 +11:00
|
|
|
assertions = db.session.scalars(db.select(BalanceAssertion).order_by(BalanceAssertion.dt.desc(), BalanceAssertion.id.desc())).all()
|
2022-12-24 17:39:18 +11:00
|
|
|
|
|
|
|
# Check assertion status
|
|
|
|
transactions = all_transactions()
|
|
|
|
assertion_status = {}
|
|
|
|
for assertion in assertions:
|
|
|
|
# FIXME: This is very inefficient
|
|
|
|
balancer = TrialBalancer()
|
|
|
|
balancer.apply_transactions([t for t in transactions if t.dt <= assertion.dt])
|
|
|
|
|
|
|
|
# TODO: Commodities
|
2023-05-28 12:22:31 +10:00
|
|
|
if assertion.account in balancer.accounts and balancer.accounts[assertion.account].quantity == assertion.quantity:
|
2022-12-24 17:39:18 +11:00
|
|
|
assertion_status[assertion] = True
|
|
|
|
else:
|
|
|
|
assertion_status[assertion] = False
|
|
|
|
|
2023-01-02 18:08:30 +11:00
|
|
|
return render_template('journal/balance_assertions.html', assertions=assertions, assertion_status=assertion_status)
|
2022-12-24 17:39:18 +11:00
|
|
|
|
|
|
|
@app.route('/balance-assertions/new', methods=['GET', 'POST'])
|
|
|
|
def balance_assertions_new():
|
|
|
|
if request.method == 'GET':
|
2024-04-06 01:42:34 +11:00
|
|
|
return render_template('journal/balance_assertions_edit.html', assertion=None, all_accounts=all_accounts())
|
2022-12-24 17:39:18 +11:00
|
|
|
|
|
|
|
quantity = round(float(request.form['amount']) * (10**AMOUNT_DPS))
|
|
|
|
if request.form['sign'] == 'cr':
|
|
|
|
quantity = -quantity
|
|
|
|
|
|
|
|
# New balance assertion
|
|
|
|
assertion = BalanceAssertion(
|
|
|
|
dt=datetime.strptime(request.form['dt'], '%Y-%m-%d'),
|
|
|
|
description=request.form['description'],
|
|
|
|
account=request.form['account'],
|
|
|
|
quantity=quantity,
|
2024-11-09 18:14:25 +11:00
|
|
|
commodity=reporting_commodity()
|
2022-12-24 17:39:18 +11:00
|
|
|
)
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.add(assertion)
|
|
|
|
db.session.commit()
|
2022-12-24 17:39:18 +11:00
|
|
|
|
2023-01-07 19:01:05 +11:00
|
|
|
return redirect(url_for('balance_assertions'))
|
2022-12-24 17:39:18 +11:00
|
|
|
|
|
|
|
@app.route('/balance-assertions/edit', methods=['GET', 'POST'])
|
|
|
|
def balance_assertions_edit():
|
2023-01-02 16:59:12 +11:00
|
|
|
assertion = db.session.get(BalanceAssertion, request.args['id'])
|
2022-12-24 17:39:18 +11:00
|
|
|
if not assertion:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
2024-04-06 01:42:34 +11:00
|
|
|
return render_template('journal/balance_assertions_edit.html', assertion=assertion, all_accounts=all_accounts())
|
2022-12-24 17:39:18 +11:00
|
|
|
|
2024-04-06 00:39:36 +11:00
|
|
|
if request.form.get('action', None) == 'delete':
|
|
|
|
# Delete balance assertion
|
|
|
|
db.session.delete(assertion)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(request.form.get('referrer', '') or url_for('balance_assertions'))
|
|
|
|
|
2022-12-24 17:39:18 +11:00
|
|
|
quantity = round(float(request.form['amount']) * (10**AMOUNT_DPS))
|
|
|
|
if request.form['sign'] == 'cr':
|
|
|
|
quantity = -quantity
|
|
|
|
|
|
|
|
# Edit balance assertion
|
|
|
|
assertion.dt = datetime.strptime(request.form['dt'], '%Y-%m-%d')
|
|
|
|
assertion.description = request.form['description']
|
|
|
|
assertion.account = request.form['account']
|
|
|
|
assertion.quantity = quantity
|
|
|
|
|
2023-01-02 16:59:12 +11:00
|
|
|
db.session.commit()
|
2022-12-24 17:39:18 +11:00
|
|
|
|
2023-01-07 19:01:05 +11:00
|
|
|
return redirect(url_for('balance_assertions'))
|