2023-01-04 00:27:44 +11:00
|
|
|
# DrCr: Web-based double-entry bookkeeping framework
|
|
|
|
# Copyright (C) 2022–2023 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/>.
|
|
|
|
|
2023-01-07 19:01:05 +11:00
|
|
|
from flask import url_for
|
|
|
|
|
2023-01-04 00:27:44 +11:00
|
|
|
from .models import AccountConfiguration, Amount, TrialBalancer
|
2023-09-18 21:29:58 +10:00
|
|
|
from .webapp import all_transactions, eofy_date, sofy_date
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
2023-01-04 00:27:44 +11:00
|
|
|
|
|
|
|
class Report:
|
|
|
|
def __init__(self, title=None, entries=None):
|
|
|
|
self.title = title
|
|
|
|
self.entries = entries or []
|
|
|
|
|
|
|
|
def calculate(self):
|
|
|
|
"""Calculate all subtotals, etc."""
|
|
|
|
|
|
|
|
for entry in self.entries:
|
|
|
|
entry.calculate(self)
|
|
|
|
|
|
|
|
def by_id(self, id):
|
|
|
|
# TODO: Make more efficient?
|
|
|
|
|
|
|
|
for entry in self.entries:
|
|
|
|
if entry.id == id:
|
|
|
|
return entry
|
|
|
|
if isinstance(entry, Section):
|
|
|
|
result = entry.by_id(id)
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
class Section:
|
2023-09-19 20:04:11 +10:00
|
|
|
def __init__(self, title=None, entries=None, *, id=None, visible=True, auto_hide=False):
|
2023-01-04 00:27:44 +11:00
|
|
|
self.title = title
|
|
|
|
self.entries = entries or []
|
|
|
|
self.id = id
|
2023-09-19 20:04:11 +10:00
|
|
|
self.visible = visible
|
|
|
|
self.auto_hide = auto_hide
|
2023-01-04 00:27:44 +11:00
|
|
|
|
|
|
|
def calculate(self, parent):
|
|
|
|
for entry in self.entries:
|
|
|
|
entry.calculate(self)
|
2023-09-19 20:04:11 +10:00
|
|
|
|
|
|
|
if self.auto_hide and self.visible:
|
2024-08-16 22:40:44 +10:00
|
|
|
if not any(isinstance(e, Entry) and e.visible for e in self.entries):
|
|
|
|
# Auto hide if no visible entries (other than Subtotal)
|
2023-09-19 20:04:11 +10:00
|
|
|
self.visible = False
|
|
|
|
|
|
|
|
# Hide next Spacer
|
|
|
|
idx = parent.entries.index(self)
|
|
|
|
if idx + 1 < len(parent.entries):
|
|
|
|
if isinstance(parent.entries[idx + 1], Spacer):
|
|
|
|
parent.entries[idx + 1].visible = False
|
2023-01-04 00:27:44 +11:00
|
|
|
|
|
|
|
def by_id(self, id):
|
|
|
|
# TODO: Make more efficient?
|
|
|
|
|
|
|
|
for entry in self.entries:
|
|
|
|
if entry.id == id:
|
|
|
|
return entry
|
|
|
|
if isinstance(entry, Section):
|
|
|
|
result = entry.by_id(id)
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
class Entry:
|
2024-08-16 22:40:44 +10:00
|
|
|
def __init__(self, text=None, amount=None, *, id=None, visible=True, auto_hide=False, link=None, heading=False, bordered=False):
|
2023-01-04 00:27:44 +11:00
|
|
|
self.text = text
|
|
|
|
self.amount = amount
|
|
|
|
self.id = id
|
2023-09-19 20:04:11 +10:00
|
|
|
self.visible = visible
|
2024-08-16 22:40:44 +10:00
|
|
|
self.auto_hide = auto_hide
|
2023-01-04 10:55:00 +11:00
|
|
|
self.link = link
|
2023-01-04 00:27:44 +11:00
|
|
|
self.heading = heading
|
|
|
|
self.bordered = bordered
|
|
|
|
|
|
|
|
def calculate(self, parent):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Subtotal:
|
2023-01-04 18:03:08 +11:00
|
|
|
def __init__(self, text=None, *, id=None, visible=True, bordered=False, floor=0):
|
2023-01-04 00:27:44 +11:00
|
|
|
self.text = text
|
|
|
|
self.id = id
|
2023-01-04 18:03:08 +11:00
|
|
|
self.visible = visible
|
2023-01-04 00:27:44 +11:00
|
|
|
self.bordered = bordered
|
2023-01-04 18:03:08 +11:00
|
|
|
self.floor = floor
|
2023-01-04 00:27:44 +11:00
|
|
|
|
|
|
|
self.amount = None
|
|
|
|
|
|
|
|
def calculate(self, parent):
|
2023-01-04 18:03:08 +11:00
|
|
|
amount = sum(e.amount.quantity for e in parent.entries if isinstance(e, Entry))
|
|
|
|
if self.floor:
|
|
|
|
amount = (amount // self.floor) * self.floor
|
|
|
|
|
|
|
|
self.amount = Amount(amount, '$')
|
2023-01-04 00:27:44 +11:00
|
|
|
|
|
|
|
class Calculated(Entry):
|
2023-01-04 10:55:00 +11:00
|
|
|
def __init__(self, text=None, calc=None, **kwargs):
|
|
|
|
super().__init__(text=text, **kwargs)
|
2023-01-04 00:27:44 +11:00
|
|
|
self.calc = calc
|
|
|
|
|
|
|
|
self.amount = None
|
|
|
|
|
|
|
|
def calculate(self, parent):
|
|
|
|
self.amount = self.calc(parent)
|
2024-08-16 22:40:44 +10:00
|
|
|
|
|
|
|
if self.auto_hide and self.visible:
|
|
|
|
if self.amount.quantity == 0:
|
|
|
|
self.visible = False
|
2023-01-04 00:27:44 +11:00
|
|
|
|
|
|
|
class Spacer:
|
|
|
|
id = None
|
|
|
|
|
2023-09-19 20:04:11 +10:00
|
|
|
def __init__(self, *, visible=True):
|
|
|
|
self.visible = visible
|
|
|
|
|
2023-01-04 00:27:44 +11:00
|
|
|
def calculate(self, parent):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def validate_accounts(accounts, account_configurations):
|
|
|
|
for account in accounts:
|
|
|
|
n = sum(1 for c in account_configurations.get(account, []) if c in ('drcr.asset', 'drcr.liability', 'drcr.equity', 'drcr.income', 'drcr.expense'))
|
|
|
|
if n != 1:
|
|
|
|
raise Exception('Account "{}" mapped to {} account types (expected 1)'.format(account, n))
|
|
|
|
|
2023-01-04 18:03:08 +11:00
|
|
|
def entries_for_kind(account_configurations, accounts, kind, neg=False, floor=0):
|
|
|
|
entries = []
|
|
|
|
for account_name, amount in accounts.items():
|
|
|
|
if kind in account_configurations.get(account_name, []) and amount.quantity != 0:
|
|
|
|
if neg:
|
|
|
|
amount = -amount
|
|
|
|
if floor:
|
|
|
|
amount.quantity = (amount.quantity // floor) * floor
|
2023-01-07 19:01:05 +11:00
|
|
|
entries.append(Entry(text=account_name, amount=amount, link=url_for('account_transactions', account=account_name)))
|
2023-01-04 18:03:08 +11:00
|
|
|
return entries
|
2023-01-04 10:55:00 +11:00
|
|
|
|
2023-01-04 00:27:44 +11:00
|
|
|
def balance_sheet_report():
|
|
|
|
# Get trial balance
|
|
|
|
balancer = TrialBalancer()
|
|
|
|
balancer.apply_transactions(all_transactions())
|
|
|
|
|
|
|
|
accounts = dict(sorted(balancer.accounts.items()))
|
|
|
|
|
|
|
|
# Get account configurations
|
|
|
|
account_configurations = AccountConfiguration.get_all_kinds()
|
|
|
|
validate_accounts(accounts, account_configurations)
|
|
|
|
|
2023-09-18 21:29:58 +10:00
|
|
|
day_before_sofy = sofy_date()
|
|
|
|
day_before_sofy -= timedelta(days=1)
|
|
|
|
|
2023-01-04 00:27:44 +11:00
|
|
|
report = Report(
|
|
|
|
title='Balance sheet',
|
|
|
|
entries=[
|
|
|
|
Section(
|
|
|
|
title='Assets',
|
2023-01-04 10:55:00 +11:00
|
|
|
entries=entries_for_kind(account_configurations, accounts, 'drcr.asset') + [Subtotal('Total assets', bordered=True)]
|
2023-01-04 00:27:44 +11:00
|
|
|
),
|
|
|
|
Spacer(),
|
|
|
|
Section(
|
|
|
|
title='Liabilities',
|
2023-01-04 10:55:00 +11:00
|
|
|
entries=entries_for_kind(account_configurations, accounts, 'drcr.liability', True) + [Subtotal('Total liabilities', bordered=True)]
|
2023-01-04 00:27:44 +11:00
|
|
|
),
|
|
|
|
Spacer(),
|
|
|
|
Section(
|
|
|
|
title='Equity',
|
2023-01-04 10:55:00 +11:00
|
|
|
entries=entries_for_kind(account_configurations, accounts, 'drcr.equity', True) + [
|
2023-01-04 00:27:44 +11:00
|
|
|
Calculated(
|
|
|
|
'Current year surplus (deficit)',
|
2023-01-04 10:55:00 +11:00
|
|
|
lambda _: income_statement_report().by_id('net_surplus').amount,
|
2023-01-07 19:01:05 +11:00
|
|
|
link=url_for('income_statement')
|
2023-01-04 00:27:44 +11:00
|
|
|
),
|
2023-09-18 21:29:58 +10:00
|
|
|
Calculated(
|
|
|
|
'Accumulated surplus (deficit)',
|
|
|
|
lambda _: income_statement_report(start_date=datetime.min, end_date=day_before_sofy).by_id('net_surplus').amount
|
|
|
|
),
|
2023-01-04 00:27:44 +11:00
|
|
|
Subtotal('Total equity', bordered=True)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
report.calculate()
|
|
|
|
|
|
|
|
return report
|
|
|
|
|
2023-09-18 21:29:58 +10:00
|
|
|
def income_statement_report(start_date=None, end_date=None):
|
|
|
|
if start_date is None:
|
|
|
|
start_date = sofy_date()
|
|
|
|
if end_date is None:
|
|
|
|
end_date = eofy_date()
|
|
|
|
|
2023-01-04 00:27:44 +11:00
|
|
|
# Get trial balance
|
|
|
|
balancer = TrialBalancer()
|
2023-09-18 21:29:58 +10:00
|
|
|
balancer.apply_transactions(all_transactions(start_date=start_date, end_date=end_date))
|
2023-01-04 00:27:44 +11:00
|
|
|
|
|
|
|
accounts = dict(sorted(balancer.accounts.items()))
|
|
|
|
|
|
|
|
# Get account configurations
|
|
|
|
account_configurations = AccountConfiguration.get_all_kinds()
|
|
|
|
validate_accounts(accounts, account_configurations)
|
|
|
|
|
|
|
|
report = Report(
|
|
|
|
title='Income statement',
|
|
|
|
entries=[
|
|
|
|
Section(
|
|
|
|
title='Income',
|
2023-01-04 10:55:00 +11:00
|
|
|
entries=entries_for_kind(account_configurations, accounts, 'drcr.income', True) + [Subtotal('Total income', id='total_income', bordered=True)]
|
2023-01-04 00:27:44 +11:00
|
|
|
),
|
|
|
|
Spacer(),
|
|
|
|
Section(
|
|
|
|
title='Expenses',
|
2023-01-04 10:55:00 +11:00
|
|
|
entries=entries_for_kind(account_configurations, accounts, 'drcr.expense') + [Subtotal('Total expenses', id='total_expenses', bordered=True)]
|
2023-01-04 00:27:44 +11:00
|
|
|
),
|
|
|
|
Spacer(),
|
|
|
|
Calculated(
|
|
|
|
'Net surplus (deficit)',
|
|
|
|
lambda r: r.by_id('total_income').amount - r.by_id('total_expenses').amount,
|
|
|
|
id='net_surplus',
|
|
|
|
heading=True,
|
|
|
|
bordered=True
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
report.calculate()
|
|
|
|
|
|
|
|
return report
|