Implement income statement

This commit is contained in:
RunasSudo 2022-12-25 13:00:34 +11:00
parent f97e7e10e0
commit 28376de4a7
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
5 changed files with 145 additions and 8 deletions

View File

@ -129,6 +129,10 @@ class TrialBalancer:
# TODO: Keep a record of internal transactions? # 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: if source_account not in self.accounts:
return return

View File

@ -16,13 +16,12 @@
from flask import render_template 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 ..models import Amount, TrialBalancer
from ..webapp import all_transactions, app from ..webapp import all_transactions, app
@app.route('/reports/balance-sheet') def get_trial_balance():
def balance_sheet(): # Get trial balance and validate COA
# Extract trial balance
balancer = TrialBalancer() balancer = TrialBalancer()
balancer.apply_transactions(all_transactions()) balancer.apply_transactions(all_transactions())
@ -30,7 +29,50 @@ def balance_sheet():
for source_account, destination_account in COA_MAPPING.items(): for source_account, destination_account in COA_MAPPING.items():
balancer.transfer_balance(source_account, destination_account) balancer.transfer_balance(source_account, destination_account)
balancer.transfer_balance('Income', 'Current year surplus (deficit)') return balancer
balancer.transfer_balance('Expenses', 'Current year surplus (deficit)')
# 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) 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)

View File

@ -35,5 +35,6 @@
<h1 class="h2 my-4">Advanced reports</h1> <h1 class="h2 my-4">Advanced reports</h1>
<ul> <ul>
<li><a href="/reports/balance-sheet">Balance sheet</a></li> <li><a href="/reports/balance-sheet">Balance sheet</a></li>
<li><a href="/reports/income-statement">Income statement</a></li>
</ul> </ul>
{% endblock %} {% endblock %}

View File

@ -29,7 +29,11 @@
{% macro acctrow(name, mul=1) %} {% macro acctrow(name, mul=1) %}
<tr> <tr>
{% if name == 'Current year surplus (deficit)' %}
<td><a href="/reports/income-statement">{{ name }}</a></td>
{% else %}
<td>{{ name }}</td> <td>{{ name }}</td>
{% endif %}
<td class="text-end">{{ fmtbal(accounts[name], mul) }}</td> <td class="text-end">{{ fmtbal(accounts[name], mul) }}</td>
</tr> </tr>
{% set _ = running_total.__setattr__('quantity', running_total.quantity + accounts[name].quantity) %} {% set _ = running_total.__setattr__('quantity', running_total.quantity + accounts[name].quantity) %}
@ -42,7 +46,7 @@
<thead> <thead>
<tr style="border-width:0 0 1px 0"> <tr style="border-width:0 0 1px 0">
<th></th> <th></th>
<th class="text-end">$</th> <th class="text-end">$&nbsp;</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>

View File

@ -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 <https://www.gnu.org/licenses/>.
#}
{% 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) }}&nbsp;
{% else %}
({{ '{:,.2f}'.format(amount.quantity|abs / 100) }})
{% endif %}
{% endmacro %}
{% macro acctrow(name, mul=1) %}
<tr>
<td>{{ name }}</td>
<td class="text-end">{{ fmtbal(accounts[name], mul) }}</td>
</tr>
{% set _ = running_total.__setattr__('quantity', running_total.quantity + accounts[name].quantity) %}
{% endmacro %}
{% block content %}
<h1 class="h2 mt-4">Income statement</h1>
<table class="table table-borderless table-sm">
<thead>
<tr style="border-width:0 0 1px 0">
<th></th>
<th class="text-end">$&nbsp;</th>
</tr>
</thead>
<tbody>
<tr>
<th colspan="2">Income</th>
</tr>
{% set _ = running_total.__setattr__('quantity', 0) %}
{% for account in INCOME_STATEMENT_MAPPING['Income'] if account in accounts %}
{{ acctrow(account, -1) }}
{% endfor %}
<tr>
<th>Total income</th>
<th class="text-end">{{ fmtbal(running_total, -1) }}</th>
</tr>
{% set surplus = running_total.quantity %}
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<th colspan="2">Expenses</th>
</tr>
{% set _ = running_total.__setattr__('quantity', 0) %}
{% for account in INCOME_STATEMENT_MAPPING['Expenses'] if account in accounts %}
{{ acctrow(account) }}
{% endfor %}
<tr>
<th>Total expenses</th>
<th class="text-end">{{ fmtbal(running_total) }}</th>
</tr>
{% set surplus = surplus + running_total.quantity %}
<tr><td colspan="2">&nbsp;</td></tr>
<tr style="border-width:1px 0">
<th>Net surplus (deficit)</th>
{% set _ = running_total.__setattr__('quantity', surplus) %}
<th class="text-end">{{ fmtbal(running_total, -1) }}</th>
</tr>
</tbody>
</table>
{% endblock %}