austax: Automatically hide non-applicable sections from tax summary report

This commit is contained in:
RunasSudo 2023-09-19 20:04:11 +10:00
parent ad5571e219
commit a8966cc19e
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 54 additions and 28 deletions

View File

@ -84,22 +84,26 @@ def tax_summary_report():
entries=[
Section(
title='Salary or wages (1)',
entries=entries_for_kind(account_configurations, accounts, 'austax.income1', neg=True, floor=100) + [Subtotal('Total item 1', id='income1')]
entries=entries_for_kind(account_configurations, accounts, 'austax.income1', neg=True, floor=100) + [Subtotal('Total item 1', id='income1')],
auto_hide=True
),
Spacer(),
Section(
title='Australian Government allowances and payments (5)',
entries=entries_for_kind(account_configurations, accounts, 'austax.income5', neg=True) + [Subtotal('Total item 5', id='income5', floor=100)]
entries=entries_for_kind(account_configurations, accounts, 'austax.income5', neg=True) + [Subtotal('Total item 5', id='income5', floor=100)],
auto_hide=True
),
Spacer(),
Section(
title='Gross interest (10)',
entries=entries_for_kind(account_configurations, accounts, 'austax.income10', neg=True) + [Subtotal('Total item 10', id='income10', floor=100)]
entries=entries_for_kind(account_configurations, accounts, 'austax.income10', neg=True) + [Subtotal('Total item 10', id='income10', floor=100)],
auto_hide=True
),
Spacer(),
Section(
title='Partnerships and trusts (13)',
entries=entries_for_kind(account_configurations, accounts, 'austax.income13', neg=True, floor=100) + [Subtotal('Total item 13', id='income13', floor=100)]
entries=entries_for_kind(account_configurations, accounts, 'austax.income13', neg=True, floor=100) + [Subtotal('Total item 13', id='income13', floor=100)],
auto_hide=True
),
Spacer(),
#Section(
@ -109,7 +113,8 @@ def tax_summary_report():
#Spacer(),
Section(
title='Foreign source income and foreign assets or property (20)',
entries=entries_for_kind(account_configurations, accounts, 'austax.income20', neg=True, floor=100) + [Subtotal('Total item 20', id='income20', floor=100)]
entries=entries_for_kind(account_configurations, accounts, 'austax.income20', neg=True, floor=100) + [Subtotal('Total item 20', id='income20', floor=100)],
auto_hide=True
),
Spacer(),
Calculated(
@ -122,22 +127,26 @@ def tax_summary_report():
Spacer(),
Section(
title='Work-related travel expenses (D2)',
entries=entries_for_kind(account_configurations, accounts, 'austax.d2') + [Subtotal('Total item D2', id='d2', floor=100)]
entries=entries_for_kind(account_configurations, accounts, 'austax.d2') + [Subtotal('Total item D2', id='d2', floor=100)],
auto_hide=True
),
Spacer(),
Section(
title='Work-related self-education expenses (D4)',
entries=entries_for_kind(account_configurations, accounts, 'austax.d4') + [Subtotal('Total item D4', id='d4', floor=100)]
entries=entries_for_kind(account_configurations, accounts, 'austax.d4') + [Subtotal('Total item D4', id='d4', floor=100)],
auto_hide=True
),
Spacer(),
Section(
title='Other work-related expenses (D5)',
entries=entries_for_kind(account_configurations, accounts, 'austax.d5') + [Subtotal('Total item D5', id='d5', floor=100)]
entries=entries_for_kind(account_configurations, accounts, 'austax.d5') + [Subtotal('Total item D5', id='d5', floor=100)],
auto_hide=True
),
Spacer(),
Section(
title='Gifts or donations (D9)',
entries=entries_for_kind(account_configurations, accounts, 'austax.d9') + [Subtotal('Total item D9', id='d9', floor=100)]
entries=entries_for_kind(account_configurations, accounts, 'austax.d9') + [Subtotal('Total item D9', id='d9', floor=100)],
auto_hide=True
),
Spacer(),
Calculated(

View File

@ -46,14 +46,27 @@ class Report:
return None
class Section:
def __init__(self, title=None, entries=None, *, id=None):
def __init__(self, title=None, entries=None, *, id=None, visible=True, auto_hide=False):
self.title = title
self.entries = entries or []
self.id = id
self.visible = visible
self.auto_hide = auto_hide
def calculate(self, parent):
for entry in self.entries:
entry.calculate(self)
if self.auto_hide and self.visible:
if not any(isinstance(e, Entry) for e in self.entries):
# Auto hide if no entries (other than Subtotal)
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
def by_id(self, id):
# TODO: Make more efficient?
@ -69,10 +82,11 @@ class Section:
return None
class Entry:
def __init__(self, text=None, amount=None, *, id=None, link=None, heading=False, bordered=False):
def __init__(self, text=None, amount=None, *, id=None, visible=True, link=None, heading=False, bordered=False):
self.text = text
self.amount = amount
self.id = id
self.visible = visible
self.link = link
self.heading = heading
self.bordered = bordered
@ -110,6 +124,9 @@ class Calculated(Entry):
class Spacer:
id = None
def __init__(self, *, visible=True):
self.visible = visible
def calculate(self, parent):
pass

View File

@ -31,28 +31,28 @@
{% endmacro %}
{% macro render_entry(entry) %}
{% if entry.__class__.__name__ == 'Section' %}
{{ render_section(entry) }}
{% elif entry.__class__.__name__ == 'Subtotal' %}
{% if entry.visible %}
{% if entry.visible %}
{% if entry.__class__.__name__ == 'Section' %}
{{ render_section(entry) }}
{% elif entry.__class__.__name__ == 'Subtotal' %}
<tr{% if entry.bordered %} style="border-width:1px 0"{% endif %}>
<th>{{ entry.text }}</th>
<th class="text-end">{{ entry.amount.format_accounting() }}</th>
</tr>
{% elif entry.__class__.__name__ == 'Spacer' %}
<tr><td colspan="2">&nbsp;</td></tr>
{% else %}
<tr{% if entry.bordered %} style="border-width:1px 0"{% endif %}>
<{{ 'th' if entry.heading else 'td' }}>
{% if entry.link %}
<a href="{{ entry.link }}">{{ entry.text }}</a>
{% else %}
{{ entry.text }}
{% endif %}
</{{ 'th' if entry.heading else 'td' }}>
<{{ 'th' if entry.heading else 'td' }} class="text-end">{{ entry.amount.format_accounting() }}</{{ 'th' if entry.heading else 'td' }}>
</tr>
{% endif %}
{% elif entry.__class__.__name__ == 'Spacer' %}
<tr><td colspan="2">&nbsp;</td></tr>
{% else %}
<tr{% if entry.bordered %} style="border-width:1px 0"{% endif %}>
<{{ 'th' if entry.heading else 'td' }}>
{% if entry.link %}
<a href="{{ entry.link }}">{{ entry.text }}</a>
{% else %}
{{ entry.text }}
{% endif %}
</{{ 'th' if entry.heading else 'td' }}>
<{{ 'th' if entry.heading else 'td' }} class="text-end">{{ entry.amount.format_accounting() }}</{{ 'th' if entry.heading else 'td' }}>
</tr>
{% endif %}
{% endmacro %}