Merge remote-tracking branch 'origin/main' into feature/member-overview-groups
This commit is contained in:
commit
6831ba046f
48 changed files with 3516 additions and 182 deletions
|
|
@ -151,9 +151,17 @@ defmodule MvWeb.CoreComponents do
|
|||
## Examples
|
||||
|
||||
<.dropdown_menu items={@items} open={@open} phx_target={@myself} />
|
||||
|
||||
When using custom content (e.g., forms), use the inner_block slot:
|
||||
|
||||
<.dropdown_menu button_label="Export" icon="hero-arrow-down-tray" open={@open} phx_target={@myself}>
|
||||
<li role="none">
|
||||
<form>...</form>
|
||||
</li>
|
||||
</.dropdown_menu>
|
||||
"""
|
||||
attr :id, :string, default: "dropdown-menu"
|
||||
attr :items, :list, required: true, doc: "List of %{label: string, value: any} maps"
|
||||
attr :items, :list, default: [], doc: "List of %{label: string, value: any} maps"
|
||||
attr :button_label, :string, default: "Dropdown"
|
||||
attr :icon, :string, default: nil
|
||||
attr :checkboxes, :boolean, default: false
|
||||
|
|
@ -161,8 +169,30 @@ defmodule MvWeb.CoreComponents do
|
|||
attr :open, :boolean, default: false, doc: "Whether the dropdown is open"
|
||||
attr :show_select_buttons, :boolean, default: false, doc: "Show select all/none buttons"
|
||||
attr :phx_target, :any, required: true, doc: "The LiveView/LiveComponent target for events"
|
||||
attr :menu_class, :string, default: nil, doc: "Additional CSS classes for the menu"
|
||||
attr :menu_width, :string, default: "w-64", doc: "Width class for the menu (default: w-64)"
|
||||
|
||||
attr :button_class, :string,
|
||||
default: nil,
|
||||
doc: "Additional CSS classes for the button (e.g., btn-secondary)"
|
||||
|
||||
attr :menu_align, :string,
|
||||
default: "right",
|
||||
doc: "Menu alignment: 'left' or 'right' (default: right)"
|
||||
|
||||
attr :testid, :string, default: "dropdown-menu", doc: "data-testid for the dropdown container"
|
||||
attr :button_testid, :string, default: "dropdown-button", doc: "data-testid for the button"
|
||||
|
||||
attr :menu_testid, :string,
|
||||
default: nil,
|
||||
doc: "data-testid for the menu (defaults to testid + '-menu')"
|
||||
|
||||
slot :inner_block, doc: "Custom content for the dropdown menu (e.g., forms)"
|
||||
|
||||
def dropdown_menu(assigns) do
|
||||
menu_testid = assigns.menu_testid || "#{assigns.testid}-menu"
|
||||
assigns = assign(assigns, :menu_testid, menu_testid)
|
||||
|
||||
~H"""
|
||||
<div
|
||||
class="relative"
|
||||
|
|
@ -170,7 +200,7 @@ defmodule MvWeb.CoreComponents do
|
|||
phx-target={@phx_target}
|
||||
phx-window-keydown="close_dropdown"
|
||||
phx-key="Escape"
|
||||
data-testid="dropdown-menu"
|
||||
data-testid={@testid}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
|
|
@ -180,10 +210,17 @@ defmodule MvWeb.CoreComponents do
|
|||
aria-expanded={@open}
|
||||
aria-controls={@id}
|
||||
aria-label={@button_label}
|
||||
class="btn focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-base-content/20"
|
||||
class={[
|
||||
"btn",
|
||||
"focus:outline-none",
|
||||
"focus-visible:ring-2",
|
||||
"focus-visible:ring-offset-2",
|
||||
"focus-visible:ring-base-content/20",
|
||||
@button_class
|
||||
]}
|
||||
phx-click="toggle_dropdown"
|
||||
phx-target={@phx_target}
|
||||
data-testid="dropdown-button"
|
||||
data-testid={@button_testid}
|
||||
>
|
||||
<%= if @icon do %>
|
||||
<.icon name={@icon} />
|
||||
|
|
@ -195,69 +232,79 @@ defmodule MvWeb.CoreComponents do
|
|||
:if={@open}
|
||||
id={@id}
|
||||
role="menu"
|
||||
class="absolute right-0 mt-2 bg-base-100 z-[100] p-2 shadow-lg rounded-box w-64 max-h-96 overflow-y-auto border border-base-300"
|
||||
class={[
|
||||
"absolute mt-2 bg-base-100 z-[100] p-2 shadow-lg rounded-box max-h-96 overflow-y-auto border border-base-300",
|
||||
if(@menu_align == "left", do: "left-0", else: "right-0"),
|
||||
@menu_width,
|
||||
@menu_class
|
||||
]}
|
||||
tabindex="0"
|
||||
phx-target={@phx_target}
|
||||
data-testid={@menu_testid}
|
||||
>
|
||||
<li :if={@show_select_buttons} role="none">
|
||||
<div class="flex justify-between items-center mb-2 px-2">
|
||||
<span class="font-semibold">{gettext("Options")}</span>
|
||||
<div class="flex gap-1">
|
||||
<button
|
||||
type="button"
|
||||
role="menuitem"
|
||||
aria-label={gettext("Select all")}
|
||||
phx-click="select_all"
|
||||
phx-target={@phx_target}
|
||||
class="btn btn-xs btn-ghost"
|
||||
>
|
||||
{gettext("All")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
role="menuitem"
|
||||
aria-label={gettext("Select none")}
|
||||
phx-click="select_none"
|
||||
phx-target={@phx_target}
|
||||
class="btn btn-xs btn-ghost"
|
||||
>
|
||||
{gettext("None")}
|
||||
</button>
|
||||
<%= if assigns.inner_block != [] do %>
|
||||
{render_slot(@inner_block)}
|
||||
<% else %>
|
||||
<li :if={@show_select_buttons} role="none">
|
||||
<div class="flex justify-between items-center mb-2 px-2">
|
||||
<span class="font-semibold">{gettext("Options")}</span>
|
||||
<div class="flex gap-1">
|
||||
<button
|
||||
type="button"
|
||||
role="menuitem"
|
||||
aria-label={gettext("Select all")}
|
||||
phx-click="select_all"
|
||||
phx-target={@phx_target}
|
||||
class="btn btn-xs btn-ghost"
|
||||
>
|
||||
{gettext("All")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
role="menuitem"
|
||||
aria-label={gettext("Select none")}
|
||||
phx-click="select_none"
|
||||
phx-target={@phx_target}
|
||||
class="btn btn-xs btn-ghost"
|
||||
>
|
||||
{gettext("None")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
|
||||
<li :if={@show_select_buttons} role="separator" class="divider my-1"></li>
|
||||
|
||||
<%= for item <- @items do %>
|
||||
<li role="none">
|
||||
<button
|
||||
type="button"
|
||||
role={if @checkboxes, do: "menuitemcheckbox", else: "menuitem"}
|
||||
aria-label={item.label}
|
||||
aria-checked={
|
||||
if @checkboxes, do: to_string(Map.get(@selected, item.value, true)), else: nil
|
||||
}
|
||||
tabindex="0"
|
||||
class="flex items-center gap-2 px-2 py-1 rounded cursor-pointer hover:bg-base-200 w-full text-left focus:outline-none focus-visible:ring-2 focus-visible:ring-base-content/20 focus-visible:ring-inset"
|
||||
phx-click="select_item"
|
||||
phx-keydown="select_item"
|
||||
phx-key="Enter"
|
||||
phx-value-item={item.value}
|
||||
phx-target={@phx_target}
|
||||
>
|
||||
<%= if @checkboxes do %>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={Map.get(@selected, item.value, true)}
|
||||
class="checkbox checkbox-sm checkbox-primary pointer-events-none"
|
||||
tabindex="-1"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<% end %>
|
||||
<span>{item.label}</span>
|
||||
</button>
|
||||
</li>
|
||||
|
||||
<li :if={@show_select_buttons} role="separator" class="divider my-1"></li>
|
||||
|
||||
<%= for item <- @items do %>
|
||||
<li role="none">
|
||||
<button
|
||||
type="button"
|
||||
role={if @checkboxes, do: "menuitemcheckbox", else: "menuitem"}
|
||||
aria-label={item.label}
|
||||
aria-checked={
|
||||
if @checkboxes, do: to_string(Map.get(@selected, item.value, true)), else: nil
|
||||
}
|
||||
tabindex="0"
|
||||
class="flex items-center gap-2 px-2 py-1 rounded cursor-pointer hover:bg-base-200 w-full text-left focus:outline-none focus-visible:ring-2 focus-visible:ring-base-content/20 focus-visible:ring-inset"
|
||||
phx-click="select_item"
|
||||
phx-keydown="select_item"
|
||||
phx-key="Enter"
|
||||
phx-value-item={item.value}
|
||||
phx-target={@phx_target}
|
||||
>
|
||||
<%= if @checkboxes do %>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={Map.get(@selected, item.value, true)}
|
||||
class="checkbox checkbox-sm checkbox-primary pointer-events-none"
|
||||
tabindex="-1"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<% end %>
|
||||
<span>{item.label}</span>
|
||||
</button>
|
||||
</li>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
|
|
@ -512,7 +559,7 @@ defmodule MvWeb.CoreComponents do
|
|||
{render_slot(@subtitle)}
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex-none">{render_slot(@actions)}</div>
|
||||
<div class="flex gap-4 justify-end">{render_slot(@actions)}</div>
|
||||
</header>
|
||||
"""
|
||||
end
|
||||
|
|
|
|||
100
lib/mv_web/components/export_dropdown.ex
Normal file
100
lib/mv_web/components/export_dropdown.ex
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
defmodule MvWeb.Components.ExportDropdown do
|
||||
@moduledoc """
|
||||
Export dropdown component for member export (CSV/PDF).
|
||||
|
||||
Provides an accessible dropdown menu with CSV and PDF export options.
|
||||
Uses the same export payload as the previous single-button export.
|
||||
"""
|
||||
use MvWeb, :live_component
|
||||
use Gettext, backend: MvWeb.Gettext
|
||||
|
||||
@impl true
|
||||
def mount(socket) do
|
||||
{:ok, assign(socket, :open, false)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(assigns, socket) do
|
||||
socket =
|
||||
socket
|
||||
|> assign(:id, assigns.id)
|
||||
|> assign(:export_payload_json, assigns[:export_payload_json] || "")
|
||||
|> assign(:selected_count, assigns[:selected_count] || 0)
|
||||
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
button_label =
|
||||
gettext("Export") <>
|
||||
" (" <>
|
||||
if(assigns.selected_count == 0,
|
||||
do: gettext("all"),
|
||||
else: to_string(assigns.selected_count)
|
||||
) <>
|
||||
")"
|
||||
|
||||
assigns = assign(assigns, :button_label, button_label)
|
||||
|
||||
~H"""
|
||||
<div id={@id} data-testid="export-dropdown" class="flex-auto flex-wrap">
|
||||
<.dropdown_menu
|
||||
id={"#{@id}-menu"}
|
||||
button_label={@button_label}
|
||||
icon="hero-arrow-down-tray"
|
||||
open={@open}
|
||||
phx_target={@myself}
|
||||
menu_width="w-48"
|
||||
menu_align="left"
|
||||
button_class="btn-secondary gap-2"
|
||||
testid="export-dropdown"
|
||||
button_testid="export-dropdown-button"
|
||||
menu_testid="export-dropdown-menu"
|
||||
>
|
||||
<li role="none">
|
||||
<form method="post" action={~p"/members/export.csv"} target="_blank" class="w-full">
|
||||
<input type="hidden" name="_csrf_token" value={Plug.CSRFProtection.get_csrf_token()} />
|
||||
<input type="hidden" name="payload" value={@export_payload_json} />
|
||||
<button
|
||||
type="submit"
|
||||
role="menuitem"
|
||||
class="flex items-center gap-2 px-2 py-1 rounded cursor-pointer hover:bg-base-200 w-full text-left focus:outline-none focus-visible:ring-2 focus-visible:ring-base-content/20 focus-visible:ring-inset"
|
||||
aria-label={gettext("Export members to CSV")}
|
||||
data-testid="export-csv-link"
|
||||
>
|
||||
<.icon name="hero-document-arrow-down" class="h-4 w-4" />
|
||||
<span>{gettext("CSV")}</span>
|
||||
</button>
|
||||
</form>
|
||||
</li>
|
||||
<li role="none">
|
||||
<form method="post" action={~p"/members/export.pdf"} target="_blank" class="w-full">
|
||||
<input type="hidden" name="_csrf_token" value={Plug.CSRFProtection.get_csrf_token()} />
|
||||
<input type="hidden" name="payload" value={@export_payload_json} />
|
||||
<button
|
||||
type="submit"
|
||||
role="menuitem"
|
||||
class="flex items-center gap-2 px-2 py-1 rounded cursor-pointer hover:bg-base-200 w-full text-left focus:outline-none focus-visible:ring-2 focus-visible:ring-base-content/20 focus-visible:ring-inset"
|
||||
aria-label={gettext("Export members to PDF")}
|
||||
data-testid="export-pdf-link"
|
||||
>
|
||||
<.icon name="hero-document-text" class="h-4 w-4" />
|
||||
<span>{gettext("PDF")}</span>
|
||||
</button>
|
||||
</form>
|
||||
</li>
|
||||
</.dropdown_menu>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("toggle_dropdown", _params, socket) do
|
||||
{:noreply, assign(socket, :open, !socket.assigns.open)}
|
||||
end
|
||||
|
||||
def handle_event("close_dropdown", _params, socket) do
|
||||
{:noreply, assign(socket, :open, false)}
|
||||
end
|
||||
end
|
||||
|
|
@ -88,6 +88,14 @@ defmodule MvWeb.Layouts.Sidebar do
|
|||
/>
|
||||
<% end %>
|
||||
|
||||
<%= if can_access_page?(@current_user, PagePaths.statistics()) do %>
|
||||
<.menu_item
|
||||
href={~p"/statistics"}
|
||||
icon="hero-chart-bar"
|
||||
label={gettext("Statistics")}
|
||||
/>
|
||||
<% end %>
|
||||
|
||||
<%= if admin_menu_visible?(@current_user) do %>
|
||||
<.menu_group
|
||||
icon="hero-cog-6-tooth"
|
||||
|
|
|
|||
159
lib/mv_web/controllers/member_pdf_export_controller.ex
Normal file
159
lib/mv_web/controllers/member_pdf_export_controller.ex
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
defmodule MvWeb.MemberPdfExportController do
|
||||
@moduledoc """
|
||||
PDF export for members.
|
||||
|
||||
Expects `payload` as JSON string form param.
|
||||
Uses the same actor/permissions as the member overview.
|
||||
"""
|
||||
|
||||
use MvWeb, :controller
|
||||
|
||||
require Logger
|
||||
|
||||
alias Mv.Authorization.Actor
|
||||
alias Mv.Membership.{MemberExport, MemberExport.Build, MembersPDF}
|
||||
alias MvWeb.Translations.MemberFields
|
||||
|
||||
use Gettext, backend: MvWeb.Gettext
|
||||
|
||||
@payload_required_message "payload required"
|
||||
@invalid_json_message "invalid JSON"
|
||||
@export_failed_message "Failed to generate PDF export"
|
||||
|
||||
@allowed_member_field_strings Mv.Constants.member_fields() |> Enum.map(&Atom.to_string/1)
|
||||
|
||||
def export(conn, %{"payload" => payload}) when is_binary(payload) do
|
||||
actor = current_actor(conn)
|
||||
|
||||
if is_nil(actor) do
|
||||
forbidden(conn)
|
||||
else
|
||||
locale = get_locale(conn)
|
||||
club_name = get_club_name()
|
||||
|
||||
with {:ok, decoded} <- decode_json_map(payload),
|
||||
parsed <- MemberExport.parse_params(decoded),
|
||||
{:ok, export_data} <- Build.build(actor, parsed, &label_for_column/1),
|
||||
{:ok, pdf_binary} <-
|
||||
MembersPDF.render(export_data, locale: locale, club_name: club_name) do
|
||||
filename = "members-#{Date.utc_today()}.pdf"
|
||||
|
||||
send_download(
|
||||
conn,
|
||||
{:binary, pdf_binary},
|
||||
filename: filename,
|
||||
content_type: "application/pdf"
|
||||
)
|
||||
else
|
||||
{:error, :invalid_json} ->
|
||||
bad_request(conn, @invalid_json_message)
|
||||
|
||||
{:error, :forbidden} ->
|
||||
forbidden(conn)
|
||||
|
||||
{:error, {:row_limit_exceeded, row_count, max_rows}} ->
|
||||
unprocessable_entity(conn, %{
|
||||
error: "row_limit_exceeded",
|
||||
message:
|
||||
gettext("Export contains %{count} rows, maximum is %{max}",
|
||||
count: row_count,
|
||||
max: max_rows
|
||||
),
|
||||
row_count: row_count,
|
||||
max_rows: max_rows
|
||||
})
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.warning("PDF export failed: #{inspect(reason)}")
|
||||
|
||||
internal_error(conn, %{
|
||||
error: "export_failed",
|
||||
message: gettext(@export_failed_message)
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def export(conn, _params) do
|
||||
bad_request(conn, @payload_required_message)
|
||||
end
|
||||
|
||||
# --- Actor / auth ---
|
||||
|
||||
defp current_actor(conn) do
|
||||
conn.assigns[:current_user]
|
||||
|> Actor.ensure_loaded()
|
||||
end
|
||||
|
||||
defp forbidden(conn) do
|
||||
conn
|
||||
|> put_status(:forbidden)
|
||||
|> json(%{error: "forbidden", message: "Forbidden"})
|
||||
|> halt()
|
||||
end
|
||||
|
||||
# --- Decoding / validation ---
|
||||
|
||||
defp decode_json_map(payload) when is_binary(payload) do
|
||||
case Jason.decode(payload) do
|
||||
{:ok, decoded} when is_map(decoded) -> {:ok, decoded}
|
||||
_ -> {:error, :invalid_json}
|
||||
end
|
||||
end
|
||||
|
||||
# --- Column labels ---
|
||||
|
||||
# Goal: translate known member fields to UI labels, but never crash.
|
||||
# - Atoms: label directly.
|
||||
# - Binaries: only translate if they are known member fields (allowlist); otherwise return the string.
|
||||
# This avoids String.to_existing_atom/1 exceptions for arbitrary keys (e.g., "custom_field_...").
|
||||
defp label_for_column(key) when is_atom(key) do
|
||||
MemberFields.label(key)
|
||||
end
|
||||
|
||||
defp label_for_column(key) when is_binary(key) do
|
||||
if key in @allowed_member_field_strings do
|
||||
# Safe because key is in allowlist which originates from existing atoms
|
||||
MemberFields.label(String.to_existing_atom(key))
|
||||
else
|
||||
key
|
||||
end
|
||||
end
|
||||
|
||||
defp label_for_column(key) do
|
||||
to_string(key)
|
||||
end
|
||||
|
||||
# --- Locale and club name ---
|
||||
|
||||
defp get_locale(conn) do
|
||||
conn.assigns[:locale] || Gettext.get_locale(MvWeb.Gettext) || "en"
|
||||
end
|
||||
|
||||
defp get_club_name do
|
||||
case Mv.Membership.get_settings() do
|
||||
{:ok, settings} -> settings.club_name
|
||||
_ -> "Club"
|
||||
end
|
||||
end
|
||||
|
||||
# --- JSON responses ---
|
||||
|
||||
defp bad_request(conn, message) when is_binary(message) do
|
||||
conn
|
||||
|> put_status(:bad_request)
|
||||
|> json(%{error: "bad_request", message: message})
|
||||
end
|
||||
|
||||
defp unprocessable_entity(conn, body) when is_map(body) do
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(body)
|
||||
end
|
||||
|
||||
defp internal_error(conn, body) when is_map(body) do
|
||||
conn
|
||||
|> put_status(:internal_server_error)
|
||||
|> json(body)
|
||||
end
|
||||
end
|
||||
|
|
@ -810,53 +810,59 @@ defmodule MvWeb.MemberLive.Index do
|
|||
show_current_cycle,
|
||||
boolean_filters
|
||||
) do
|
||||
field_str =
|
||||
if is_atom(sort_field) do
|
||||
Atom.to_string(sort_field)
|
||||
else
|
||||
sort_field
|
||||
end
|
||||
base_params = build_base_params(query, sort_field, sort_order)
|
||||
base_params = add_cycle_status_filter(base_params, cycle_status_filter)
|
||||
base_params = add_group_filters(base_params, group_filters)
|
||||
base_params = add_show_current_cycle(base_params, show_current_cycle)
|
||||
add_boolean_filters(base_params, boolean_filters)
|
||||
end
|
||||
|
||||
order_str =
|
||||
if is_atom(sort_order) do
|
||||
Atom.to_string(sort_order)
|
||||
else
|
||||
sort_order
|
||||
end
|
||||
|
||||
base_params = %{
|
||||
"query" => query,
|
||||
"sort_field" => field_str,
|
||||
"sort_order" => order_str
|
||||
defp build_base_params(query, sort_field, sort_order) do
|
||||
%{
|
||||
"query" => query || "",
|
||||
"sort_field" => normalize_sort_field(sort_field),
|
||||
"sort_order" => normalize_sort_order(sort_order)
|
||||
}
|
||||
end
|
||||
|
||||
base_params =
|
||||
case cycle_status_filter do
|
||||
nil -> base_params
|
||||
:paid -> Map.put(base_params, "cycle_status_filter", "paid")
|
||||
:unpaid -> Map.put(base_params, "cycle_status_filter", "unpaid")
|
||||
end
|
||||
defp normalize_sort_field(nil), do: ""
|
||||
defp normalize_sort_field(field) when is_atom(field), do: Atom.to_string(field)
|
||||
defp normalize_sort_field(field) when is_binary(field), do: field
|
||||
defp normalize_sort_field(_), do: ""
|
||||
|
||||
base_params =
|
||||
Enum.reduce(group_filters, base_params, fn {group_id_str, value}, acc ->
|
||||
param_value = if value == :in, do: "in", else: "not_in"
|
||||
Map.put(acc, "#{@group_filter_prefix}#{group_id_str}", param_value)
|
||||
end)
|
||||
defp normalize_sort_order(nil), do: ""
|
||||
defp normalize_sort_order(order) when is_atom(order), do: Atom.to_string(order)
|
||||
defp normalize_sort_order(order) when is_binary(order), do: order
|
||||
defp normalize_sort_order(_), do: ""
|
||||
|
||||
base_params =
|
||||
if show_current_cycle do
|
||||
Map.put(base_params, "show_current_cycle", "true")
|
||||
else
|
||||
base_params
|
||||
end
|
||||
|
||||
Enum.reduce(boolean_filters, base_params, fn {custom_field_id, filter_value}, acc ->
|
||||
param_key = "#{@boolean_filter_prefix}#{custom_field_id}"
|
||||
param_value = if filter_value == true, do: "true", else: "false"
|
||||
Map.put(acc, param_key, param_value)
|
||||
defp add_group_filters(params, group_filters) do
|
||||
Enum.reduce(group_filters, params, fn {group_id_str, value}, acc ->
|
||||
param_value = if value == :in, do: "in", else: "not_in"
|
||||
Map.put(acc, "#{@group_filter_prefix}#{group_id_str}", param_value)
|
||||
end)
|
||||
end
|
||||
|
||||
defp add_cycle_status_filter(params, nil), do: params
|
||||
defp add_cycle_status_filter(params, :paid), do: Map.put(params, "cycle_status_filter", "paid")
|
||||
|
||||
defp add_cycle_status_filter(params, :unpaid),
|
||||
do: Map.put(params, "cycle_status_filter", "unpaid")
|
||||
|
||||
defp add_cycle_status_filter(params, _), do: params
|
||||
|
||||
defp add_show_current_cycle(params, true), do: Map.put(params, "show_current_cycle", "true")
|
||||
defp add_show_current_cycle(params, _), do: params
|
||||
|
||||
defp add_boolean_filters(params, boolean_filters) do
|
||||
Enum.reduce(boolean_filters, params, &add_boolean_filter/2)
|
||||
end
|
||||
|
||||
defp add_boolean_filter({custom_field_id, filter_value}, acc) do
|
||||
param_key = "#{@boolean_filter_prefix}#{custom_field_id}"
|
||||
param_value = if filter_value == true, do: "true", else: "false"
|
||||
Map.put(acc, param_key, param_value)
|
||||
end
|
||||
|
||||
# -------------------------------------------------------------
|
||||
# Loading members
|
||||
# -------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -2,20 +2,12 @@
|
|||
<.header>
|
||||
{gettext("Members")}
|
||||
<:actions>
|
||||
<form method="post" action={~p"/members/export.csv"} target="_blank" class="inline">
|
||||
<input type="hidden" name="_csrf_token" value={Plug.CSRFProtection.get_csrf_token()} />
|
||||
<input type="hidden" name="payload" value={@export_payload_json} />
|
||||
<button
|
||||
type="submit"
|
||||
class="btn btn-secondary gap-2"
|
||||
aria-label={gettext("Export members to CSV")}
|
||||
>
|
||||
<.icon name="hero-arrow-down-tray" />
|
||||
{gettext("Export to CSV")} ({if @selected_count == 0,
|
||||
do: gettext("all"),
|
||||
else: @selected_count})
|
||||
</button>
|
||||
</form>
|
||||
<.live_component
|
||||
module={MvWeb.Components.ExportDropdown}
|
||||
id="export-dropdown"
|
||||
export_payload_json={@export_payload_json}
|
||||
selected_count={@selected_count}
|
||||
/>
|
||||
<.button
|
||||
class="secondary"
|
||||
id="copy-emails-btn"
|
||||
|
|
|
|||
628
lib/mv_web/live/statistics_live.ex
Normal file
628
lib/mv_web/live/statistics_live.ex
Normal file
|
|
@ -0,0 +1,628 @@
|
|||
defmodule MvWeb.StatisticsLive do
|
||||
@moduledoc """
|
||||
LiveView for the statistics page at /statistics.
|
||||
|
||||
Displays aggregated member and membership fee cycle statistics.
|
||||
"""
|
||||
use MvWeb, :live_view
|
||||
|
||||
require Logger
|
||||
|
||||
import MvWeb.LiveHelpers, only: [current_actor: 1]
|
||||
alias Mv.Statistics
|
||||
alias Mv.MembershipFees.MembershipFeeType
|
||||
alias MvWeb.Helpers.MembershipFeeHelpers
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
# Only static assigns and fee types here; load_statistics runs once in handle_params
|
||||
socket =
|
||||
socket
|
||||
|> assign(:page_title, gettext("Statistics"))
|
||||
|> assign(:selected_fee_type_id, nil)
|
||||
|> load_fee_types()
|
||||
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, uri, socket) do
|
||||
# Query params: after push_patch, params may not include query string in some cases;
|
||||
# always derive from URI as well so fee_type_id is reliable.
|
||||
uri_query = if uri, do: URI.decode_query(URI.parse(uri).query || ""), else: %{}
|
||||
fee_type_id = params["fee_type_id"] || uri_query["fee_type_id"]
|
||||
fee_type_id = normalize_fee_type_id(fee_type_id)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(:selected_fee_type_id, fee_type_id)
|
||||
|> load_statistics()
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp normalize_fee_type_id(nil), do: nil
|
||||
defp normalize_fee_type_id(""), do: nil
|
||||
|
||||
defp normalize_fee_type_id(id) when is_binary(id) do
|
||||
case String.trim(id) do
|
||||
"" -> nil
|
||||
trimmed -> trimmed
|
||||
end
|
||||
end
|
||||
|
||||
defp normalize_fee_type_id(_), do: nil
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<Layouts.app flash={@flash} current_user={@current_user}>
|
||||
<.header>
|
||||
{gettext("Statistics")}
|
||||
<:subtitle>{gettext("Overview from first membership to today")}</:subtitle>
|
||||
</.header>
|
||||
|
||||
<section class="mb-8" aria-labelledby="members-heading">
|
||||
<h2 id="members-heading" class="text-xl font-semibold mb-4">{gettext("Members")}</h2>
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-6">
|
||||
<div class="card bg-base-200 shadow-md border border-base-300">
|
||||
<div class="card-body p-5">
|
||||
<h3 class="card-title text-sm font-medium text-base-content/80">
|
||||
{gettext("Active members")}
|
||||
</h3>
|
||||
<p
|
||||
class="text-3xl font-bold tabular-nums"
|
||||
aria-label={gettext("Active members") <> ": " <> to_string(@active_count)}
|
||||
>
|
||||
{@active_count}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card bg-base-200 shadow-md border border-base-300">
|
||||
<div class="card-body p-5">
|
||||
<h3 class="card-title text-sm font-medium text-base-content/80">
|
||||
{gettext("Inactive members")}
|
||||
</h3>
|
||||
<p
|
||||
class="text-3xl font-bold tabular-nums"
|
||||
aria-label={gettext("Inactive members") <> ": " <> to_string(@inactive_count)}
|
||||
>
|
||||
{@inactive_count}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card bg-base-200 shadow-md border border-base-300">
|
||||
<div class="card-body">
|
||||
<h3 class="card-title text-lg mb-4">{gettext("Member numbers by year")}</h3>
|
||||
<p class="text-sm text-base-content/70 mb-4">
|
||||
{gettext("From %{first} to %{last} (relevant years with membership data)",
|
||||
first: @years |> List.last() |> to_string(),
|
||||
last: @years |> List.first() |> to_string()
|
||||
)}
|
||||
</p>
|
||||
<.member_numbers_table joins_exits_by_year={@joins_exits_by_year} />
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="mb-8" aria-labelledby="contributions-heading">
|
||||
<h2 id="contributions-heading" class="text-xl font-semibold mb-4">
|
||||
{gettext("Contributions")}
|
||||
</h2>
|
||||
<div class="flex flex-wrap items-end gap-4 mb-6">
|
||||
<div class="flex items-center gap-2">
|
||||
<form id="fee-type-form" phx-change="change_fee_type" class="flex items-center gap-2">
|
||||
<label for="fee-type-filter" class="text-sm font-medium text-base-content/80">
|
||||
{gettext("Fee type")}:
|
||||
</label>
|
||||
<select
|
||||
id="fee-type-filter"
|
||||
name="fee_type_id"
|
||||
class="select select-bordered select-sm min-w-[10rem]"
|
||||
>
|
||||
<option value="" selected={@selected_fee_type_id in [nil, ""]}>
|
||||
{gettext("All")}
|
||||
</option>
|
||||
<%= for ft <- @membership_fee_types do %>
|
||||
<option
|
||||
value={ft.id}
|
||||
selected={to_string(@selected_fee_type_id) == to_string(ft.id)}
|
||||
>
|
||||
{ft.name}
|
||||
</option>
|
||||
<% end %>
|
||||
</select>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card bg-base-200 shadow-md border border-base-300">
|
||||
<div class="card-body">
|
||||
<h3 class="card-title text-lg mb-4">{gettext("Contributions by year")}</h3>
|
||||
<div class="flex flex-col gap-8 items-start">
|
||||
<div class="w-full">
|
||||
<.contributions_bars_by_year
|
||||
contributions_by_year={@contributions_by_year}
|
||||
totals_over_all_years={@totals_over_all_years}
|
||||
/>
|
||||
</div>
|
||||
<div class="w-full flex flex-col items-center pt-6 mt-2 border-t border-base-300">
|
||||
<h4 class="text-sm font-semibold mb-3">{gettext("All years combined (pie)")}</h4>
|
||||
<.contributions_pie cycle_totals={@totals_over_all_years} />
|
||||
<p class="text-xs text-base-content/70 mt-2">
|
||||
<span class="inline-block w-2 h-2 rounded-full bg-success align-middle mr-1"></span>
|
||||
{gettext("Paid")}
|
||||
<span class="inline-block w-2 h-2 rounded-full bg-warning align-middle mx-2 mr-1">
|
||||
</span>
|
||||
{gettext("Unpaid")}
|
||||
<span class="inline-block w-2 h-2 rounded-full bg-base-content/20 align-middle mx-2 mr-1">
|
||||
</span>
|
||||
{gettext("Suspended")}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</Layouts.app>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("change_fee_type", %{"fee_type_id" => ""}, socket) do
|
||||
{:noreply, push_patch(socket, to: ~p"/statistics")}
|
||||
end
|
||||
|
||||
def handle_event("change_fee_type", %{"fee_type_id" => id}, socket) when is_binary(id) do
|
||||
trimmed = String.trim(id)
|
||||
|
||||
to =
|
||||
if trimmed == "",
|
||||
do: ~p"/statistics",
|
||||
else: ~p"/statistics" <> "?" <> URI.encode_query(%{"fee_type_id" => trimmed})
|
||||
|
||||
{:noreply, push_patch(socket, to: to)}
|
||||
end
|
||||
|
||||
attr :joins_exits_by_year, :list, required: true
|
||||
|
||||
defp member_numbers_table(assigns) do
|
||||
rows = assigns.joins_exits_by_year
|
||||
total_activity = Enum.map(rows, fn r -> r.joins + r.exits end)
|
||||
max_total = (total_activity != [] && Enum.max(total_activity)) || 1
|
||||
|
||||
rows_with_pct =
|
||||
Enum.map(rows, fn row ->
|
||||
sum = row.joins + row.exits
|
||||
|
||||
bar_pct =
|
||||
if max_total > 0 and sum > 0 do
|
||||
min(100.0, Float.round(sum / max_total * 100, 1))
|
||||
else
|
||||
0
|
||||
end
|
||||
|
||||
seg_scale = max(sum, 1)
|
||||
joins_pct = min(100.0, row.joins / seg_scale * 100)
|
||||
exits_pct = min(100.0, row.exits / seg_scale * 100)
|
||||
|
||||
%{
|
||||
year: row.year,
|
||||
joins: row.joins,
|
||||
exits: row.exits,
|
||||
bar_pct: bar_pct,
|
||||
joins_pct: Float.round(joins_pct, 1),
|
||||
exits_pct: Float.round(exits_pct, 1)
|
||||
}
|
||||
end)
|
||||
|
||||
assigns = assign(assigns, :rows, rows_with_pct)
|
||||
|
||||
~H"""
|
||||
<div
|
||||
class="overflow-x-auto"
|
||||
role="img"
|
||||
aria-label={gettext("Member numbers by year as table with bars")}
|
||||
>
|
||||
<table class="table table-sm w-full">
|
||||
<thead class="bg-base-300">
|
||||
<tr>
|
||||
<th scope="col" class="text-base-content font-semibold w-20">{gettext("Year")}</th>
|
||||
<th scope="col" class="text-base-content font-semibold">{gettext("Joins")}</th>
|
||||
<th scope="col" class="text-base-content font-semibold">{gettext("Exits")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<%= for row <- @rows do %>
|
||||
<tr>
|
||||
<td
|
||||
rowspan="2"
|
||||
class="font-mono align-middle border-b-0"
|
||||
>
|
||||
{row.year}
|
||||
</td>
|
||||
<td colspan="2" class="align-top p-1 pb-0 border-b-0">
|
||||
<div class="h-6 rounded overflow-hidden bg-base-300 relative min-w-[4rem]">
|
||||
<div
|
||||
class="flex h-full absolute left-0 top-0 bottom-0 min-w-0 rounded"
|
||||
style={"width: #{max(0, row.bar_pct)}%"}
|
||||
>
|
||||
<div
|
||||
class="h-full bg-success min-w-0 rounded-l"
|
||||
style={"width: #{row.joins_pct}%"}
|
||||
title={gettext("Joins")}
|
||||
>
|
||||
</div>
|
||||
<div
|
||||
class="h-full bg-error min-w-0 rounded-r"
|
||||
style={"width: #{row.exits_pct}%"}
|
||||
title={gettext("Exits")}
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="align-middle text-xs font-mono text-base-content whitespace-nowrap pt-0">
|
||||
<span class="inline-flex items-center gap-1.5">
|
||||
{row.joins}
|
||||
<span
|
||||
class="inline-block w-2 h-2 rounded-full bg-success shrink-0"
|
||||
aria-hidden="true"
|
||||
title={gettext("Joins")}
|
||||
>
|
||||
</span>
|
||||
</span>
|
||||
</td>
|
||||
<td class="align-middle text-xs font-mono text-base-content whitespace-nowrap pt-0">
|
||||
<span class="inline-flex items-center gap-1.5">
|
||||
{row.exits}
|
||||
<span
|
||||
class="inline-block w-2 h-2 rounded-full bg-error shrink-0"
|
||||
aria-hidden="true"
|
||||
title={gettext("Exits")}
|
||||
>
|
||||
</span>
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
attr :contributions_by_year, :list, required: true
|
||||
attr :totals_over_all_years, :map, required: true
|
||||
|
||||
defp contributions_bars_by_year(assigns) do
|
||||
rows = assigns.contributions_by_year
|
||||
totals = assigns.totals_over_all_years
|
||||
|
||||
all_rows_with_decimals =
|
||||
Enum.map(rows, fn row ->
|
||||
%{
|
||||
year: row.year,
|
||||
summary: false,
|
||||
total: row.total,
|
||||
paid: row.paid,
|
||||
unpaid: row.unpaid,
|
||||
suspended: row.suspended
|
||||
}
|
||||
end) ++
|
||||
[
|
||||
%{
|
||||
year: nil,
|
||||
summary: true,
|
||||
total: totals.total,
|
||||
paid: totals.paid,
|
||||
unpaid: totals.unpaid,
|
||||
suspended: totals.suspended
|
||||
}
|
||||
]
|
||||
|
||||
max_total = max_decimal(all_rows_with_decimals, :total)
|
||||
|
||||
rows_with_pct =
|
||||
Enum.map(all_rows_with_decimals, fn row ->
|
||||
bar_pct = bar_pct(row.total, max_total)
|
||||
|
||||
sum_positive =
|
||||
Decimal.add(Decimal.add(row.paid, row.unpaid), row.suspended)
|
||||
|
||||
seg_scale =
|
||||
if Decimal.compare(sum_positive, 0) == :gt, do: sum_positive, else: Decimal.new(1)
|
||||
|
||||
paid_pct =
|
||||
row.paid
|
||||
|> Decimal.div(seg_scale)
|
||||
|> Decimal.mult(100)
|
||||
|> Decimal.to_float()
|
||||
|> min(100.0)
|
||||
|
||||
unpaid_pct =
|
||||
row.unpaid
|
||||
|> Decimal.div(seg_scale)
|
||||
|> Decimal.mult(100)
|
||||
|> Decimal.to_float()
|
||||
|> min(100.0)
|
||||
|
||||
suspended_pct =
|
||||
row.suspended
|
||||
|> Decimal.div(seg_scale)
|
||||
|> Decimal.mult(100)
|
||||
|> Decimal.to_float()
|
||||
|> min(100.0)
|
||||
|
||||
%{
|
||||
year: row.year,
|
||||
summary: row.summary,
|
||||
total_formatted: MembershipFeeHelpers.format_currency(row.total),
|
||||
paid_formatted: MembershipFeeHelpers.format_currency(row.paid),
|
||||
unpaid_formatted: MembershipFeeHelpers.format_currency(row.unpaid),
|
||||
suspended_formatted: MembershipFeeHelpers.format_currency(row.suspended),
|
||||
bar_pct: bar_pct,
|
||||
paid_pct: paid_pct,
|
||||
unpaid_pct: unpaid_pct,
|
||||
suspended_pct: suspended_pct
|
||||
}
|
||||
end)
|
||||
|
||||
assigns = assign(assigns, :rows, rows_with_pct)
|
||||
|
||||
~H"""
|
||||
<div
|
||||
class="overflow-x-auto"
|
||||
role="img"
|
||||
aria-label={gettext("Contributions by year as table with stacked bars")}
|
||||
>
|
||||
<table class="table table-sm w-full">
|
||||
<thead class="bg-base-300">
|
||||
<tr>
|
||||
<th scope="col" class="text-base-content font-semibold w-20">{gettext("Year")}</th>
|
||||
<th scope="col" class="text-base-content font-semibold">{gettext("Paid")}</th>
|
||||
<th scope="col" class="text-base-content font-semibold">{gettext("Unpaid")}</th>
|
||||
<th scope="col" class="text-base-content font-semibold">{gettext("Suspended")}</th>
|
||||
<th scope="col" class="text-base-content font-semibold">{gettext("Total")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<%= for row <- @rows do %>
|
||||
<tr class={row.summary && "border-t-2 border-base-300 bg-base-300/30"}>
|
||||
<td
|
||||
rowspan="2"
|
||||
class={"font-mono align-middle border-b-0 #{if row.summary, do: "font-semibold", else: ""}"}
|
||||
>
|
||||
<%= if row.summary do %>
|
||||
{gettext("Total")}
|
||||
<% else %>
|
||||
{row.year}
|
||||
<% end %>
|
||||
</td>
|
||||
<td colspan="4" class="align-top p-1 pb-0 border-b-0">
|
||||
<div class="h-6 rounded overflow-hidden bg-base-300 relative min-w-[4rem]">
|
||||
<div
|
||||
class="flex h-full absolute left-0 top-0 bottom-0 min-w-0 rounded"
|
||||
style={"width: #{max(0, Float.round(row.bar_pct, 1))}%"}
|
||||
>
|
||||
<div
|
||||
class="h-full bg-success min-w-0 rounded-l"
|
||||
style={"width: #{Float.round(row.paid_pct, 1)}%"}
|
||||
title={gettext("Paid")}
|
||||
>
|
||||
</div>
|
||||
<div
|
||||
class="h-full bg-warning min-w-0"
|
||||
style={"width: #{Float.round(row.unpaid_pct, 1)}%"}
|
||||
title={gettext("Unpaid")}
|
||||
>
|
||||
</div>
|
||||
<div
|
||||
class="h-full bg-base-content/20 min-w-0 rounded-r"
|
||||
style={"width: #{Float.round(row.suspended_pct, 1)}%"}
|
||||
title={gettext("Suspended")}
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr class={row.summary && "bg-base-300/30"}>
|
||||
<td class="align-middle text-xs font-mono text-base-content whitespace-nowrap pt-0">
|
||||
<span class="inline-flex items-center gap-1.5">
|
||||
{row.paid_formatted}
|
||||
<span
|
||||
class="inline-block w-2 h-2 rounded-full bg-success shrink-0"
|
||||
aria-hidden="true"
|
||||
title={gettext("Paid")}
|
||||
>
|
||||
</span>
|
||||
</span>
|
||||
</td>
|
||||
<td class="align-middle text-xs font-mono text-base-content whitespace-nowrap pt-0">
|
||||
<span class="inline-flex items-center gap-1.5">
|
||||
{row.unpaid_formatted}
|
||||
<span
|
||||
class="inline-block w-2 h-2 rounded-full bg-warning shrink-0"
|
||||
aria-hidden="true"
|
||||
title={gettext("Unpaid")}
|
||||
>
|
||||
</span>
|
||||
</span>
|
||||
</td>
|
||||
<td class="align-middle text-xs font-mono text-base-content whitespace-nowrap pt-0">
|
||||
<span class="inline-flex items-center gap-1.5">
|
||||
{row.suspended_formatted}
|
||||
<span
|
||||
class="inline-block w-2 h-2 rounded-full bg-base-content/20 shrink-0"
|
||||
aria-hidden="true"
|
||||
title={gettext("Suspended")}
|
||||
>
|
||||
</span>
|
||||
</span>
|
||||
</td>
|
||||
<td class="align-middle text-xs font-mono text-base-content whitespace-nowrap pt-0">
|
||||
{row.total_formatted}
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp max_decimal(rows, key) do
|
||||
Enum.reduce(rows, Decimal.new(0), fn row, acc ->
|
||||
val = Map.get(row, key)
|
||||
if Decimal.compare(val, acc) == :gt, do: val, else: acc
|
||||
end)
|
||||
end
|
||||
|
||||
defp bar_pct(value, max) do
|
||||
scale = if Decimal.compare(max, 0) == :gt, do: max, else: Decimal.new(1)
|
||||
pct = value |> Decimal.div(scale) |> Decimal.mult(100) |> Decimal.to_float()
|
||||
min(100.0, pct)
|
||||
end
|
||||
|
||||
attr :cycle_totals, :map, required: true
|
||||
|
||||
defp contributions_pie(assigns) do
|
||||
paid = assigns.cycle_totals.paid
|
||||
unpaid = assigns.cycle_totals.unpaid
|
||||
suspended = assigns.cycle_totals.suspended
|
||||
|
||||
sum_positive = Decimal.add(Decimal.add(paid, unpaid), suspended)
|
||||
scale = if Decimal.compare(sum_positive, 0) == :gt, do: sum_positive, else: Decimal.new(1)
|
||||
|
||||
paid_pct = Decimal.div(paid, scale) |> Decimal.mult(100) |> Decimal.to_float()
|
||||
unpaid_pct = Decimal.div(unpaid, scale) |> Decimal.mult(100) |> Decimal.to_float()
|
||||
suspended_pct = Decimal.div(suspended, scale) |> Decimal.mult(100) |> Decimal.to_float()
|
||||
|
||||
# Conic gradient: 0deg = top, clockwise. Success (paid), warning (unpaid), base-300 (suspended)
|
||||
# Use theme CSS variables (--color-*) so the pie renders in all themes
|
||||
paid_deg = paid_pct * 3.6
|
||||
unpaid_deg = unpaid_pct * 3.6
|
||||
|
||||
gradient_stops =
|
||||
"var(--color-success) 0deg, var(--color-success) #{paid_deg}deg, var(--color-warning) #{paid_deg}deg, var(--color-warning) #{paid_deg + unpaid_deg}deg, var(--color-base-300) #{paid_deg + unpaid_deg}deg, var(--color-base-300) 360deg"
|
||||
|
||||
assigns =
|
||||
assigns
|
||||
|> assign(:paid_pct, paid_pct)
|
||||
|> assign(:unpaid_pct, unpaid_pct)
|
||||
|> assign(:suspended_pct, suspended_pct)
|
||||
|> assign(:gradient_stops, gradient_stops)
|
||||
|
||||
~H"""
|
||||
<div
|
||||
class="w-40 h-40 min-h-[10rem] rounded-full shrink-0 border-2 border-base-300 bg-base-300"
|
||||
style={"background: conic-gradient(#{@gradient_stops});"}
|
||||
role="img"
|
||||
aria-label={
|
||||
gettext(
|
||||
"Contributions pie: paid %{paid}%%, unpaid %{unpaid}%%, suspended %{suspended}%%",
|
||||
paid: Float.round(@paid_pct, 1),
|
||||
unpaid: Float.round(@unpaid_pct, 1),
|
||||
suspended: Float.round(@suspended_pct, 1)
|
||||
)
|
||||
}
|
||||
title={"#{gettext("Paid")}: #{Float.round(@paid_pct, 1)}%, #{gettext("Unpaid")}: #{Float.round(@unpaid_pct, 1)}%, #{gettext("Suspended")}: #{Float.round(@suspended_pct, 1)}%"}
|
||||
>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp load_fee_types(socket) do
|
||||
actor = current_actor(socket)
|
||||
|
||||
case MembershipFeeType
|
||||
|> Ash.Query.sort(name: :asc)
|
||||
|> Ash.read(domain: Mv.MembershipFees, actor: actor) do
|
||||
{:ok, fee_types} ->
|
||||
assign(socket, :membership_fee_types, fee_types)
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.warning("StatisticsLive: failed to load fee types: #{inspect(reason)}")
|
||||
|
||||
socket
|
||||
|> put_flash(:error, gettext("Fee types could not be loaded."))
|
||||
|> assign(:membership_fee_types, [])
|
||||
end
|
||||
end
|
||||
|
||||
defp load_statistics(socket) do
|
||||
actor = current_actor(socket)
|
||||
fee_type_id = socket.assigns[:selected_fee_type_id]
|
||||
# Member stats must never depend on fee type (only contributions do)
|
||||
opts_member = [actor: actor]
|
||||
|
||||
opts_contributions =
|
||||
[actor: actor] ++ if fee_type_id, do: [fee_type_id: fee_type_id], else: []
|
||||
|
||||
current_year = Date.utc_today().year
|
||||
first_year = Statistics.first_join_year(opts_member) || current_year
|
||||
years = first_year..current_year |> Enum.to_list() |> Enum.reverse()
|
||||
|
||||
active_count = Statistics.active_member_count(opts_member)
|
||||
inactive_count = Statistics.inactive_member_count(opts_member)
|
||||
joins_exits_by_year = build_joins_exits_by_year(years, opts_member)
|
||||
contributions_by_year = build_contributions_by_year(years, opts_contributions)
|
||||
totals_over_all_years = sum_cycle_totals(contributions_by_year)
|
||||
|
||||
assign(socket,
|
||||
years: years,
|
||||
active_count: active_count,
|
||||
inactive_count: inactive_count,
|
||||
joins_exits_by_year: joins_exits_by_year,
|
||||
contributions_by_year: contributions_by_year,
|
||||
totals_over_all_years: totals_over_all_years
|
||||
)
|
||||
end
|
||||
|
||||
defp build_joins_exits_by_year(years, opts) do
|
||||
Enum.map(years, fn y ->
|
||||
%{
|
||||
year: y,
|
||||
joins: Statistics.joins_by_year(y, opts),
|
||||
exits: Statistics.exits_by_year(y, opts)
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp build_contributions_by_year(years, opts) do
|
||||
Enum.map(years, fn y ->
|
||||
totals = Statistics.cycle_totals_by_year(y, opts)
|
||||
|
||||
%{
|
||||
year: y,
|
||||
total: totals.total,
|
||||
paid: totals.paid,
|
||||
unpaid: totals.unpaid,
|
||||
suspended: totals.suspended
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp sum_cycle_totals(contributions_by_year) do
|
||||
Enum.reduce(
|
||||
contributions_by_year,
|
||||
%{
|
||||
total: Decimal.new(0),
|
||||
paid: Decimal.new(0),
|
||||
unpaid: Decimal.new(0),
|
||||
suspended: Decimal.new(0)
|
||||
},
|
||||
fn row, acc ->
|
||||
%{
|
||||
total: Decimal.add(acc.total, row.total),
|
||||
paid: Decimal.add(acc.paid, row.paid),
|
||||
unpaid: Decimal.add(acc.unpaid, row.unpaid),
|
||||
suspended: Decimal.add(acc.suspended, row.suspended)
|
||||
}
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
@ -9,6 +9,7 @@ defmodule MvWeb.PagePaths do
|
|||
# Sidebar top-level menu paths
|
||||
@members "/members"
|
||||
@membership_fee_types "/membership_fee_types"
|
||||
@statistics "/statistics"
|
||||
|
||||
# Administration submenu paths (all must match router)
|
||||
@users "/users"
|
||||
|
|
@ -31,6 +32,9 @@ defmodule MvWeb.PagePaths do
|
|||
@doc "Path for Membership Fee Types index (sidebar and page permission check)."
|
||||
def membership_fee_types, do: @membership_fee_types
|
||||
|
||||
@doc "Path for Statistics page (sidebar and page permission check)."
|
||||
def statistics, do: @statistics
|
||||
|
||||
@doc "Paths for Administration menu; show group if user can access any of these."
|
||||
def admin_menu_paths, do: @admin_page_paths
|
||||
|
||||
|
|
|
|||
|
|
@ -73,6 +73,9 @@ defmodule MvWeb.Router do
|
|||
|
||||
# Membership Fee Types Management
|
||||
live "/membership_fee_types", MembershipFeeTypeLive.Index, :index
|
||||
|
||||
# Statistics
|
||||
live "/statistics", StatisticsLive, :index
|
||||
live "/membership_fee_types/new", MembershipFeeTypeLive.Form, :new
|
||||
live "/membership_fee_types/:id/edit", MembershipFeeTypeLive.Form, :edit
|
||||
|
||||
|
|
@ -92,6 +95,7 @@ defmodule MvWeb.Router do
|
|||
live "/admin/import-export", ImportExportLive
|
||||
|
||||
post "/members/export.csv", MemberExportController, :export
|
||||
post "/members/export.pdf", MemberPdfExportController, :export
|
||||
post "/set_locale", LocaleController, :set_locale
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue