From 80c28d2c6de69d81719654a7e52bea0548a0e8ce Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 18 Sep 2023 21:29:58 +1000 Subject: [PATCH] Limit income statement report to current financial year --- austax/__init__.py | 3 ++- austax/models.py | 3 +-- austax/reports.py | 10 ++-------- austax/views.py | 4 ++-- drcr/reports.py | 20 +++++++++++++++++--- drcr/webapp.py | 33 +++++++++++++++++++++++++++------ 6 files changed, 51 insertions(+), 22 deletions(-) diff --git a/austax/__init__.py b/austax/__init__.py index 814f608..25ec4a7 100644 --- a/austax/__init__.py +++ b/austax/__init__.py @@ -18,9 +18,10 @@ from flask import render_template, url_for from drcr.models import AccountConfiguration, Posting, Transaction, TrialBalancer from drcr.database import db +from drcr.webapp import eofy_date import drcr.plugins -from .reports import eofy_date, tax_summary_report +from .reports import tax_summary_report from . import views def plugin_init(): diff --git a/austax/models.py b/austax/models.py index 95c3be1..fbdc032 100644 --- a/austax/models.py +++ b/austax/models.py @@ -16,8 +16,7 @@ from drcr.database import db from drcr.models import Amount - -from .reports import eofy_date +from drcr.webapp import eofy_date class CGTAsset(Amount): def __init__(self, quantity, commodity, account, acquisition_date): diff --git a/austax/reports.py b/austax/reports.py index bcf038b..1606018 100644 --- a/austax/reports.py +++ b/austax/reports.py @@ -18,16 +18,10 @@ from drcr import AMOUNT_DPS from drcr.database import db from drcr.models import AccountConfiguration, Amount, Metadata, Transaction, TrialBalancer 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 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): """Get the amount of base income tax""" @@ -66,7 +60,7 @@ def study_loan_repayment(year, taxable_income, rfb_grossedup): def tax_summary_report(): # Get trial balance 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())) diff --git a/austax/views.py b/austax/views.py index 0ab8efc..ecc3474 100644 --- a/austax/views.py +++ b/austax/views.py @@ -19,10 +19,10 @@ from flask import redirect, render_template, request, url_for from drcr.models import AccountConfiguration, Amount, Posting, Transaction from drcr.database import db 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 .reports import eofy_date, tax_summary_report +from .reports import tax_summary_report from datetime import datetime from math import copysign diff --git a/drcr/reports.py b/drcr/reports.py index 300da65..80176ce 100644 --- a/drcr/reports.py +++ b/drcr/reports.py @@ -17,7 +17,9 @@ from flask import url_for 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: def __init__(self, title=None, entries=None): @@ -139,6 +141,9 @@ def balance_sheet_report(): account_configurations = AccountConfiguration.get_all_kinds() validate_accounts(accounts, account_configurations) + day_before_sofy = sofy_date() + day_before_sofy -= timedelta(days=1) + report = Report( title='Balance sheet', entries=[ @@ -160,6 +165,10 @@ def balance_sheet_report(): lambda _: income_statement_report().by_id('net_surplus').amount, 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) ] ), @@ -169,10 +178,15 @@ def balance_sheet_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 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())) diff --git a/drcr/webapp.py b/drcr/webapp.py index 7fb2255..c922400 100644 --- a/drcr/webapp.py +++ b/drcr/webapp.py @@ -22,21 +22,29 @@ app.config.from_file('config.toml', load=toml.load) from flask_sqlalchemy.record_queries import get_recorded_queries from .database import db -from .models import Transaction +from .models import Metadata, Transaction from .plugins import init_plugins, transaction_providers from .statements.models import StatementLine +from datetime import datetime, timedelta import time app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug db.init_app(app) -def all_transactions(join_postings=True): - # All Transactions in database +def all_transactions(start_date=None, end_date=None, join_postings=True): + # 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: - transactions = db.session.scalars(db.select(Transaction).options(db.selectinload(Transaction.postings))).all() - else: - transactions = db.session.scalars(db.select(Transaction)).all() + query = query.options(db.selectinload(Transaction.postings)) + + transactions = db.session.scalars(query).all() # Unreconciled StatementLines 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 +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 .journal import views from .statements import views