|
| 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) |
| 16 | + RoleRow = Data.define(:label, :entries, :exits, :net) |
| 17 | + GroupBreakdown = Data.define(:title, :role_rows, :total_row) |
| 18 | + |
| 19 | + def total_entries |
| 20 | + @total_entries ||= entries_scope.where(group_id: groups.map(&:id)).count |
| 21 | + end |
| 22 | + |
| 23 | + def total_exits |
| 24 | + @total_exits ||= exits_scope.where(group_id: groups.map(&:id)).count |
| 25 | + end |
| 26 | + |
| 27 | + def net_change |
| 28 | + total_entries - total_exits |
| 29 | + end |
| 30 | + |
| 31 | + def group_breakdowns |
| 32 | + @group_breakdowns ||= groups.map { |group| build_group_breakdown(group) } |
| 33 | + end |
| 34 | + |
| 35 | + private |
| 36 | + |
| 37 | + def build_group_breakdown(group) |
| 38 | + title = breadcrumb_title(group) |
| 39 | + role_rows = build_role_rows(group) |
| 40 | + total_row = TotalRow.new( |
| 41 | + entries: role_rows.sum(&:entries), |
| 42 | + exits: role_rows.sum(&:exits), |
| 43 | + net: role_rows.sum(&:net) |
| 44 | + ) |
| 45 | + GroupBreakdown.new(title:, role_rows:, total_row:) |
| 46 | + end |
| 47 | + |
| 48 | + def breadcrumb_title(group) |
| 49 | + group.local_hierarchy[1..].map(&:to_s).join(" → ") |
| 50 | + end |
| 51 | + |
| 52 | + def build_role_rows(group) |
| 53 | + role_types_in(group.id).map do |role_type| |
| 54 | + build_role_row(group.id, role_type) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + def role_types_in(group_id) |
| 59 | + (entries_by_group_and_type.keys + exits_by_group_and_type.keys) |
| 60 | + .select { |id, _| id == group_id } |
| 61 | + .map { |_, role_type| role_type } |
| 62 | + .uniq |
| 63 | + end |
| 64 | + |
| 65 | + def build_role_row(group_id, type) |
| 66 | + entries = entries_by_group_and_type.fetch([group_id, type], 0) |
| 67 | + exits = exits_by_group_and_type.fetch([group_id, type], 0) |
| 68 | + net = entries - exits |
| 69 | + RoleRow.new(label: type.constantize.label, entries:, exits:, net:) |
| 70 | + end |
| 71 | + |
| 72 | + def groups |
| 73 | + @groups ||= group.descendants.where(layer_group_id: layer.id).order(:lft).to_a |
| 74 | + end |
| 75 | + |
| 76 | + def entries_by_group_and_type |
| 77 | + @entries_by_group_and_type ||= entries_scope.group(:group_id, :type).count |
| 78 | + end |
| 79 | + |
| 80 | + def exits_by_group_and_type |
| 81 | + @exits_by_group_and_type ||= exits_scope.group(:group_id, :type).count |
| 82 | + end |
| 83 | + |
| 84 | + def entries_scope |
| 85 | + ::Role.with_inactive.where(start_on: from_date..to_date) |
| 86 | + end |
| 87 | + |
| 88 | + def exits_scope |
| 89 | + ::Role.with_inactive.where(end_on: from_date..to_date) |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments