Implement adding general journal transactions

This commit is contained in:
RunasSudo 2022-12-24 18:20:42 +11:00
parent 43a886b3b5
commit f76e0d3dcb
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 125 additions and 2 deletions

View File

@ -20,13 +20,44 @@ from .. import AMOUNT_DPS
from ..database import db_session from ..database import db_session
from ..models import TrialBalancer from ..models import TrialBalancer
from ..webapp import all_transactions, app from ..webapp import all_transactions, app
from .models import BalanceAssertion, GeneralJournalTransaction from .models import BalanceAssertion, GeneralJournalPosting, GeneralJournalTransaction
from datetime import datetime from datetime import datetime
@app.route('/general-journal') @app.route('/general-journal')
def 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') @app.route('/balance-assertions')
def balance_assertions(): def balance_assertions():

View File

@ -21,6 +21,10 @@
{% block content %} {% block content %}
<h1 class="h2 my-4">General journal</h1> <h1 class="h2 my-4">General journal</h1>
<div class="mb-2">
<a href="/general-journal/new" class="btn btn-primary"><i class="bi bi-plus-lg"></i> New transaction</a>
</div>
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>

View File

@ -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 <https://www.gnu.org/licenses/>.
#}
{% extends 'base.html' %}
{% block title %}{{ 'Edit' if transaction else 'New' }} general journal transaction{% endblock %}
{% block content %}
<h1 class="h2 my-4">{{ 'Edit' if transaction else 'New' }} general journal transaction</h1>
<form method="POST">
<table class="table">
<thead>
<tr>
<th>Date</th>
<th colspan="2">Description</th>
<th>Dr</th>
<th>Cr</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="date" name="dt" class="form-control"></td>
<td colspan="2"><input type="text" name="description" value="" class="form-control"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<div class="input-group">
<select class="form-select" name="sign" style="flex-grow:0;min-width:5em">
<option value="dr" selected>Dr</option>
{#<option value="cr">Cr</option>#}
{# FIXME: Multiple postings, etc. #}
</select>
<input type="text" name="account" class="form-control">
</div>
</td>
<td>
<div class="input-group">
<div class="input-group-text">$</div>
<input type="number" name="amount" step="0.01" value="" class="form-control">
</div>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<div class="input-group">
<select class="form-select" name="sign" style="flex-grow:0;min-width:5em">
{#<option value="dr">Dr</option>#}
<option value="cr" selected>Cr</option>
</select>
<input type="text" name="account" class="form-control">
</div>
</td>
<td></td>
<td>
<div class="input-group">
<div class="input-group-text">$</div>
<input type="number" name="amount" step="0.01" value="" class="form-control">
</div>
</td>
</tr>
</tbody>
</table>
<div class="d-flex justify-content-end">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
{% endblock %}