Merge remote-tracking branch 'origin/main' into feature/member-overview-groups
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/promote/production Build is passing

This commit is contained in:
Simon 2026-02-16 15:57:57 +01:00
commit 6831ba046f
Signed by: simon
GPG key ID: 40E7A58C4AA1EDB2
48 changed files with 3516 additions and 182 deletions

View file

@ -178,7 +178,9 @@ defmodule Mv.Authorization.PermissionSets do
# Groups overview
"/groups",
# Group detail
"/groups/:slug"
"/groups/:slug",
# Statistics
"/statistics"
]
}
end
@ -243,7 +245,9 @@ defmodule Mv.Authorization.PermissionSets do
# Group detail
"/groups/:slug",
# Edit group
"/groups/:slug/edit"
"/groups/:slug/edit",
# Statistics
"/statistics"
]
}
end

View file

@ -116,4 +116,30 @@ defmodule Mv.Config do
defp parse_and_validate_integer(_value, default) do
default
end
@doc """
Returns the maximum number of rows allowed in PDF exports.
Reads the `row_limit` value from the PDF export configuration.
## Returns
- Maximum number of rows (default: 5000)
## Examples
iex> Mv.Config.pdf_export_row_limit()
5000
"""
@spec pdf_export_row_limit() :: pos_integer()
def pdf_export_row_limit do
get_pdf_export_config(:row_limit, 5000)
end
# Helper function to get PDF export config values
defp get_pdf_export_config(key, default) do
Application.get_env(:mv, :pdf_export, [])
|> Keyword.get(key, default)
|> parse_and_validate_integer(default)
end
end

View file

@ -0,0 +1,433 @@
defmodule Mv.Membership.MemberExport.Build do
@moduledoc """
Builds export data structure for member exports (CSV/PDF).
Extracts common logic for loading, filtering, sorting, and formatting member data
into a unified structure that can be used by both CSV and PDF exporters.
Returns a structure:
```
%{
columns: [%{key: term(), kind: :member_field | :custom_field | :computed, ...}],
rows: [[cell_string, ...]],
meta: %{generated_at: String.t(), member_count: integer(), ...}
}
```
No translations/Gettext in this module - labels come from the web layer via a function.
"""
require Ash.Query
import Ash.Expr
alias Mv.Membership.{CustomField, CustomFieldValueFormatter, Member, MemberExportSort}
alias MvWeb.MemberLive.Index.MembershipFeeStatus
@custom_field_prefix Mv.Constants.custom_field_prefix()
@doc """
Builds export data structure from parsed parameters.
- `actor` - Ash actor (e.g. current user)
- `parsed` - Map with export parameters (from `MemberExport.parse_params/1`)
- `label_fn` - Function to get labels for columns: `(key) -> String.t()`
Returns `{:ok, data}` or `{:error, :forbidden}`.
The `data` map contains:
- `columns`: List of column specs with `key`, `kind`, and optional `custom_field`
- `rows`: List of rows, each row is a list of cell strings
- `meta`: Metadata including `generated_at` and `member_count`
"""
@spec build(struct(), map(), (term() -> String.t())) ::
{:ok, map()} | {:error, :forbidden}
def build(actor, parsed, label_fn) when is_function(label_fn, 1) do
# Ensure sort custom field is loaded if needed
parsed = ensure_sort_custom_field_loaded(parsed)
custom_field_ids_union =
(parsed.custom_field_ids ++ Map.keys(parsed.boolean_filters || %{})) |> Enum.uniq()
with {:ok, custom_fields_by_id} <- load_custom_fields_by_id(custom_field_ids_union, actor),
{:ok, members} <- load_members(actor, parsed, custom_fields_by_id) do
columns = build_columns(parsed, custom_fields_by_id, label_fn)
rows = build_rows(members, columns, custom_fields_by_id)
meta = build_meta(members)
{:ok, %{columns: columns, rows: rows, meta: meta}}
end
end
defp ensure_sort_custom_field_loaded(%{custom_field_ids: ids, sort_field: sort_field} = parsed) do
case extract_sort_custom_field_id(sort_field) do
nil -> parsed
id -> %{parsed | custom_field_ids: Enum.uniq([id | ids])}
end
end
defp extract_sort_custom_field_id(field) when is_binary(field) do
if String.starts_with?(field, @custom_field_prefix) do
String.trim_leading(field, @custom_field_prefix)
else
nil
end
end
defp extract_sort_custom_field_id(_), do: nil
defp load_custom_fields_by_id([], _actor), do: {:ok, %{}}
defp load_custom_fields_by_id(custom_field_ids, actor) do
query =
CustomField
|> Ash.Query.filter(expr(id in ^custom_field_ids))
|> Ash.Query.select([:id, :name, :value_type])
case Ash.read(query, actor: actor) do
{:ok, custom_fields} ->
by_id = build_custom_fields_by_id(custom_field_ids, custom_fields)
{:ok, by_id}
{:error, %Ash.Error.Forbidden{}} ->
{:error, :forbidden}
end
end
defp build_custom_fields_by_id(custom_field_ids, custom_fields) do
Enum.reduce(custom_field_ids, %{}, fn id, acc ->
find_and_add_custom_field(acc, id, custom_fields)
end)
end
defp find_and_add_custom_field(acc, id, custom_fields) do
case Enum.find(custom_fields, fn cf -> to_string(cf.id) == to_string(id) end) do
nil -> acc
cf -> Map.put(acc, id, cf)
end
end
defp load_members(actor, parsed, custom_fields_by_id) do
{query, sort_after_load} = build_members_query(parsed, custom_fields_by_id)
case Ash.read(query, actor: actor) do
{:ok, members} ->
processed_members =
process_loaded_members(members, parsed, custom_fields_by_id, sort_after_load)
{:ok, processed_members}
{:error, %Ash.Error.Forbidden{}} ->
{:error, :forbidden}
end
end
defp build_members_query(parsed, _custom_fields_by_id) do
select_fields =
[:id] ++ Enum.map(parsed.selectable_member_fields, &String.to_existing_atom/1)
custom_field_ids_union = parsed.custom_field_ids ++ Map.keys(parsed.boolean_filters || %{})
need_cycles =
parsed.show_current_cycle or parsed.cycle_status_filter != nil or
parsed.computed_fields != [] or
"membership_fee_status" in parsed.member_fields
query =
Member
|> Ash.Query.new()
|> Ash.Query.select(select_fields)
|> load_custom_field_values_query(custom_field_ids_union)
|> maybe_load_cycles(need_cycles, parsed.show_current_cycle)
query =
if parsed.selected_ids != [] do
Ash.Query.filter(query, expr(id in ^parsed.selected_ids))
else
apply_search(query, parsed.query)
end
# Apply sorting at query level if possible (not custom fields)
maybe_sort(query, parsed.sort_field, parsed.sort_order)
end
defp process_loaded_members(members, parsed, custom_fields_by_id, sort_after_load) do
members
|> apply_post_load_filters(parsed, custom_fields_by_id)
|> apply_post_load_sorting(parsed, custom_fields_by_id, sort_after_load)
|> add_computed_fields(parsed.computed_fields, parsed.show_current_cycle)
end
defp apply_post_load_filters(members, parsed, custom_fields_by_id) do
if parsed.selected_ids == [] do
members
|> apply_cycle_status_filter(parsed.cycle_status_filter, parsed.show_current_cycle)
|> MvWeb.MemberLive.Index.apply_boolean_custom_field_filters(
parsed.boolean_filters || %{},
Map.values(custom_fields_by_id)
)
else
members
end
end
defp apply_post_load_sorting(members, parsed, custom_fields_by_id, sort_after_load) do
# Sort after load for custom fields (always, even with selected_ids)
if sort_after_load do
sort_members_by_custom_field(
members,
parsed.sort_field,
parsed.sort_order,
Map.values(custom_fields_by_id)
)
else
# For selected_ids, we may need to apply sorting that wasn't done at query level
if (parsed.selected_ids != [] and parsed.sort_field) && parsed.sort_order do
# Re-sort in memory to ensure consistent ordering
sort_members_in_memory(members, parsed.sort_field, parsed.sort_order)
else
members
end
end
end
defp sort_members_in_memory(members, field, order) when is_binary(field) do
field_atom = String.to_existing_atom(field)
if field_atom in Mv.Constants.member_fields() do
sort_by_field(members, field_atom, order)
else
members
end
rescue
ArgumentError -> members
end
defp sort_members_in_memory(members, _field, _order), do: members
defp sort_by_field(members, field_atom, order) do
key_fn = fn member -> Map.get(member, field_atom) end
compare_fn = build_compare_fn(order)
Enum.sort_by(members, key_fn, compare_fn)
end
defp build_compare_fn("asc"), do: fn a, b -> a <= b end
defp build_compare_fn("desc"), do: fn a, b -> b <= a end
defp build_compare_fn(_), do: fn _a, _b -> true end
defp load_custom_field_values_query(query, []), do: query
defp load_custom_field_values_query(query, custom_field_ids) do
cfv_query =
Mv.Membership.CustomFieldValue
|> Ash.Query.filter(expr(custom_field_id in ^custom_field_ids))
|> Ash.Query.load(custom_field: [:id, :name, :value_type])
Ash.Query.load(query, custom_field_values: cfv_query)
end
defp apply_search(query, nil), do: query
defp apply_search(query, ""), do: query
defp apply_search(query, q) when is_binary(q) do
if String.trim(q) != "" do
Member.fuzzy_search(query, %{query: q})
else
query
end
end
defp maybe_sort(query, nil, _order), do: {query, false}
defp maybe_sort(query, _field, nil), do: {query, false}
defp maybe_sort(query, field, order) when is_binary(field) do
if custom_field_sort?(field) do
{query, true}
else
field_atom = String.to_existing_atom(field)
if field_atom in (Mv.Constants.member_fields() -- [:notes]) do
{Ash.Query.sort(query, [{field_atom, String.to_existing_atom(order)}]), false}
else
{query, false}
end
end
rescue
ArgumentError -> {query, false}
end
defp sort_members_by_custom_field(members, _field, _order, _custom_fields) when members == [],
do: []
defp sort_members_by_custom_field(members, field, order, custom_fields) when is_binary(field) do
id_str = String.trim_leading(field, @custom_field_prefix)
custom_field = Enum.find(custom_fields, fn cf -> to_string(cf.id) == id_str end)
if is_nil(custom_field), do: members
key_fn = fn member ->
cfv = find_cfv(member, custom_field)
raw = if cfv, do: cfv.value, else: nil
MemberExportSort.custom_field_sort_key(custom_field.value_type, raw)
end
members
|> Enum.map(fn m -> {m, key_fn.(m)} end)
|> Enum.sort(fn {_, ka}, {_, kb} -> MemberExportSort.key_lt(ka, kb, order) end)
|> Enum.map(fn {m, _} -> m end)
end
defp find_cfv(member, custom_field) do
(member.custom_field_values || [])
|> Enum.find(fn cfv ->
to_string(cfv.custom_field_id) == to_string(custom_field.id) or
(Map.get(cfv, :custom_field) &&
to_string(cfv.custom_field.id) == to_string(custom_field.id))
end)
end
defp custom_field_sort?(field), do: String.starts_with?(field, @custom_field_prefix)
defp maybe_load_cycles(query, false, _show_current), do: query
defp maybe_load_cycles(query, true, show_current) do
MembershipFeeStatus.load_cycles_for_members(query, show_current)
end
defp apply_cycle_status_filter(members, nil, _show_current), do: members
defp apply_cycle_status_filter(members, status, show_current) when status in [:paid, :unpaid] do
MembershipFeeStatus.filter_members_by_cycle_status(members, status, show_current)
end
defp apply_cycle_status_filter(members, _status, _show_current), do: members
defp add_computed_fields(members, computed_fields, show_current_cycle) do
computed_fields = computed_fields || []
if "membership_fee_status" in computed_fields do
Enum.map(members, fn member ->
status = MembershipFeeStatus.get_cycle_status_for_member(member, show_current_cycle)
# Format as string for export (controller will handle translation)
status_string = format_membership_fee_status(status)
Map.put(member, :membership_fee_status, status_string)
end)
else
members
end
end
defp format_membership_fee_status(:paid), do: "paid"
defp format_membership_fee_status(:unpaid), do: "unpaid"
defp format_membership_fee_status(:suspended), do: "suspended"
defp format_membership_fee_status(nil), do: ""
defp build_columns(parsed, custom_fields_by_id, label_fn) do
member_cols =
Enum.map(parsed.selectable_member_fields, fn field ->
%{
key: field,
kind: :member_field,
label: label_fn.(field)
}
end)
computed_cols =
Enum.map(parsed.computed_fields, fn key ->
atom_key = String.to_existing_atom(key)
%{
key: atom_key,
kind: :computed,
label: label_fn.(atom_key)
}
end)
custom_cols =
parsed.custom_field_ids
|> Enum.map(fn id ->
cf = Map.get(custom_fields_by_id, id) || Map.get(custom_fields_by_id, to_string(id))
if cf do
%{
key: to_string(id),
kind: :custom_field,
label: cf.name,
custom_field: cf
}
else
nil
end
end)
|> Enum.reject(&is_nil/1)
member_cols ++ computed_cols ++ custom_cols
end
defp build_rows(members, columns, custom_fields_by_id) do
Enum.map(members, fn member ->
Enum.map(columns, fn col -> cell_value(member, col, custom_fields_by_id) end)
end)
end
defp cell_value(member, %{kind: :member_field, key: key}, _custom_fields_by_id) do
key_atom = key_to_atom(key)
value = Map.get(member, key_atom)
format_member_value(value)
end
defp cell_value(member, %{kind: :custom_field, key: id, custom_field: cf}, _custom_fields_by_id) do
cfv = get_cfv_by_id(member, id)
if cfv do
CustomFieldValueFormatter.format_custom_field_value(cfv.value, cf)
else
""
end
end
defp cell_value(member, %{kind: :computed, key: key}, _custom_fields_by_id) do
value = Map.get(member, key)
if is_binary(value), do: value, else: ""
end
defp key_to_atom(k) when is_atom(k), do: k
defp key_to_atom(k) when is_binary(k) do
try do
String.to_existing_atom(k)
rescue
ArgumentError -> k
end
end
defp get_cfv_by_id(member, id) do
values =
case Map.get(member, :custom_field_values) do
v when is_list(v) -> v
_ -> []
end
id_str = to_string(id)
Enum.find(values, fn cfv ->
to_string(cfv.custom_field_id) == id_str or
(Map.get(cfv, :custom_field) && to_string(cfv.custom_field.id) == id_str)
end)
end
defp format_member_value(nil), do: ""
defp format_member_value(true), do: "true"
defp format_member_value(false), do: "false"
defp format_member_value(%Date{} = d), do: Date.to_iso8601(d)
defp format_member_value(%DateTime{} = dt), do: DateTime.to_iso8601(dt)
defp format_member_value(%NaiveDateTime{} = dt), do: NaiveDateTime.to_iso8601(dt)
defp format_member_value(value), do: to_string(value)
defp build_meta(members) do
%{
generated_at: DateTime.utc_now() |> DateTime.to_iso8601(),
member_count: length(members)
}
end
end

View file

@ -0,0 +1,456 @@
defmodule Mv.Membership.MembersPDF do
@moduledoc """
Exports members to PDF using Typst templates and Imprintor.
Uses the same data structure as `MemberExport.Build` and converts it
to the format expected by the Typst template. Handles internationalization
for PDF-specific labels (title, metadata) and membership fee status.
Ensures deterministic output by maintaining column and row order.
Creates a temporary directory per request and copies the template there
to avoid symlink issues and ensure isolation.
"""
require Logger
use Gettext, backend: MvWeb.Gettext
alias Mv.Config
@template_filename "members_export.typ"
@doc """
Renders export data to PDF binary.
- `export_data` - Map from `MemberExport.Build.build/3` with `columns`, `rows`, and `meta`
- `opts` - Keyword list with `:locale` (default: "en") and `:club_name` (default: "Club")
Returns `{:ok, binary}` where binary is the PDF content, or `{:error, term}`.
The PDF binary starts with "%PDF" (PDF magic bytes).
Validates row count against configured limit before processing.
"""
@spec render(map(), keyword()) :: {:ok, binary()} | {:error, term()}
def render(export_data, opts \\ []) do
row_count = length(export_data.rows)
max_rows = Config.pdf_export_row_limit()
if row_count > max_rows do
Logger.warning(
"PDF export rejected: row count exceeds limit (rows: #{row_count}, max: #{max_rows})",
error_type: :row_limit_exceeded
)
{:error, {:row_limit_exceeded, row_count, max_rows}}
else
Logger.info(
"Starting PDF export (rows: #{row_count}, columns: #{length(export_data.columns)})"
)
locale = Keyword.get(opts, :locale, "en")
club_name = Keyword.get(opts, :club_name, "Club")
create_and_use_temp_directory(export_data, locale, club_name)
end
end
defp create_and_use_temp_directory(export_data, locale, club_name) do
case create_temp_directory() do
{:ok, temp_dir} ->
try do
with {:ok, template_content} <- load_template(),
{:ok, _template_path} <- copy_template_to_temp(temp_dir, template_content),
{:ok, template_data} <-
convert_to_template_format(export_data, locale, club_name),
{:ok, config} <-
build_imprintor_config(template_content, template_data, temp_dir),
{:ok, pdf_binary} <- compile_to_pdf(config) do
Logger.info("PDF export completed successfully (rows: #{length(export_data.rows)})")
{:ok, pdf_binary}
else
{:error, reason} = error ->
Logger.error("PDF export failed: #{inspect(reason)}",
error_type: :pdf_export_failed
)
error
end
after
cleanup_temp_directory(temp_dir)
end
{:error, reason} = error ->
Logger.error("Failed to create temp directory: #{inspect(reason)}",
error_type: :temp_dir_creation_failed
)
error
end
end
defp create_temp_directory do
# Create unique temp directory per request
temp_base = System.tmp_dir!()
temp_dir = Path.join(temp_base, "mv_pdf_export_#{System.unique_integer([:positive])}")
case File.mkdir_p(temp_dir) do
:ok -> {:ok, temp_dir}
{:error, reason} -> {:error, {:temp_dir_creation_failed, reason}}
end
end
defp load_template do
# Try multiple paths: compiled app path and source path (for tests/dev)
template_paths = [
Path.join(Application.app_dir(:mv, "priv"), "pdf_templates/#{@template_filename}"),
Path.join([File.cwd!(), "priv", "pdf_templates", @template_filename])
]
Enum.reduce_while(template_paths, nil, fn path, _acc ->
case File.read(path) do
{:ok, content} -> {:halt, {:ok, content}}
{:error, _reason} -> {:cont, nil}
end
end)
|> case do
{:ok, content} -> {:ok, content}
nil -> {:error, {:template_not_found, :enoent}}
end
end
defp copy_template_to_temp(temp_dir, template_content) do
# Write template to temp directory (no symlinks, actual file copy)
template_path = Path.join(temp_dir, @template_filename)
case File.write(template_path, template_content) do
:ok -> {:ok, template_path}
{:error, reason} -> {:error, {:template_copy_failed, reason}}
end
end
defp cleanup_temp_directory(temp_dir) do
# Clean up temp directory and all contents
case File.rm_rf(temp_dir) do
{:ok, _} ->
:ok
{:error, reason, _} ->
Logger.warning("Failed to cleanup temp directory: #{temp_dir}, error: #{inspect(reason)}")
end
end
defp convert_to_template_format(export_data, locale, club_name) do
# Set locale for translations
Gettext.put_locale(MvWeb.Gettext, locale)
headers = Enum.map(export_data.columns, & &1.label)
column_count = length(export_data.columns)
meta = Map.get(export_data, :meta) || Map.get(export_data, "meta") || %{}
generated_at_raw =
Map.get(meta, :generated_at) ||
Map.get(meta, "generated_at") ||
DateTime.utc_now() |> DateTime.to_iso8601()
generated_at = format_datetime(generated_at_raw, locale)
member_count =
Map.get(meta, :member_count) ||
Map.get(meta, "member_count") ||
length(export_data.rows)
# Translate membership fee status and format dates in rows
rows =
export_data.rows
|> translate_membership_fee_status_in_rows(export_data.columns)
|> format_dates_in_rows(export_data.columns, locale)
# Build title based on locale
title = build_title(locale, club_name)
# Build translated labels for metadata
created_at_label = gettext("Created at:")
member_count_label = gettext("Member count:")
template_data = %{
"title" => title,
"created_at_label" => created_at_label,
"member_count_label" => member_count_label,
"generated_at" => generated_at,
"column_count" => column_count,
"headers" => headers,
"rows" => rows,
"columns" =>
Enum.map(export_data.columns, fn col ->
%{
"key" => to_string(col.key),
"kind" => to_string(col.kind),
"label" => col.label
}
end),
"meta" => %{
"generated_at" => generated_at,
"member_count" => member_count
},
"locale" => locale
}
{:ok, template_data}
end
defp build_title(_locale, club_name) do
gettext("Member %{club_name}", club_name: club_name)
end
defp format_datetime(iso8601_string, locale) when is_binary(iso8601_string) do
# Try to parse as DateTime first
case DateTime.from_iso8601(iso8601_string) do
{:ok, datetime, _offset} ->
format_datetime(datetime, locale)
{:ok, datetime} ->
format_datetime(datetime, locale)
{:error, _} ->
# Try NaiveDateTime if DateTime parsing fails
case NaiveDateTime.from_iso8601(iso8601_string) do
{:ok, naive_dt} ->
# Convert to DateTime in UTC
datetime = DateTime.from_naive!(naive_dt, "Etc/UTC")
format_datetime(datetime, locale)
{:error, _} ->
# If both fail, return original string
iso8601_string
end
end
end
defp format_datetime(%DateTime{} = datetime, locale) do
# Format as readable date and time (locale-specific)
case locale do
"de" ->
# German format: dd.mm.yyyy - HH:MM Uhr
Calendar.strftime(datetime, "%d.%m.%Y - %H:%M Uhr")
_ ->
# English format: MM/DD/YYYY HH:MM AM/PM
Calendar.strftime(datetime, "%m/%d/%Y %I:%M %p")
end
end
defp format_datetime(_, _), do: ""
defp format_date(%Date{} = date, locale) do
# Format as readable date (locale-specific)
case locale do
"de" ->
# German format: dd.mm.yyyy
Calendar.strftime(date, "%d.%m.%Y")
_ ->
# English format: MM/DD/YYYY
Calendar.strftime(date, "%m/%d/%Y")
end
end
defp format_date(_, _), do: ""
defp format_dates_in_rows(rows, columns, locale) do
date_indices = find_date_column_indices(columns)
if date_indices == [] do
rows
else
format_rows_dates(rows, date_indices, locale)
end
end
defp find_date_column_indices(columns) do
columns
|> Enum.with_index()
|> Enum.filter(fn {col, _idx} -> date_column?(col) end)
|> Enum.map(fn {_col, idx} -> idx end)
end
defp format_rows_dates(rows, date_indices, locale) do
Enum.map(rows, fn row -> format_row_dates(row, date_indices, locale) end)
end
defp format_row_dates(row, date_indices, locale) do
Enum.with_index(row)
|> Enum.map(fn {cell_value, idx} ->
if idx in date_indices do
format_cell_date(cell_value, locale)
else
cell_value
end
end)
end
defp date_column?(%{kind: :member_field, key: key}) do
key_atom = key_to_atom_safe(key)
key_atom in [:join_date, :exit_date, :membership_fee_start_date]
end
defp date_column?(_), do: false
defp key_to_atom_safe(key) when is_binary(key) do
try do
String.to_existing_atom(key)
rescue
ArgumentError -> key
end
end
defp key_to_atom_safe(key), do: key
defp format_cell_date(cell_value, locale) when is_binary(cell_value) do
format_cell_date_iso8601(cell_value, locale)
end
defp format_cell_date(cell_value, _locale), do: cell_value
defp format_cell_date_iso8601(cell_value, locale) do
case Date.from_iso8601(cell_value) do
{:ok, date} -> format_date(date, locale)
_ -> format_cell_date_datetime(cell_value, locale)
end
end
defp format_cell_date_datetime(cell_value, locale) do
case DateTime.from_iso8601(cell_value) do
{:ok, datetime} -> format_datetime(datetime, locale)
_ -> format_cell_date_naive(cell_value, locale)
end
end
defp format_cell_date_naive(cell_value, locale) do
case NaiveDateTime.from_iso8601(cell_value) do
{:ok, naive_dt} ->
datetime = DateTime.from_naive!(naive_dt, "Etc/UTC")
format_datetime(datetime, locale)
_ ->
cell_value
end
end
defp translate_membership_fee_status_in_rows(rows, columns) do
status_col_index = find_membership_fee_status_index(columns)
if status_col_index do
translate_rows_status(rows, status_col_index)
else
rows
end
end
defp find_membership_fee_status_index(columns) do
Enum.find_index(columns, fn col ->
col.kind == :computed && col.key == :membership_fee_status
end)
end
defp translate_rows_status(rows, status_col_index) do
Enum.map(rows, fn row ->
List.update_at(row, status_col_index, &translate_membership_fee_status/1)
end)
end
defp translate_membership_fee_status("paid"), do: gettext("Paid")
defp translate_membership_fee_status("unpaid"), do: gettext("Unpaid")
defp translate_membership_fee_status("suspended"), do: gettext("Suspended")
defp translate_membership_fee_status(value), do: value
defp build_imprintor_config(template_content, template_data, root_directory) do
# Imprintor.Config.new(source_document, inputs, options)
# inputs: %{"elixir_data" => template_data} for sys.inputs.elixir_data in template
# options: set root_directory to temp dir to ensure no symlink issues
# extra_fonts: list of font file paths for Typst to use
extra_fonts = get_extra_fonts()
options = [root_directory: root_directory, extra_fonts: extra_fonts]
config = Imprintor.Config.new(template_content, template_data, options)
{:ok, config}
end
defp get_extra_fonts do
font_paths = get_font_paths()
Enum.reduce_while(font_paths, [], &find_fonts_in_path/2)
|> normalize_fonts_result()
end
defp get_font_paths do
[
Path.join(Application.app_dir(:mv, "priv"), "fonts"),
Path.join([File.cwd!(), "priv", "fonts"])
]
end
defp find_fonts_in_path(base_path, _acc) do
case File.ls(base_path) do
{:ok, files} -> process_font_files(files, base_path)
{:error, _reason} -> {:cont, []}
end
end
defp process_font_files(files, base_path) do
fonts =
files
|> Enum.filter(&String.ends_with?(&1, ".ttf"))
|> Enum.map(&Path.join(base_path, &1))
|> Enum.sort()
if fonts != [] do
{:halt, fonts}
else
{:cont, []}
end
end
defp normalize_fonts_result([]), do: []
defp normalize_fonts_result(fonts), do: fonts
defp compile_to_pdf(config) do
case Imprintor.compile_to_pdf(config) do
{:ok, pdf_binary} when is_binary(pdf_binary) ->
# Verify PDF magic bytes
if String.starts_with?(pdf_binary, "%PDF") do
{:ok, pdf_binary}
else
Logger.error(
"PDF compilation returned invalid format (start: #{String.slice(pdf_binary, 0, 20)})"
)
{:error, :invalid_pdf_format}
end
{:error, reason} ->
Logger.error("PDF compilation failed",
error: inspect(reason),
error_type: :imprintor_compile_error
)
{:error, {:compile_error, reason}}
other ->
Logger.error("PDF compilation returned unexpected result: #{inspect(other)}",
error_type: :unexpected_result
)
{:error, {:unexpected_result, other}}
end
rescue
e ->
Logger.error("PDF compilation raised exception: #{inspect(e)}",
error_type: :compile_exception
)
{:error, {:compile_exception, e}}
end
end

View file

@ -87,7 +87,7 @@ defmodule Mv.MembershipFees.CycleGenerator do
def generate_cycles_for_member(member_or_id, opts \\ [])
def generate_cycles_for_member(member_id, opts) when is_binary(member_id) do
case load_member(member_id) do
case load_member(member_id, opts) do
{:ok, member} -> generate_cycles_for_member(member, opts)
{:error, reason} -> {:error, reason}
end
@ -97,25 +97,25 @@ defmodule Mv.MembershipFees.CycleGenerator do
today = Keyword.get(opts, :today, Date.utc_today())
skip_lock? = Keyword.get(opts, :skip_lock?, false)
do_generate_cycles_with_lock(member, today, skip_lock?)
do_generate_cycles_with_lock(member, today, skip_lock?, opts)
end
# Generate cycles with lock handling
# Returns {:ok, cycles, notifications} - notifications are never sent here,
# they should be returned to the caller (e.g., via after_action hook)
defp do_generate_cycles_with_lock(member, today, true = _skip_lock?) do
# Lock already set by caller (e.g., regenerate_cycles_on_type_change)
defp do_generate_cycles_with_lock(member, today, true = _skip_lock?, opts) do
# Lock already set by caller (e.g., regenerate_cycles_on_type_change or seeds)
# Just generate cycles without additional locking
do_generate_cycles(member, today)
do_generate_cycles(member, today, opts)
end
defp do_generate_cycles_with_lock(member, today, false) do
defp do_generate_cycles_with_lock(member, today, false, opts) do
lock_key = :erlang.phash2(member.id)
Repo.transaction(fn ->
Ecto.Adapters.SQL.query!(Repo, "SELECT pg_advisory_xact_lock($1)", [lock_key])
case do_generate_cycles(member, today) do
case do_generate_cycles(member, today, opts) do
{:ok, cycles, notifications} ->
# Return cycles and notifications - do NOT send notifications here
# They will be sent by the caller (e.g., via after_action hook)
@ -235,25 +235,33 @@ defmodule Mv.MembershipFees.CycleGenerator do
# Private functions
defp load_member(member_id) do
system_actor = SystemActor.get_system_actor()
opts = Helpers.ash_actor_opts(system_actor)
# Use actor from opts when provided (e.g. seeds pass admin); otherwise system actor
defp get_actor(opts) do
case Keyword.get(opts, :actor) do
nil -> SystemActor.get_system_actor()
actor -> actor
end
end
defp load_member(member_id, opts) do
actor = get_actor(opts)
read_opts = Helpers.ash_actor_opts(actor)
query =
Member
|> Ash.Query.filter(id == ^member_id)
|> Ash.Query.load([:membership_fee_type, :membership_fee_cycles])
case Ash.read_one(query, opts) do
case Ash.read_one(query, read_opts) do
{:ok, nil} -> {:error, :member_not_found}
{:ok, member} -> {:ok, member}
{:error, reason} -> {:error, reason}
end
end
defp do_generate_cycles(member, today) do
defp do_generate_cycles(member, today, opts) do
# Reload member with relationships to ensure fresh data
case load_member(member.id) do
case load_member(member.id, opts) do
{:ok, member} ->
cond do
is_nil(member.membership_fee_type_id) ->
@ -263,7 +271,7 @@ defmodule Mv.MembershipFees.CycleGenerator do
{:error, :no_join_date}
true ->
generate_missing_cycles(member, today)
generate_missing_cycles(member, today, opts)
end
{:error, reason} ->
@ -271,7 +279,7 @@ defmodule Mv.MembershipFees.CycleGenerator do
end
end
defp generate_missing_cycles(member, today) do
defp generate_missing_cycles(member, today, opts) do
fee_type = member.membership_fee_type
interval = fee_type.interval
amount = fee_type.amount
@ -287,7 +295,7 @@ defmodule Mv.MembershipFees.CycleGenerator do
# Only generate if start_date <= end_date
if start_date && Date.compare(start_date, end_date) != :gt do
cycle_starts = generate_cycle_starts(start_date, end_date, interval)
create_cycles(cycle_starts, member.id, fee_type.id, amount)
create_cycles(cycle_starts, member.id, fee_type.id, amount, opts)
else
{:ok, [], []}
end
@ -382,9 +390,9 @@ defmodule Mv.MembershipFees.CycleGenerator do
end
end
defp create_cycles(cycle_starts, member_id, fee_type_id, amount) do
system_actor = SystemActor.get_system_actor()
opts = Helpers.ash_actor_opts(system_actor)
defp create_cycles(cycle_starts, member_id, fee_type_id, amount, opts) do
actor = get_actor(opts)
create_opts = Helpers.ash_actor_opts(actor)
# Always use return_notifications?: true to collect notifications
# Notifications will be returned to the caller, who is responsible for
@ -400,7 +408,7 @@ defmodule Mv.MembershipFees.CycleGenerator do
}
handle_cycle_creation_result(
Ash.create(MembershipFeeCycle, attrs, [return_notifications?: true] ++ opts),
Ash.create(MembershipFeeCycle, attrs, [return_notifications?: true] ++ create_opts),
cycle_start
)
end)

237
lib/mv/statistics.ex Normal file
View file

@ -0,0 +1,237 @@
defmodule Mv.Statistics do
@moduledoc """
Aggregated statistics for members and membership fee cycles.
Used by the statistics LiveView to display counts and sums. All functions
accept an `opts` keyword list and pass `:actor` (and `:domain` where needed)
to Ash reads so that policies are enforced.
"""
require Ash.Query
import Ash.Expr
require Logger
alias Mv.Membership.Member
alias Mv.MembershipFees
alias Mv.MembershipFees.MembershipFeeCycle
@doc """
Returns the earliest year in which any member has a join_date.
Used to determine the start of the "relevant" year range for statistics
(from first membership to current year). Returns `nil` if no member has
a join_date.
"""
@spec first_join_year(keyword()) :: non_neg_integer() | nil
def first_join_year(opts) do
query =
Member
|> Ash.Query.filter(expr(not is_nil(join_date)))
|> Ash.Query.sort(join_date: :asc)
|> Ash.Query.limit(1)
case Ash.read_one(query, opts) do
{:ok, nil} ->
nil
{:ok, member} ->
member.join_date.year
{:error, reason} ->
Logger.warning("Statistics.first_join_year failed: #{inspect(reason)}")
nil
end
end
@doc """
Returns the count of active members (exit_date is nil).
"""
@spec active_member_count(keyword()) :: non_neg_integer()
def active_member_count(opts) do
query =
Member
|> Ash.Query.filter(expr(is_nil(exit_date)))
case Ash.count(query, opts) do
{:ok, count} ->
count
{:error, reason} ->
Logger.warning("Statistics.active_member_count failed: #{inspect(reason)}")
0
end
end
@doc """
Returns the count of inactive members (exit_date is not nil).
"""
@spec inactive_member_count(keyword()) :: non_neg_integer()
def inactive_member_count(opts) do
query =
Member
|> Ash.Query.filter(expr(not is_nil(exit_date)))
case Ash.count(query, opts) do
{:ok, count} ->
count
{:error, reason} ->
Logger.warning("Statistics.inactive_member_count failed: #{inspect(reason)}")
0
end
end
@doc """
Returns the count of members who joined in the given year (join_date in that year).
"""
@spec joins_by_year(integer(), keyword()) :: non_neg_integer()
def joins_by_year(year, opts) do
first_day = Date.new!(year, 1, 1)
last_day = Date.new!(year, 12, 31)
query =
Member
|> Ash.Query.filter(expr(join_date >= ^first_day and join_date <= ^last_day))
case Ash.count(query, opts) do
{:ok, count} ->
count
{:error, reason} ->
Logger.warning("Statistics.joins_by_year failed: #{inspect(reason)}")
0
end
end
@doc """
Returns the count of members who exited in the given year (exit_date in that year).
"""
@spec exits_by_year(integer(), keyword()) :: non_neg_integer()
def exits_by_year(year, opts) do
first_day = Date.new!(year, 1, 1)
last_day = Date.new!(year, 12, 31)
query =
Member
|> Ash.Query.filter(expr(exit_date >= ^first_day and exit_date <= ^last_day))
case Ash.count(query, opts) do
{:ok, count} ->
count
{:error, reason} ->
Logger.warning("Statistics.exits_by_year failed: #{inspect(reason)}")
0
end
end
@doc """
Returns totals for membership fee cycles whose cycle_start falls in the given year.
Returns a map with keys: `:total`, `:paid`, `:unpaid`, `:suspended` (each a Decimal sum).
"""
@spec cycle_totals_by_year(integer(), keyword()) :: %{
total: Decimal.t(),
paid: Decimal.t(),
unpaid: Decimal.t(),
suspended: Decimal.t()
}
def cycle_totals_by_year(year, opts) do
first_day = Date.new!(year, 1, 1)
last_day = Date.new!(year, 12, 31)
query =
MembershipFeeCycle
|> Ash.Query.filter(expr(cycle_start >= ^first_day and cycle_start <= ^last_day))
query = maybe_filter_by_fee_type(query, opts)
# Only pass actor and domain to Ash.read; fee_type_id is only for our filter above
opts_for_read =
opts
|> Keyword.drop([:fee_type_id])
|> Keyword.put(:domain, MembershipFees)
case Ash.read(query, opts_for_read) do
{:ok, cycles} ->
cycle_totals_from_cycles(cycles)
{:error, reason} ->
Logger.warning("Statistics.cycle_totals_by_year failed: #{inspect(reason)}")
zero_cycle_totals()
end
end
defp cycle_totals_from_cycles(cycles) do
by_status = Enum.group_by(cycles, & &1.status)
sum = fn status -> sum_amounts(by_status[status] || []) end
total =
[:paid, :unpaid, :suspended]
|> Enum.map(&sum.(&1))
|> Enum.reduce(Decimal.new(0), &Decimal.add/2)
%{
total: total,
paid: sum.(:paid),
unpaid: sum.(:unpaid),
suspended: sum.(:suspended)
}
end
defp sum_amounts(cycles),
do: Enum.reduce(cycles, Decimal.new(0), fn c, acc -> Decimal.add(acc, c.amount) end)
defp zero_cycle_totals do
%{
total: Decimal.new(0),
paid: Decimal.new(0),
unpaid: Decimal.new(0),
suspended: Decimal.new(0)
}
end
defp maybe_filter_by_fee_type(query, opts) do
case Keyword.get(opts, :fee_type_id) do
nil ->
query
id when is_binary(id) ->
# Only apply filter for valid UUID strings (e.g. from form/URL)
if Ecto.UUID.cast(id) != :error do
Ash.Query.filter(query, expr(membership_fee_type_id == ^id))
else
query
end
id ->
Ash.Query.filter(query, expr(membership_fee_type_id == ^id))
end
end
@doc """
Returns the sum of amount for all cycles with status :unpaid.
"""
@spec open_amount_total(keyword()) :: Decimal.t()
def open_amount_total(opts) do
query =
MembershipFeeCycle
|> Ash.Query.filter(expr(status == :unpaid))
query = maybe_filter_by_fee_type(query, opts)
opts_for_read =
opts
|> Keyword.drop([:fee_type_id])
|> Keyword.put(:domain, MembershipFees)
case Ash.read(query, opts_for_read) do
{:ok, cycles} ->
Enum.reduce(cycles, Decimal.new(0), fn c, acc -> Decimal.add(acc, c.amount) end)
{:error, reason} ->
Logger.warning("Statistics.open_amount_total failed: #{inspect(reason)}")
Decimal.new(0)
end
end
end

View file

@ -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

View 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

View file

@ -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"

View 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

View file

@ -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
# -------------------------------------------------------------

View file

@ -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"

View 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

View file

@ -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

View file

@ -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