Crude cash basis implementation

This commit is contained in:
RunasSudo 2020-03-21 01:18:48 +11:00
parent f5f59a7098
commit c5a34d8559
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
10 changed files with 79 additions and 29 deletions

View File

@ -17,3 +17,5 @@ current_year_earnings: 'Equity:Current Year Earnings'
# Which asset accounts to consider in cash basis mode
cash_asset_accounts: ['Assets:Cash on Hand', 'Assets:Cash at Bank']
# Which account to charge non-cash transactions to in cash basis mode, when no other account is suitable
cash_other_income: 'Income:Other Income'

View File

@ -37,14 +37,17 @@ def trial():
date = datetime.strptime(flask.request.args['date'], '%Y-%m-%d')
pstart = datetime.strptime(flask.request.args['pstart'], '%Y-%m-%d')
compare = int(flask.request.args['compare'])
#cash = flask.request.args.get('cash', False)
cash = flask.request.args.get('cash', False)
report_currency = Currency(*config['report_currency'])
if compare == 0:
# Get trial balance
l = ledger.raw_transactions_at_date(date)
if cash:
l = accounting.cash_basis(l, report_currency)
trial_balance = accounting.trial_balance(l, date, pstart)
report_currency = Currency(*config['report_currency'])
trial_balance = accounting.add_unrealized_gains(trial_balance, report_currency)
total_dr = Amount(0, report_currency)
@ -64,8 +67,9 @@ def trial():
dates = [date.replace(year=date.year - i) for i in range(0, compare + 1)]
pstarts = [pstart.replace(year=pstart.year - i) for i in range(0, compare + 1)]
report_currency = Currency(*config['report_currency'])
l = ledger.raw_transactions_at_date(date)
if cash:
l = accounting.cash_basis(l, report_currency)
trial_balances = [accounting.add_unrealized_gains(accounting.trial_balance(l, d, p), report_currency) for d, p in zip(dates, pstarts)]
# Delete accounts with always zero balances
@ -74,20 +78,22 @@ def trial():
if all(t.get_balance(account) == 0 for t in trial_balances):
accounts.remove(account)
return flask.render_template('trial_multiple.html', trial_balances=trial_balances, accounts=accounts, report_currency=report_currency)
return flask.render_template('trial_multiple.html', trial_balances=trial_balances, accounts=accounts, report_currency=report_currency, cash=cash)
@app.route('/balance')
def balance():
date = datetime.strptime(flask.request.args['date'], '%Y-%m-%d')
pstart = datetime.strptime(flask.request.args['pstart'], '%Y-%m-%d')
compare = int(flask.request.args['compare'])
#cash = flask.request.args.get('cash', False)
cash = flask.request.args.get('cash', False)
dates = [date.replace(year=date.year - i) for i in range(0, compare + 1)]
pstarts = [pstart.replace(year=pstart.year - i) for i in range(0, compare + 1)]
report_currency = Currency(*config['report_currency'])
l = ledger.raw_transactions_at_date(date)
if cash:
l = accounting.cash_basis(l, report_currency)
balance_sheets = [accounting.balance_sheet(accounting.add_unrealized_gains(accounting.trial_balance(l, d, p), report_currency)) for d, p in zip(dates, pstarts)]
# Delete accounts with always zero balances
@ -96,7 +102,7 @@ def balance():
if all(b.get_balance(account) == 0 and b.get_total(account) == 0 for b in balance_sheets):
accounts.remove(account)
return flask.render_template('balance.html', ledger=l, balance_sheets=balance_sheets, accounts=accounts, config=config, report_currency=report_currency)
return flask.render_template('balance.html', ledger=l, balance_sheets=balance_sheets, accounts=accounts, config=config, report_currency=report_currency, cash=cash)
def describe_period(date_end, date_beg):
if date_end == (date_beg.replace(year=date_beg.year + 1) - timedelta(days=1)):
@ -111,13 +117,15 @@ def pandl():
date_beg = datetime.strptime(flask.request.args['date_beg'], '%Y-%m-%d')
date_end = datetime.strptime(flask.request.args['date_end'], '%Y-%m-%d')
compare = int(flask.request.args['compare'])
#cash = flask.request.args.get('cash', False)
cash = flask.request.args.get('cash', False)
dates_beg = [date_beg.replace(year=date_beg.year - i) for i in range(0, compare + 1)]
dates_end = [date_end.replace(year=date_end.year - i) for i in range(0, compare + 1)]
report_currency = Currency(*config['report_currency'])
l = ledger.raw_transactions_at_date(date_end)
if cash:
l = accounting.cash_basis(l, report_currency)
pandls = [accounting.trial_balance(l, de, db) for de, db in zip(dates_end, dates_beg)]
# Delete accounts with always zero balances
@ -126,19 +134,23 @@ def pandl():
if all(p.get_balance(account) == 0 and p.get_total(account) == 0 for p in pandls):
accounts.remove(account)
return flask.render_template('pandl.html', period=describe_period(date_end, date_beg), ledger=l, pandls=pandls, accounts=accounts, config=config, report_currency=report_currency)
return flask.render_template('pandl.html', period=describe_period(date_end, date_beg), ledger=l, pandls=pandls, accounts=accounts, config=config, report_currency=report_currency, cash=cash)
@app.route('/transactions')
def transactions():
date = datetime.strptime(flask.request.args['date'], '%Y-%m-%d')
pstart = datetime.strptime(flask.request.args['pstart'], '%Y-%m-%d')
account = flask.request.args.get('account', None)
cash = flask.request.args.get('cash', False)
report_currency = Currency(*config['report_currency'])
# General ledger
l = ledger.raw_transactions_at_date(date)
if cash:
l = accounting.cash_basis(l, report_currency)
# Unrealized gains
report_currency = Currency(*config['report_currency'])
l = accounting.add_unrealized_gains(accounting.trial_balance(l, date, pstart), report_currency).ledger
if not account:
@ -147,7 +159,7 @@ def transactions():
total_dr = sum((p.amount for t in transactions for p in t.postings if p.amount > 0), Balance()).exchange(report_currency, True)
total_cr = sum((p.amount for t in transactions for p in t.postings if p.amount < 0), Balance()).exchange(report_currency, True)
return flask.render_template('transactions.html', date=date, pstart=pstart, account=None, ledger=l, transactions=transactions, total_dr=total_dr, total_cr=total_cr, report_currency=report_currency)
return flask.render_template('transactions.html', date=date, pstart=pstart, account=None, ledger=l, transactions=transactions, total_dr=total_dr, total_cr=total_cr, report_currency=report_currency, cash=cash)
else:
account = l.get_account(account)
transactions = [t for t in l.transactions if t.date <= date and t.date >= pstart and any(p.account == account for p in t.postings)]
@ -155,7 +167,7 @@ def transactions():
opening_balance = accounting.trial_balance(l, pstart, pstart).get_balance(account).exchange(report_currency, True)
closing_balance = accounting.trial_balance(l, date, pstart).get_balance(account).exchange(report_currency, True)
return flask.render_template('transactions.html', date=date, pstart=pstart, period=describe_period(date, pstart), account=account, ledger=l, transactions=transactions, opening_balance=opening_balance, closing_balance=closing_balance, report_currency=report_currency, timedelta=timedelta)
return flask.render_template('transactions.html', date=date, pstart=pstart, period=describe_period(date, pstart), account=account, ledger=l, transactions=transactions, opening_balance=opening_balance, closing_balance=closing_balance, report_currency=report_currency, cash=cash, timedelta=timedelta)
@app.template_filter('a')
def filter_amount(amt):

View File

@ -19,6 +19,8 @@ from decimal import Decimal
from .model import *
# Generate a trial balance
# Perform closing of books based on specified dates
def trial_balance(ledger, date, pstart):
tb = TrialBalance(ledger, date, pstart)
@ -34,6 +36,7 @@ def trial_balance(ledger, date, pstart):
return tb
# Adjust (in place) a trial balance for unrealized gains
def add_unrealized_gains(tb, currency):
for account in list(tb.ledger.accounts.values()):
if not account.is_market:
@ -51,6 +54,8 @@ def add_unrealized_gains(tb, currency):
return trial_balance(tb.ledger, tb.date, tb.pstart)
# Adjust (in place) a trial balance to include a Current Year Earnings account
# Suitable for display on a balance sheet
def balance_sheet(tb):
# Calculate Profit/Loss
total_pandl = tb.get_total(tb.ledger.get_account(config['income_account'])) + tb.get_total(tb.ledger.get_account(config['expenses_account']))
@ -59,3 +64,28 @@ def balance_sheet(tb):
tb.balances[config['current_year_earnings']] = tb.get_balance(tb.ledger.get_account(config['current_year_earnings'])) + total_pandl
return tb
# Adjust (in place) a ledger to convert accounting to a cash basis
def cash_basis(ledger, currency):
for transaction in ledger.transactions:
non_cash_postings = [p for p in transaction.postings if not (p.account.is_cash or p.account.is_income or p.account.is_expense or p.account.is_equity)]
if non_cash_postings:
# We have liabilities or non-cash assets which need to be excluded
cash_postings = [p for p in transaction.postings if p.account.is_income or p.account.is_expense or p.account.is_equity]
cash_total = sum((p.amount for p in cash_postings), Balance()).exchange(currency, True).amount
if cash_postings:
for posting in non_cash_postings:
posting_amount = posting.amount.exchange(currency, True).amount
for posting_xfer in cash_postings:
posting_xfer_amount = posting_xfer.amount.exchange(currency, True).amount
transaction.postings.append(Posting(transaction, posting_xfer.account, Amount(posting_amount * posting_xfer_amount / cash_total, currency)))
transaction.postings.remove(posting)
else:
for posting in non_cash_postings:
posting.account = ledger.get_account(config['cash_other_income'])
return ledger

View File

@ -23,9 +23,9 @@
<td style="padding-left: calc(2px + {{ level }}em);">
{% if balance_sheets|length == 1 %}
{% if account.name == config['current_year_earnings'] %}
<a href="/pandl?{{ {'date_end': balance_sheets[0].date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0'}|urlencode }}">
<a href="/pandl?{{ {'date_end': balance_sheets[0].date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else ''}|urlencode }}">
{% else %}
<a href="/transactions?{{ {'date': balance_sheets[0].date.strftime('%Y-%m-%d'), 'pstart': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'account': account.name}|urlencode }}">
<a href="/transactions?{{ {'date': balance_sheets[0].date.strftime('%Y-%m-%d'), 'pstart': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">
{% endif %}
{{ account.bits[-1] }}
</a>
@ -38,9 +38,9 @@
<td>
{% if amount != 0 %}
{% if account.name == config['current_year_earnings'] %}
<a href="/pandl?{{ {'date_end': balance_sheet.date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0'}|urlencode }}">
<a href="/pandl?{{ {'date_end': balance_sheet.date.strftime('%Y-%m-%d'), 'date_beg': balance_sheets[0].pstart.strftime('%Y-%m-%d'), 'compare': '0', 'cash': 'on' if cash else ''}|urlencode }}">
{% else %}
<a href="/transactions?{{ {'date': balance_sheet.date.strftime('%Y-%m-%d'), 'pstart': balance_sheet.pstart.strftime('%Y-%m-%d'), 'account': account.name}|urlencode }}">
<a href="/transactions?{{ {'date': balance_sheet.date.strftime('%Y-%m-%d'), 'pstart': balance_sheet.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">
{% endif %}
{{ amount|a }}
</a>
@ -95,9 +95,11 @@
<tr><td colspan="2">&nbsp;</td></tr>
{# Liabilities #}
<tr><th class="h1" colspan="{{ balance_sheets|length + 1 }}">Liabilities</th></tr>
{{ do_accounts(ledger.get_account(config['liabilities_account']), 'Liabilities', True, False) }}
<tr><td colspan="2">&nbsp;</td></tr>
{% if not cash %}
<tr><th class="h1" colspan="{{ balance_sheets|length + 1 }}">Liabilities</th></tr>
{{ do_accounts(ledger.get_account(config['liabilities_account']), 'Liabilities', True, False) }}
<tr><td colspan="2">&nbsp;</td></tr>
{% endif %}
{# Equity #}
<tr><th class="h1" colspan="{{ balance_sheets|length + 1 }}">Equity</th></tr>

View File

@ -27,7 +27,7 @@
<label>Date: <input name="date" value="{{ date.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
<label>Period start: <input name="pstart" value="{{ pstart.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
<label>Compare <input name="compare" value="0" style="width: 2em;" oninput="txtc(this)"> periods</label>
{#<label><input name="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>#}
<label><input name="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>
</form></li>
<li><form action="{{ url_for('balance') }}">
@ -35,7 +35,7 @@
<label>Date: <input name="date" value="{{ date.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
<label>Period start: <input name="pstart" value="{{ pstart.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
<label>Compare <input name="compare" value="0" style="width: 2em;" oninput="txtc(this)"> periods</label>
{#<label><input name="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>#}
<label><input name="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>
</form></li>
<li><form action="{{ url_for('pandl') }}">
@ -43,14 +43,14 @@
<label>Begin date: <input name="date_beg" value="{{ pstart.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
<label>End date: <input name="date_end" value="{{ date.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
<label>Compare <input name="compare" value="0" style="width: 2em;" oninput="txtc(this)"> periods</label>
{#<label><input name="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>#}
<label><input name="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>
</form></li>
<li><form action="{{ url_for('transactions') }}">
<button type="submit">General ledger</button>
<label>Date: <input name="date" value="{{ date.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
<label>Period start: <input name="pstart" value="{{ pstart.strftime('%Y-%m-%d') }}" style="width: 6em;" oninput="txtc(this)"></label>
{#<label><input name="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>#}
<label><input name="cash" type="checkbox" oninput="chbc(this)"> Cash basis</label>
</form></li>
</ul>

View File

@ -22,14 +22,14 @@
<tr>
<td style="padding-left: calc(2px + {{ level }}em);">
{% if pandls|length == 1 %}
<a href="/transactions?{{ {'date': pandls[0].date.strftime('%Y-%m-%d'), 'pstart': pandls[0].pstart.strftime('%Y-%m-%d'), 'account': account.name}|urlencode }}">{{ account.bits[-1] }}</a>
<a href="/transactions?{{ {'date': pandls[0].date.strftime('%Y-%m-%d'), 'pstart': pandls[0].pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ account.bits[-1] }}</a>
{% else %}
{{ account.bits[-1] }}
{% endif %}
</td>
{% for pandl in pandls %}
{% set amount = (-pandl.get_balance(account) if invert else pandl.get_balance(account)).exchange(report_currency, True) %}
<td>{% if amount != 0 %}<a href="/transactions?{{ {'date': pandl.date.strftime('%Y-%m-%d'), 'pstart': pandl.pstart.strftime('%Y-%m-%d'), 'account': account.name}|urlencode }}">{{ amount|a }}</a>{% endif %}</td>
<td>{% if amount != 0 %}<a href="/transactions?{{ {'date': pandl.date.strftime('%Y-%m-%d'), 'pstart': pandl.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ amount|a }}</a>{% endif %}</td>
{% endfor %}
</tr>

View File

@ -43,12 +43,12 @@
{% if account %}
<tr class="total">
<td>{{ pstart.strftime('%Y-%m-%d') }}</td>
<td><a href="/transactions?{{ {'date': (pstart - timedelta(days=1)).strftime('%Y-%m-%d'), 'pstart': pstart.replace(year=pstart.year-1).strftime('%Y-%m-%d'), 'account': account.name}|urlencode }}">Opening Balance</a></td>
<td><a href="/transactions?{{ {'date': (pstart - timedelta(days=1)).strftime('%Y-%m-%d'), 'pstart': pstart.replace(year=pstart.year-1).strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">Opening Balance</a></td>
<td></td>
<td style="text-align: right;"></td>
<td style="text-align: right;"></td>
<td style="text-align: right;">
<a href="/transactions?{{ {'date': (pstart - timedelta(days=1)).strftime('%Y-%m-%d'), 'pstart': pstart.replace(year=pstart.year-1).strftime('%Y-%m-%d'), 'account': account.name}|urlencode }}">
<a href="/transactions?{{ {'date': (pstart - timedelta(days=1)).strftime('%Y-%m-%d'), 'pstart': pstart.replace(year=pstart.year-1).strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">
{% if opening_balance >= 0 %}
{{ opening_balance|b }} Dr
{% else %}
@ -66,7 +66,7 @@
<tr>
<td>{% if loop.first %}{{ transaction.date.strftime('%Y-%m-%d') }}{% endif %}</td>
<td>{% if loop.first %}{{ transaction.description }}{% endif %}</td>
<td><a href="/transactions?{{ {'date': date.strftime('%Y-%m-%d'), 'pstart': pstart.strftime('%Y-%m-%d'), 'account': posting.account.name}|urlencode }}">{{ (posting.account.name|e).__str__().replace(':', ':<wbr>')|safe }}</a></td>
<td><a href="/transactions?{{ {'date': date.strftime('%Y-%m-%d'), 'pstart': pstart.strftime('%Y-%m-%d'), 'account': posting.account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ (posting.account.name|e).__str__().replace(':', ':<wbr>')|safe }}</a></td>
{% if account %}
{# Reverse Dr/Cr so it's from the "perspective" of this account #}
{% set ns.balance = ns.balance - amount %}

View File

@ -33,7 +33,7 @@
{% for account in accounts %}
{# Display in "cost basis" as we have already accounted for unrealised gains #}
{% set balance = trial_balance.get_balance(account).exchange(report_currency, True) %}
{% set trn_url = "/transactions?" + {'date': trial_balance.date.strftime('%Y-%m-%d'), 'pstart': trial_balance.pstart.strftime('%Y-%m-%d'), 'account': account.name}|urlencode %}
{% set trn_url = "/transactions?" + {'date': trial_balance.date.strftime('%Y-%m-%d'), 'pstart': trial_balance.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode %}
{% if balance != 0 %}
<tr>
<td><a href="{{ trn_url }}">{{ account.name }}</a></td>

View File

@ -34,7 +34,7 @@
<td>{{ account.name }}</td>
{% for trial_balance in trial_balances %}
{% set balance = trial_balance.get_balance(account).exchange(report_currency, True) %}
<td>{% if balance != 0 %}{{ balance|a }}{% endif %}</td>
<td>{% if balance != 0 %}<a href="/transactions?{{ {'date': trial_balance.date.strftime('%Y-%m-%d'), 'pstart': trial_balance.pstart.strftime('%Y-%m-%d'), 'account': account.name, 'cash': 'on' if cash else ''}|urlencode }}">{{ balance|a }}</a>{% endif %}</td>
{% endfor %}
</tr>
{% endfor %}

View File

@ -123,6 +123,10 @@ class Account:
@property
def is_liability(self):
return self.matches(config['liabilities_account'])
@property
def is_cash(self):
# Is this a cash asset?
return any(self.matches(a) for a in config['cash_asset_accounts'])
@property
def is_cost(self):