|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright (c) 2026, Schweizer Wanderwege. This file is part of |
| 4 | +# hitobito_sww and licensed under the Affero General Public License version 3 |
| 5 | +# or later. See the COPYING file at the top-level directory or at |
| 6 | +# https://github.com/hitobito/hitobito_sww |
| 7 | + |
| 8 | +module Sww::Group::Statistics |
| 9 | + class Memberships < ::Group::Statistics::Base |
| 10 | + self.key = :memberships |
| 11 | + self.layer_only = false |
| 12 | + |
| 13 | + include DateRangeFilter |
| 14 | + |
| 15 | + TotalRow = Data.define(:entries, :exits, :net, :count) do |
| 16 | + def blank? = entries.zero? && exits.zero? && count.zero? |
| 17 | + end |
| 18 | + RoleRow = Data.define(:label, :entries, :exits, :net, :count) |
| 19 | + GroupBreakdown = Data.define(:title, :role_rows, :total_row) |
| 20 | + |
| 21 | + def total_entries |
| 22 | + @total_entries ||= entries_scope.where(group_id: groups.map(&:id)).count |
| 23 | + end |
| 24 | + |
| 25 | + def total_exits |
| 26 | + @total_exits ||= exits_scope.where(group_id: groups.map(&:id)).count |
| 27 | + end |
| 28 | + |
| 29 | + def net_change |
| 30 | + total_entries - total_exits |
| 31 | + end |
| 32 | + |
| 33 | + def total_count |
| 34 | + @total_count ||= actives_scope.where(group_id: groups.map(&:id)).count |
| 35 | + end |
| 36 | + |
| 37 | + def group_breakdowns |
| 38 | + @group_breakdowns ||= groups.map { |group| build_group_breakdown(group) } |
| 39 | + .reject { |breakdown| breakdown.total_row.blank? } |
| 40 | + end |
| 41 | + |
| 42 | + private |
| 43 | + |
| 44 | + def build_group_breakdown(group) |
| 45 | + title = breadcrumb_title(group) |
| 46 | + role_rows = build_role_rows(group) |
| 47 | + total_row = TotalRow.new( |
| 48 | + entries: role_rows.sum(&:entries), |
| 49 | + exits: role_rows.sum(&:exits), |
| 50 | + net: role_rows.sum(&:net), |
| 51 | + count: role_rows.sum(&:count) |
| 52 | + ) |
| 53 | + GroupBreakdown.new(title:, role_rows:, total_row:) |
| 54 | + end |
| 55 | + |
| 56 | + def breadcrumb_title(current_group) |
| 57 | + if current_group == group |
| 58 | + "#{group} #{I18n.t("group.statistics.memberships.layer_only_suffix")}" |
| 59 | + else |
| 60 | + current_group.local_hierarchy[1..].map(&:to_s).join(" → ") |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + def build_role_rows(current_group) |
| 65 | + role_types_in(current_group.id).map do |role_type| |
| 66 | + build_role_row(current_group.id, role_type) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def role_types_in(group_id) |
| 71 | + keys = entries_by_group_and_type.keys + |
| 72 | + exits_by_group_and_type.keys + |
| 73 | + actives_by_group_and_type.keys |
| 74 | + |
| 75 | + keys.filter_map { |id, role_type| role_type if id == group_id }.uniq |
| 76 | + end |
| 77 | + |
| 78 | + def build_role_row(group_id, type) |
| 79 | + entries = entries_by_group_and_type.fetch([group_id, type], 0) |
| 80 | + exits = exits_by_group_and_type.fetch([group_id, type], 0) |
| 81 | + net = entries - exits |
| 82 | + count = actives_by_group_and_type.fetch([group_id, type], 0) |
| 83 | + RoleRow.new(label: type.constantize.label, entries:, exits:, net:, count:) |
| 84 | + end |
| 85 | + |
| 86 | + def groups |
| 87 | + @groups ||= group.self_and_descendants.where(layer_group_id: layer.id).order(:lft).to_a |
| 88 | + end |
| 89 | + |
| 90 | + def entries_by_group_and_type |
| 91 | + @entries_by_group_and_type ||= entries_scope.group(:group_id, :type).count |
| 92 | + end |
| 93 | + |
| 94 | + def exits_by_group_and_type |
| 95 | + @exits_by_group_and_type ||= exits_scope.group(:group_id, :type).count |
| 96 | + end |
| 97 | + |
| 98 | + def actives_by_group_and_type |
| 99 | + @actives_by_group_and_type ||= actives_scope.group(:group_id, :type).count |
| 100 | + end |
| 101 | + |
| 102 | + def entries_scope |
| 103 | + ::Role.with_inactive.where(start_on: from_date..to_date) |
| 104 | + end |
| 105 | + |
| 106 | + def exits_scope |
| 107 | + ::Role.with_inactive.where(end_on: from_date..to_date) |
| 108 | + end |
| 109 | + |
| 110 | + def actives_scope |
| 111 | + ::Role.active(to_date) |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments