membershipworks: Use more consistent and readable format for money columns
All checks were successful
Test / test (push) Successful in 8m43s
Ruff / ruff (push) Successful in 3m18s

This commit is contained in:
Adam Goldsmith 2024-09-09 13:44:33 -04:00
parent 9198503572
commit 56f49f8784

View File

@ -21,6 +21,23 @@ class DurationColumn(tables.Column):
return value.total_seconds() / 60 / 60
class MoneyColumn(tables.columns.Column):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.attrs["cell"] = {"class": "text-end", **self.attrs.get("cell", {})}
def render(self, value) -> str:
return f"{super().render(value):.2f}"
def value(self, **kwargs):
return kwargs["value"]
class CurrencySymbolMoneyColumn(MoneyColumn):
def render(self, value) -> str:
return f"${super().render(value)}"
class EventTable(tables.Table):
title = tables.TemplateColumn(
template_code=(
@ -36,9 +53,9 @@ class EventTable(tables.Table):
duration = DurationColumn()
person_hours = DurationColumn()
meetings = tables.Column()
gross_revenue = tables.Column()
total_due_to_instructor = tables.Column()
net_revenue = tables.Column()
gross_revenue = MoneyColumn()
total_due_to_instructor = MoneyColumn()
net_revenue = MoneyColumn()
invoice__date_submitted = tables.DateColumn(verbose_name="Invoice Submitted")
invoice__date_paid = tables.DateColumn(verbose_name="Invoice Paid")
@ -103,9 +120,9 @@ class EventSummaryTable(tables.Table):
meetings__sum = tables.Column("Meetings")
duration__sum = DurationColumn("Class Hours")
person_hours__sum = DurationColumn("Person Hours")
gross_revenue__sum = tables.Column("Gross Revenue")
total_due_to_instructor__sum = tables.Column("Total Due to Instructor")
net_revenue__sum = tables.Column("Net Revenue")
gross_revenue__sum = MoneyColumn("Gross Revenue")
total_due_to_instructor__sum = MoneyColumn("Total Due to Instructor")
net_revenue__sum = MoneyColumn("Net Revenue")
class UserEventTable(EventTable):
@ -136,12 +153,7 @@ class CurrentAndUpcomingEventTable(EventTable):
sequence = ("title", "start", "next_meeting")
class InvoiceMoneyColumn(tables.columns.Column):
def render(self, value):
return f"${super().render(value):.2f}"
class InvoiceMoneyFooterColumn(InvoiceMoneyColumn):
class MoneyFooterColumn(MoneyColumn):
def render_footer(self, bound_column, table):
value = getattr(table.event, bound_column.accessor)
if value is not None:
@ -164,23 +176,23 @@ class InvoiceTable(tables.Table):
)
label = tables.Column("Ticket Type", footer="Subtotals")
list_price = InvoiceMoneyColumn("Ticket Price")
actual_price = InvoiceMoneyColumn(_math_header("Actual Price", "P"))
list_price = CurrencySymbolMoneyColumn("Ticket Price")
actual_price = CurrencySymbolMoneyColumn(_math_header("Actual Price", "P"))
quantity = tables.Column(
_math_header("Quantity", "Q"),
footer=lambda table: table.event.quantity,
)
amount = InvoiceMoneyFooterColumn(_math_header("Amount", "A=P*Q"))
materials = InvoiceMoneyFooterColumn(
amount = CurrencySymbolMoneyColumn(_math_header("Amount", "A=P*Q"))
materials = CurrencySymbolMoneyColumn(
_math_header("CMS Collected Materials Fee", "M=m*Q")
)
amount_without_materials = InvoiceMoneyFooterColumn(
amount_without_materials = CurrencySymbolMoneyColumn(
_math_header("Event Revenue Base", "B=A-M")
)
instructor_revenue = InvoiceMoneyFooterColumn(
_math_header("Instructor Percentage Revenue", "R=B*I")
instructor_revenue = CurrencySymbolMoneyColumn(
_math_header("Instructor Percentage Revenue", "R=B*I"),
)
instructor_amount = InvoiceMoneyFooterColumn(
instructor_amount = CurrencySymbolMoneyColumn(
_math_header("Amount Due to Instructor", "R+M")
)