2022-12-24 15:18:53 +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 15:18:53 +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 redirect, render_template, request, url_for
|
2022-12-24 15:18:53 +11:00
|
|
|
|
2023-01-03 18:37:10 +11:00
|
|
|
from .database import db
|
|
|
|
from .models import AccountConfiguration, Amount, Balance, Posting, TrialBalancer
|
2023-01-07 14:07:58 +11:00
|
|
|
from .plugins import account_kinds, advanced_reports, data_sources
|
2023-01-04 00:27:44 +11:00
|
|
|
from .reports import balance_sheet_report, income_statement_report
|
2022-12-24 15:18:53 +11:00
|
|
|
from .webapp import all_transactions, app
|
|
|
|
|
2023-01-07 01:09:17 +11:00
|
|
|
from itertools import groupby
|
|
|
|
|
2022-12-24 15:18:53 +11:00
|
|
|
@app.route('/')
|
|
|
|
def index():
|
2023-01-07 14:07:58 +11:00
|
|
|
return render_template('index.html', data_sources=data_sources, advanced_reports=advanced_reports)
|
2022-12-24 15:18:53 +11:00
|
|
|
|
2023-01-03 18:37:10 +11:00
|
|
|
@app.route('/chart-of-accounts')
|
|
|
|
def chart_of_accounts():
|
2023-01-04 00:27:44 +11:00
|
|
|
#accounts = sorted(db.session.execute(db.select(Posting.account)).unique().scalars().all())
|
|
|
|
accounts = sorted(list(set(p.account for t in all_transactions() for p in t.postings)))
|
2023-01-03 18:37:10 +11:00
|
|
|
|
|
|
|
# Get existing AccountConfiguration's
|
2023-01-04 00:27:44 +11:00
|
|
|
account_configurations = AccountConfiguration.get_all()
|
2023-01-03 18:37:10 +11:00
|
|
|
|
2023-01-07 01:09:17 +11:00
|
|
|
# Preprocess account kinds
|
|
|
|
account_kinds_by_plugin = {v: list(g) for v, g in groupby(account_kinds, key=lambda k: k[0][:k[0].index('.')])}
|
|
|
|
account_kinds_map = {name: label for name, label in account_kinds}
|
|
|
|
|
2023-01-03 18:37:10 +11:00
|
|
|
# TODO: Handle orphans
|
2023-01-07 01:09:17 +11:00
|
|
|
return render_template(
|
|
|
|
'chart_of_accounts.html',
|
|
|
|
accounts=accounts,
|
|
|
|
account_configurations=account_configurations,
|
|
|
|
account_kinds_by_plugin=account_kinds_by_plugin,
|
|
|
|
account_kinds_map=account_kinds_map
|
|
|
|
)
|
2023-01-03 18:37:10 +11:00
|
|
|
|
|
|
|
@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()
|
|
|
|
|
2023-01-07 19:01:05 +11:00
|
|
|
return redirect(url_for('chart_of_accounts'))
|
2023-01-03 18:37:10 +11:00
|
|
|
|
2022-12-24 15:18:53 +11:00
|
|
|
@app.route('/general-ledger')
|
|
|
|
def general_ledger():
|
2022-12-25 17:31:31 +11:00
|
|
|
return render_template(
|
|
|
|
'general_ledger.html',
|
2023-01-07 19:01:05 +11:00
|
|
|
commodity_detail=request.args.get('commodity_detail', '0') == '1',
|
2024-04-03 21:08:52 +11:00
|
|
|
transactions=sorted(all_transactions(), key=lambda t: t.dt, reverse=True)
|
2022-12-25 17:31:31 +11:00
|
|
|
)
|
2022-12-24 15:18:53 +11:00
|
|
|
|
|
|
|
@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)
|
2022-12-24 16:43:08 +11:00
|
|
|
|
|
|
|
@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)]
|
|
|
|
|
2023-01-07 19:01:05 +11:00
|
|
|
if request.args.get('commodity_detail', '0') == '1':
|
2024-04-04 01:55:52 +11:00
|
|
|
# Pre-compute running totals
|
|
|
|
# At the level of individual transactions
|
|
|
|
running_totals = {}
|
|
|
|
running_total = Balance()
|
|
|
|
for transaction in sorted(transactions, key=lambda t: t.dt):
|
|
|
|
for posting in transaction.postings:
|
|
|
|
if posting.account == request.args['account']:
|
|
|
|
running_total.add(posting.amount())
|
|
|
|
|
|
|
|
running_totals[transaction] = running_total.clone()
|
|
|
|
running_total.clean()
|
|
|
|
|
2022-12-25 17:31:31 +11:00
|
|
|
return render_template(
|
|
|
|
'transactions_commodity_detail.html',
|
|
|
|
account=request.args['account'],
|
2024-04-04 01:55:52 +11:00
|
|
|
running_totals=running_totals,
|
2024-04-04 01:53:46 +11:00
|
|
|
transactions=reversed(sorted(transactions, key=lambda t: t.dt))
|
2022-12-25 17:31:31 +11:00
|
|
|
)
|
|
|
|
else:
|
2024-04-04 01:55:52 +11:00
|
|
|
# Pre-compute running totals
|
|
|
|
# There can be more than one posting per account per transaction, so track the running total at the level of individual postings
|
|
|
|
running_totals = {}
|
|
|
|
running_total = Amount(0, '$')
|
|
|
|
for transaction in sorted(transactions, key=lambda t: t.dt):
|
|
|
|
for posting in transaction.postings:
|
|
|
|
if posting.account == request.args['account']:
|
|
|
|
running_total += posting.amount().as_cost()
|
|
|
|
running_totals[posting] = running_total
|
|
|
|
|
2022-12-25 17:31:31 +11:00
|
|
|
return render_template(
|
|
|
|
'transactions.html',
|
|
|
|
account=request.args['account'],
|
2024-04-04 01:55:52 +11:00
|
|
|
running_totals=running_totals,
|
2024-04-04 01:53:46 +11:00
|
|
|
transactions=reversed(sorted(transactions, key=lambda t: t.dt))
|
2022-12-25 17:31:31 +11:00
|
|
|
)
|
2023-01-04 00:27:44 +11:00
|
|
|
|
|
|
|
@app.route('/balance-sheet')
|
|
|
|
def balance_sheet():
|
|
|
|
report = balance_sheet_report()
|
|
|
|
return render_template('report.html', report=report)
|
|
|
|
|
|
|
|
@app.route('/income-statement')
|
|
|
|
def income_statement():
|
|
|
|
report = income_statement_report()
|
|
|
|
return render_template('report.html', report=report)
|