87 lines
3.2 KiB
Python
87 lines
3.2 KiB
Python
# DrCr: Web-based double-entry bookkeeping framework
|
|
# Copyright (C) 2022–2023 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 redirect, render_template, request
|
|
|
|
from .database import db
|
|
from .models import AccountConfiguration, Amount, Balance, Posting, TrialBalancer
|
|
from .webapp import all_transactions, app
|
|
|
|
from itertools import groupby
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/chart-of-accounts')
|
|
def chart_of_accounts():
|
|
accounts = sorted(db.session.execute(db.select(Posting.account)).unique().scalars().all())
|
|
|
|
# Get existing AccountConfiguration's
|
|
account_configurations = db.session.execute(db.select(AccountConfiguration).order_by(AccountConfiguration.account)).scalars()
|
|
account_configurations = {v: list(g) for v, g in groupby(account_configurations, lambda c: c.account)}
|
|
|
|
# TODO: Handle orphans
|
|
return render_template('chart_of_accounts.html', accounts=accounts, account_configurations=account_configurations)
|
|
|
|
@app.route('/chart-of-accounts/add-kind', methods=['POST'])
|
|
def account_add_kind():
|
|
for account in request.form.getlist('sel-account'):
|
|
account_configuration = AccountConfiguration(account=account, kind=request.form['kind'])
|
|
db.session.add(account_configuration)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect('/chart-of-accounts')
|
|
|
|
@app.route('/general-ledger')
|
|
def general_ledger():
|
|
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():
|
|
balancer = TrialBalancer()
|
|
balancer.apply_transactions(all_transactions())
|
|
|
|
total_dr = Amount(sum(v.quantity for v in balancer.accounts.values() if v.quantity > 0), '$')
|
|
total_cr = Amount(sum(v.quantity for v in balancer.accounts.values() if v.quantity < 0), '$')
|
|
|
|
return render_template('trial_balance.html', accounts=dict(sorted(balancer.accounts.items())), total_dr=total_dr, total_cr=total_cr)
|
|
|
|
@app.route('/account-transactions')
|
|
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'],
|
|
running_total=Amount(0, '$'),
|
|
transactions=sorted(transactions, key=lambda t: t.dt)
|
|
)
|