DrCr/drcr/webapp.py

68 lines
2.4 KiB
Python

# 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/>.
from flask import Flask, render_template
from .database import db_session, init_db
from .general_journal.models import GeneralJournalTransaction
from .models import Amount
from .statements.models import StatementLine, StatementLineTransaction
app = Flask(__name__)
@app.route('/')
def index():
return '<a href="/general-journal">General journal</a><br><a href="/statement-lines">Statement lines</a><br><a href="/general-ledger">General ledger</a><br><a href="/trial-balance">Trial balance</a>'
def all_transactions():
return (
GeneralJournalTransaction.query.all() +
StatementLineTransaction.query.all() +
[line.into_transaction() for line in StatementLine.query.all() if len(line.postings) == 0] # TODO: Filter in SQL
)
@app.route('/general-ledger')
def general_ledger():
return render_template('general_ledger.html', transactions=all_transactions())
@app.route('/trial-balance')
def trial_balance():
# Get balances
accounts = {}
for transaction in all_transactions():
for posting in transaction.postings:
if posting.account not in accounts:
accounts[posting.account] = Amount(0, '$')
accounts[posting.account].quantity += posting.quantity
total_dr = Amount(sum(v.quantity for v in accounts.values() if v.quantity > 0), '$')
total_cr = Amount(sum(v.quantity for v in accounts.values() if v.quantity < 0), '$')
return render_template('trial_balance.html', accounts=dict(sorted(accounts.items())), total_dr=total_dr, total_cr=total_cr)
from .general_journal import views
from .statements import views
@app.cli.command('initdb')
def initdb():
init_db()
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()