Skip to content

Commit

Permalink
Split the report into multiple tables
Browse files Browse the repository at this point in the history
  • Loading branch information
crismali committed Jan 13, 2025
1 parent 0ce573c commit e65f5bf
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 132 deletions.
47 changes: 29 additions & 18 deletions app/controllers/admin/reports/monthly_activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,43 @@ module Admin
module Reports
class MonthlyActivitiesController < BaseController
def index
fetch_monthly_activities
@monthly_activities = fetch_activities
end

def fetch_monthly_activities
@monthly_activities = {}
private

# TODO: load_async in Rails 7
appointments = MonthlyAppointment.all
loans = MonthlyLoan.all
members = MonthlyMember.all
renewals = MonthlyRenewal.all
def fetch_activities
records = [*MonthlyAppointment.all, *MonthlyLoan.all, *MonthlyMember.all, *MonthlyRenewal.all]

assign_monthlies(appointments, %i[appointments_count completed_appointments_count])
assign_monthlies(loans, %i[loans_count active_members_count])
assign_monthlies(members, %i[new_members_count pending_members_count])
assign_monthlies(renewals, %i[renewals_count])
records.group_by(&:year).each_with_object([]) do |(year, records_for_year), grouped_year|
monthly_values = records_for_year.group_by(&:month).each_with_object([]) do |(month, records_for_month), grouped_month|
grouped_month << [month, records_to_amount_hash(records_for_month)]
end

@monthly_activities = @monthly_activities.sort.to_h
grouped_year << [year, monthly_values.sort_by(&:first)]
end.sort_by(&:first)
end

def assign_monthlies(records, columns)
records.each do |record|
key = "#{record.year}-#{record.month.to_s.rjust(2, "0")}"
monthly = @monthly_activities[key] ||= Hash.new(0)
def columns_for_record(record)
case record
when MonthlyAppointment
%i[appointments_count completed_appointments_count]
when MonthlyLoan
%i[loans_count active_members_count]
when MonthlyMember
%i[new_members_count pending_members_count]
when MonthlyRenewal
%i[renewals_count]
else
raise "Unknow record type: #{record}"
end
end

columns.each { |column| monthly[column] = record[column] }
def records_to_amount_hash(records)
records.each_with_object(Hash.new(0)) do |record, hash|
columns_for_record(record).each do |column|
hash[column] = record[column]
end
end
end
end
Expand Down
126 changes: 80 additions & 46 deletions app/views/admin/reports/monthly_activities/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,83 @@
<%= index_header "Activity" %>
<% end %>

<table class="table monthly-adjustments">
<thead>
<tr>
<th></th>
<th class="left-lined" colspan="2">Activity</th>
<th class="left-lined" colspan="2">Members</th>
<th class="left-lined" colspan="2">Appointments</th>
</tr>
<tr>
<th>Month</th>
<th class="left-lined">Loans</th>
<th class="left-lined">Renewals</th>
<th>Members</th>
<th class="left-lined">New</th>
<th>Pending</th>
<th class="left-lined">Scheduled</th>
<th>Completed</th>
</tr>
</thead>
<tbody>
<% @monthly_activities.each do |date, activities| %>
<tr>
<% year, month = date.split("-") %>
<td><%= Date::MONTHNAMES[month.to_i] %> <%= year %></td>
<td class="left-lined"><%= activities[:loans_count] %></td>
<td class="left-lined"><%= activities[:renewals_count] %></td>
<td class="left-lined"><%= activities[:active_members_count] %></td>
<td class="left-lined"><%= activities[:new_members_count] %></td>
<td class="left-lined"><%= activities[:pending_members_count] %></td>
<td class="left-lined"><%= activities[:appointments_count] %></td>
<td class="left-lined"><%= activities[:completed_appointments_count] %></td>
</tr>
<% end %>
<tfoot>
<tr>
<td>Total</td>
<td class="left-lined"><%= @monthly_activities.values.sum { |a| a[:loans_count] } %></td>
<td class="left-lined"><%= @monthly_activities.values.sum { |a| a[:renewals_count] } %></td>
<td class="left-lined"><%= @monthly_activities.values.sum { |a| a[:active_members_count] } %></td>
<td class="left-lined"><%= @monthly_activities.values.sum { |a| a[:new_members_count] } %></td>
<td class="left-lined"><%= @monthly_activities.values.sum { |a| a[:pending_members_count] } %></td>
<td class="left-lined"><%= @monthly_activities.values.sum { |a| a[:appointments_count] } %></td>
<td class="left-lined"><%= @monthly_activities.values.sum { |a| a[:completed_appointments_count] } %></td>
</tr>
</tfoot>
</table>
<% @monthly_activities.each do |(year, activities_by_month)| %>
<div id="year-<%= year %>">
<h2><%= year -%></h2>
<table class="table monthly-adjustments">
<thead>
<tr>
<th></th>
<th class="left-lined" colspan="2">Activity</th>
<th class="left-lined" colspan="2">Members</th>
<th class="left-lined" colspan="2">Appointments</th>
</tr>
<tr>
<th>Month</th>
<th class="left-lined">Loans</th>
<th class="left-lined">Renewals</th>
<th>Members</th>
<th class="left-lined">New</th>
<th>Pending</th>
<th class="left-lined">Scheduled</th>
<th>Completed</th>
</tr>
</thead>
<tbody>
<% activities_by_month.each do |month, activities| %>
<tr>
<td class="month"><%= Date::MONTHNAMES[month.to_i] %></td>
<td class="left-lined <%= "loans_count-#{year}-#{month}" %>">
<%= activities[:loans_count] %>
</td>
<td class="left-lined <%= "renewals_count-#{year}-#{month}" %>">
<%= activities[:renewals_count] %>
</td>
<td class="left-lined <%= "active_members_count-#{year}-#{month}" %>">
<%= activities[:active_members_count] %>
</td>
<td class="left-lined <%= "new_members_count-#{year}-#{month}" %>">
<%= activities[:new_members_count] %>
</td>
<td class="left-lined <%= "pending_members_count-#{year}-#{month}" %>">
<%= activities[:pending_members_count] %>
</td>
<td class="left-lined <%= "appointments_count-#{year}-#{month}" %>">
<%= activities[:appointments_count] %>
</td>
<td class="left-lined <%= "completed_appointments_count-#{year}-#{month}" %>">
<%= activities[:completed_appointments_count] %>
</td>
</tr>
<% end %>
<tfoot>
<tr>
<td class="total">Total</td>
<td class="left-lined <%= "loans_count-#{year}" %>">
<%= activities_by_month.map(&:second).sum { |a| a[:loans_count] } %>
</td>
<td class="left-lined <%= "renewals_count-#{year}" %>">
<%= activities_by_month.map(&:second).sum { |a| a[:renewals_count] } %>
</td>
<td class="left-lined <%= "active_members_count-#{year}" %>">
<%= activities_by_month.map(&:second).sum { |a| a[:active_members_count] } %>
</td>
<td class="left-lined <%= "new_members_count-#{year}" %>">
<%= activities_by_month.map(&:second).sum { |a| a[:new_members_count] } %>
</td>
<td class="left-lined <%= "pending_members_count-#{year}" %>">
<%= activities_by_month.map(&:second).sum { |a| a[:pending_members_count] } %>
</td>
<td class="left-lined <%= "appointments_count-#{year}" %>">
<%= activities_by_month.map(&:second).sum { |a| a[:appointments_count] } %>
</td>
<td class="left-lined <%= "completed_appointments_count-#{year}" %>">
<%= activities_by_month.map(&:second).sum { |a| a[:completed_appointments_count] } %>
</td>
</tr>
</tfoot>
</table>
<br>
<br>
</div>
<% end %>
Loading

0 comments on commit e65f5bf

Please sign in to comment.