Limit income statement report to current financial year

This commit is contained in:
RunasSudo 2023-09-18 21:29:58 +10:00
parent a76ba56984
commit 80c28d2c6d
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
6 changed files with 51 additions and 22 deletions

View File

@ -18,9 +18,10 @@ from flask import render_template, url_for
from drcr.models import AccountConfiguration, Posting, Transaction, TrialBalancer from drcr.models import AccountConfiguration, Posting, Transaction, TrialBalancer
from drcr.database import db from drcr.database import db
from drcr.webapp import eofy_date
import drcr.plugins import drcr.plugins
from .reports import eofy_date, tax_summary_report from .reports import tax_summary_report
from . import views from . import views
def plugin_init(): def plugin_init():

View File

@ -16,8 +16,7 @@
from drcr.database import db from drcr.database import db
from drcr.models import Amount from drcr.models import Amount
from drcr.webapp import eofy_date
from .reports import eofy_date
class CGTAsset(Amount): class CGTAsset(Amount):
def __init__(self, quantity, commodity, account, acquisition_date): def __init__(self, quantity, commodity, account, acquisition_date):

View File

@ -18,16 +18,10 @@ from drcr import AMOUNT_DPS
from drcr.database import db from drcr.database import db
from drcr.models import AccountConfiguration, Amount, Metadata, Transaction, TrialBalancer from drcr.models import AccountConfiguration, Amount, Metadata, Transaction, TrialBalancer
from drcr.reports import Calculated, Report, Section, Spacer, Subtotal, entries_for_kind from drcr.reports import Calculated, Report, Section, Spacer, Subtotal, entries_for_kind
from drcr.webapp import eofy_date, sofy_date
from .tax_tables import base_tax, repayment_rates, fbt_grossup from .tax_tables import base_tax, repayment_rates, fbt_grossup
from datetime import datetime
def eofy_date():
"""Get the datetime for the end of the financial year"""
return datetime.strptime(Metadata.get('eofy_date'), '%Y-%m-%d')
def base_income_tax(year, taxable_income): def base_income_tax(year, taxable_income):
"""Get the amount of base income tax""" """Get the amount of base income tax"""
@ -66,7 +60,7 @@ def study_loan_repayment(year, taxable_income, rfb_grossedup):
def tax_summary_report(): def tax_summary_report():
# Get trial balance # Get trial balance
balancer = TrialBalancer() balancer = TrialBalancer()
balancer.apply_transactions(db.session.scalars(db.select(Transaction).where(Transaction.dt <= eofy_date()).options(db.selectinload(Transaction.postings))).all()) balancer.apply_transactions(db.session.scalars(db.select(Transaction).where((Transaction.dt >= sofy_date()) & (Transaction.dt <= eofy_date())).options(db.selectinload(Transaction.postings))).all())
accounts = dict(sorted(balancer.accounts.items())) accounts = dict(sorted(balancer.accounts.items()))

View File

@ -19,10 +19,10 @@ from flask import redirect, render_template, request, url_for
from drcr.models import AccountConfiguration, Amount, Posting, Transaction from drcr.models import AccountConfiguration, Amount, Posting, Transaction
from drcr.database import db from drcr.database import db
from drcr.plugins import render_plugin_template from drcr.plugins import render_plugin_template
from drcr.webapp import app from drcr.webapp import app, eofy_date
from .models import CGTAsset, CGTCostAdjustment from .models import CGTAsset, CGTCostAdjustment
from .reports import eofy_date, tax_summary_report from .reports import tax_summary_report
from datetime import datetime from datetime import datetime
from math import copysign from math import copysign

View File

@ -17,7 +17,9 @@
from flask import url_for from flask import url_for
from .models import AccountConfiguration, Amount, TrialBalancer from .models import AccountConfiguration, Amount, TrialBalancer
from .webapp import all_transactions from .webapp import all_transactions, eofy_date, sofy_date
from datetime import datetime, timedelta
class Report: class Report:
def __init__(self, title=None, entries=None): def __init__(self, title=None, entries=None):
@ -139,6 +141,9 @@ def balance_sheet_report():
account_configurations = AccountConfiguration.get_all_kinds() account_configurations = AccountConfiguration.get_all_kinds()
validate_accounts(accounts, account_configurations) validate_accounts(accounts, account_configurations)
day_before_sofy = sofy_date()
day_before_sofy -= timedelta(days=1)
report = Report( report = Report(
title='Balance sheet', title='Balance sheet',
entries=[ entries=[
@ -160,6 +165,10 @@ def balance_sheet_report():
lambda _: income_statement_report().by_id('net_surplus').amount, lambda _: income_statement_report().by_id('net_surplus').amount,
link=url_for('income_statement') link=url_for('income_statement')
), ),
Calculated(
'Accumulated surplus (deficit)',
lambda _: income_statement_report(start_date=datetime.min, end_date=day_before_sofy).by_id('net_surplus').amount
),
Subtotal('Total equity', bordered=True) Subtotal('Total equity', bordered=True)
] ]
), ),
@ -169,10 +178,15 @@ def balance_sheet_report():
return report return report
def income_statement_report(): 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()
# Get trial balance # Get trial balance
balancer = TrialBalancer() balancer = TrialBalancer()
balancer.apply_transactions(all_transactions()) balancer.apply_transactions(all_transactions(start_date=start_date, end_date=end_date))
accounts = dict(sorted(balancer.accounts.items())) accounts = dict(sorted(balancer.accounts.items()))

View File

@ -22,21 +22,29 @@ app.config.from_file('config.toml', load=toml.load)
from flask_sqlalchemy.record_queries import get_recorded_queries from flask_sqlalchemy.record_queries import get_recorded_queries
from .database import db from .database import db
from .models import Transaction from .models import Metadata, Transaction
from .plugins import init_plugins, transaction_providers from .plugins import init_plugins, transaction_providers
from .statements.models import StatementLine from .statements.models import StatementLine
from datetime import datetime, timedelta
import time import time
app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug
db.init_app(app) db.init_app(app)
def all_transactions(join_postings=True): def all_transactions(start_date=None, end_date=None, join_postings=True):
# All Transactions in database # All Transactions in database between start_date and end_date
query = db.select(Transaction)
if start_date and end_date:
query = query.where((Transaction.dt >= start_date) & (Transaction.dt <= end_date))
elif start_date:
query = query.where(Transaction.dt >= start_date)
elif end_date:
query = query.where(Transaction.dt <= end_date)
if join_postings: if join_postings:
transactions = db.session.scalars(db.select(Transaction).options(db.selectinload(Transaction.postings))).all() query = query.options(db.selectinload(Transaction.postings))
else:
transactions = db.session.scalars(db.select(Transaction)).all() transactions = db.session.scalars(query).all()
# Unreconciled StatementLines # Unreconciled StatementLines
transactions.extend(line.into_transaction() for line in StatementLine.query.filter(StatementLine.reconciliation == None)) transactions.extend(line.into_transaction() for line in StatementLine.query.filter(StatementLine.reconciliation == None))
@ -47,6 +55,19 @@ def all_transactions(join_postings=True):
return transactions return transactions
def eofy_date():
"""Get the datetime for the end of the financial year"""
return datetime.strptime(Metadata.get('eofy_date'), '%Y-%m-%d')
def sofy_date():
"""Get the datetime for the start of the financial year"""
dt = eofy_date()
dt = dt.replace(year=dt.year - 1)
dt += timedelta(days=1)
return dt
from . import views from . import views
from .journal import views from .journal import views
from .statements import views from .statements import views