DrCr/austax/__init__.py

87 lines
3.1 KiB
Python

# 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/>.
from flask import render_template
from drcr.models import AccountConfiguration, Posting, Transaction, TrialBalancer
from drcr.database import db
import drcr.plugins
from drcr.webapp import app
from .reports import tax_summary_report
from datetime import datetime
def plugin_init():
drcr.plugins.advanced_reports.append(('/tax/summary', 'Tax summary'))
drcr.plugins.account_kinds.append(('austax.income1', 'Salary or wages (1)'))
drcr.plugins.account_kinds.append(('austax.income5', 'Australian Government allowances and payments (5)'))
drcr.plugins.account_kinds.append(('austax.d4', 'Work-related self-education expenses (D4)'))
drcr.plugins.account_kinds.append(('austax.d5', 'Other work-related expenses (D5)'))
drcr.plugins.account_kinds.append(('austax.paygw', 'PAYG withheld amounts'))
drcr.plugins.transaction_providers.append(make_tax_transactions)
@app.route('/tax/summary')
def tax_summary():
report = tax_summary_report()
return render_template('report.html', report=report)
def make_tax_transactions():
report = tax_summary_report()
tax_amount = report.by_id('total_tax').amount
# Get EOFY date
dt = datetime.now().replace(month=6, day=30)
if dt < datetime.now():
dt = dt.replace(year=dt.year + 1)
# Estimated tax payable
transactions = [Transaction(
dt=dt,
description='Estimated income tax',
postings=[
Posting(account='Income Tax', quantity=tax_amount.quantity, commodity='$'),
Posting(account='Income Tax Control', quantity=-tax_amount.quantity, commodity='$')
]
)]
# Get trial balance
balancer = TrialBalancer()
balancer.apply_transactions(db.session.scalars(db.select(Transaction).options(db.selectinload(Transaction.postings))).all())
accounts = dict(sorted(balancer.accounts.items()))
# Get account configurations
account_configurations = AccountConfiguration.get_all_kinds()
# PAYG withholding
for account_name, kinds in account_configurations.items():
if 'austax.paygw' in kinds:
if accounts[account_name].quantity != 0:
# Transfer balance to Income Tax Control
transactions.append(Transaction(
dt=dt,
description='PAYG withheld amounts',
postings=[
Posting(account='Income Tax Control', quantity=accounts[account_name].quantity, commodity='$'),
Posting(account=account_name, quantity=-accounts[account_name].quantity, commodity='$')
]
))
return transactions