From f76e0d3dcb41687d4395bbc2d6367179883a37e4 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 24 Dec 2022 18:20:42 +1100 Subject: [PATCH] Implement adding general journal transactions --- drcr/general_journal/views.py | 35 +++++++- .../general_journal/general_journal.html | 4 + .../general_journal/general_journal_edit.html | 88 +++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 drcr/templates/general_journal/general_journal_edit.html diff --git a/drcr/general_journal/views.py b/drcr/general_journal/views.py index 3c3655a..5763866 100644 --- a/drcr/general_journal/views.py +++ b/drcr/general_journal/views.py @@ -20,13 +20,44 @@ from .. import AMOUNT_DPS from ..database import db_session from ..models import TrialBalancer from ..webapp import all_transactions, app -from .models import BalanceAssertion, GeneralJournalTransaction +from .models import BalanceAssertion, GeneralJournalPosting, GeneralJournalTransaction from datetime import datetime @app.route('/general-journal') def general_journal(): - return render_template('general_journal/general_journal.html', transactions=GeneralJournalTransaction.query.all()) + return render_template('general_journal/general_journal.html', transactions=sorted(GeneralJournalTransaction.query.all(), key=lambda t: t.dt)) + +@app.route('/general-journal/new', methods=['GET', 'POST']) +def general_journal_new(): + if request.method == 'GET': + return render_template('general_journal/general_journal_edit.html', transaction=None) + + # New transaction + transaction = GeneralJournalTransaction( + dt=datetime.strptime(request.form['dt'], '%Y-%m-%d'), + description=request.form['description'], + postings=[] + ) + + for account, sign, amount in zip(request.form.getlist('account'), request.form.getlist('sign'), request.form.getlist('amount')): + quantity = round(float(amount) * (10**AMOUNT_DPS)) + if sign == 'cr': + quantity = -quantity + + posting = GeneralJournalPosting( + account=account, + quantity=quantity, + commodity='$' # TODO: Commodities + ) + transaction.postings.append(posting) + + transaction.assert_valid() + + db_session.add(transaction) + db_session.commit() + + return redirect('/general-journal') @app.route('/balance-assertions') def balance_assertions(): diff --git a/drcr/templates/general_journal/general_journal.html b/drcr/templates/general_journal/general_journal.html index 31f1411..8d9d8c6 100644 --- a/drcr/templates/general_journal/general_journal.html +++ b/drcr/templates/general_journal/general_journal.html @@ -21,6 +21,10 @@ {% block content %}

General journal

+
+ New transaction +
+ diff --git a/drcr/templates/general_journal/general_journal_edit.html b/drcr/templates/general_journal/general_journal_edit.html new file mode 100644 index 0000000..4a119ef --- /dev/null +++ b/drcr/templates/general_journal/general_journal_edit.html @@ -0,0 +1,88 @@ +{# 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 %}{{ 'Edit' if transaction else 'New' }} general journal transaction{% endblock %} + +{% block content %} +

{{ 'Edit' if transaction else 'New' }} general journal transaction

+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DateDescriptionDrCr
+
+ + +
+
+
+
$
+ +
+
+
+ + +
+
+
+
$
+ +
+
+ +
+ +
+ +{% endblock %}