diff --git a/drcr/models.py b/drcr/models.py index 2494387..7c6390b 100644 --- a/drcr/models.py +++ b/drcr/models.py @@ -129,6 +129,10 @@ class TrialBalancer: # TODO: Keep a record of internal transactions? + if source_account == destination_account: + # Don't do anything in this case!! + return + if source_account not in self.accounts: return diff --git a/drcr/reports/views.py b/drcr/reports/views.py index 331cd5c..d42e2bc 100644 --- a/drcr/reports/views.py +++ b/drcr/reports/views.py @@ -16,13 +16,12 @@ from flask import render_template -from ..config import BALANCE_SHEET_MAPPING, COA_MAPPING +from ..config import BALANCE_SHEET_MAPPING, COA_MAPPING, INCOME_STATEMENT_MAPPING from ..models import Amount, TrialBalancer from ..webapp import all_transactions, app -@app.route('/reports/balance-sheet') -def balance_sheet(): - # Extract trial balance +def get_trial_balance(): + # Get trial balance and validate COA balancer = TrialBalancer() balancer.apply_transactions(all_transactions()) @@ -30,7 +29,50 @@ def balance_sheet(): for source_account, destination_account in COA_MAPPING.items(): balancer.transfer_balance(source_account, destination_account) - balancer.transfer_balance('Income', 'Current year surplus (deficit)') - balancer.transfer_balance('Expenses', 'Current year surplus (deficit)') + 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) diff --git a/drcr/templates/index.html b/drcr/templates/index.html index b831fcc..f475053 100644 --- a/drcr/templates/index.html +++ b/drcr/templates/index.html @@ -35,5 +35,6 @@

Advanced reports

{% endblock %} diff --git a/drcr/templates/reports/balance_sheet.html b/drcr/templates/reports/balance_sheet.html index 003dc28..f4a5e1f 100644 --- a/drcr/templates/reports/balance_sheet.html +++ b/drcr/templates/reports/balance_sheet.html @@ -29,7 +29,11 @@ {% macro acctrow(name, mul=1) %} - {{ name }} + {% if name == 'Current year surplus (deficit)' %} + {{ name }} + {% else %} + {{ name }} + {% endif %} {{ fmtbal(accounts[name], mul) }} {% set _ = running_total.__setattr__('quantity', running_total.quantity + accounts[name].quantity) %} @@ -42,7 +46,7 @@ - $ + $  diff --git a/drcr/templates/reports/income_statement.html b/drcr/templates/reports/income_statement.html new file mode 100644 index 0000000..76bf50a --- /dev/null +++ b/drcr/templates/reports/income_statement.html @@ -0,0 +1,86 @@ +{# 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 . +#} + +{% extends 'base.html' %} +{% block title %}Income statement{% endblock %} + +{% macro fmtbal(amount, mul=1) %} + {# FIXME: Honour AMOUNT_DPS #} + {% if amount.quantity * mul >= 0 %} + {{ '{:,.2f}'.format(amount.quantity|abs / 100) }}  + {% else %} + ({{ '{:,.2f}'.format(amount.quantity|abs / 100) }}) + {% endif %} +{% endmacro %} + +{% macro acctrow(name, mul=1) %} + + {{ name }} + {{ fmtbal(accounts[name], mul) }} + + {% set _ = running_total.__setattr__('quantity', running_total.quantity + accounts[name].quantity) %} +{% endmacro %} + +{% block content %} +

Income statement

+ + + + + + + + + + + + + {% set _ = running_total.__setattr__('quantity', 0) %} + {% for account in INCOME_STATEMENT_MAPPING['Income'] if account in accounts %} + {{ acctrow(account, -1) }} + {% endfor %} + + + + + {% set surplus = running_total.quantity %} + + + + + + + {% set _ = running_total.__setattr__('quantity', 0) %} + {% for account in INCOME_STATEMENT_MAPPING['Expenses'] if account in accounts %} + {{ acctrow(account) }} + {% endfor %} + + + + + {% set surplus = surplus + running_total.quantity %} + + + + + + {% set _ = running_total.__setattr__('quantity', surplus) %} + + + +
Income
Total income{{ fmtbal(running_total, -1) }}
 
Expenses
Total expenses{{ fmtbal(running_total) }}
 
Net surplus (deficit){{ fmtbal(running_total, -1) }}
+{% endblock %}