From 56f49f8784bc49d590579d033bbe9d2d9fb30e76 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 9 Sep 2024 13:44:33 -0400 Subject: [PATCH] membershipworks: Use more consistent and readable format for money columns --- membershipworks/tables.py | 52 ++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/membershipworks/tables.py b/membershipworks/tables.py index ad65dc7..8011be4 100644 --- a/membershipworks/tables.py +++ b/membershipworks/tables.py @@ -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") )