DrCr/drcr/reports/views.py

79 lines
3.0 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 render_template
from ..config import BALANCE_SHEET_MAPPING, COA_MAPPING, INCOME_STATEMENT_MAPPING
from ..models import Amount, TrialBalancer
from ..webapp import all_transactions, app
def get_trial_balance():
# Get trial balance and validate COA
balancer = TrialBalancer()
balancer.apply_transactions(all_transactions())
# Classify accounts
for source_account, destination_account in COA_MAPPING.items():
balancer.transfer_balance(source_account, destination_account)
return balancer
# Validate COA
for account in balancer.accounts:
if account in BALANCE_SHEET_MAPPING['Current assets']:
continue
if account in BALANCE_SHEET_MAPPING['Non-current assets']:
continue
if account in BALANCE_SHEET_MAPPING['Current liabilities']:
continue
if account in BALANCE_SHEET_MAPPING['Non-current liabilities']:
continue
if account in INCOME_STATEMENT_MAPPING['Income']:
continue
if account in INCOME_STATEMENT_MAPPING['Expenses']:
continue
if account == 'Accumulated surplus (deficit)':
continue
raise Exception('Account "{}" is not mapped to a report'.format(account))
return balancer
#@app.route('/reports/mapped-trial-balance')
#def mapped_trial_balance():
# balancer = get_trial_balance()
#
# 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('/reports/balance-sheet')
def balance_sheet():
balancer = get_trial_balance()
# Transfer surplus to balance sheet
for account in INCOME_STATEMENT_MAPPING['Income'] + INCOME_STATEMENT_MAPPING['Expenses']:
balancer.transfer_balance(account, 'Current year surplus (deficit)')
return render_template('reports/balance_sheet.html', accounts=balancer.accounts, running_total=Amount(0, '$'), BALANCE_SHEET_MAPPING=BALANCE_SHEET_MAPPING)
@app.route('/reports/income-statement')
def income_statement():
balancer = get_trial_balance()
return render_template('reports/income_statement.html', accounts=balancer.accounts, running_total=Amount(0, '$'), INCOME_STATEMENT_MAPPING=INCOME_STATEMENT_MAPPING)