Class: Crm::Scheduler::DashboardController

Inherits:
CrmController
  • Object
show all
Defined in:
app/controllers/crm/scheduler/dashboard_controller.rb

Overview

Scheduler admin dashboard: booking counts/status breakdown for a period
(week or month), plus per-profile, per-page, and per-employee rollups.

Instance Method Summary collapse

Instance Method Details

#indexvoid

This method returns an undefined value.

Builds the dashboard stats for params[:period] ("week" or "month",
default "month"): overall booking counts by status, active scheduling
profiles, booking pages, and a per-employee breakdown.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/crm/scheduler/dashboard_controller.rb', line 11

def index
  @period = params[:period].presence || 'month'
  @date_range = date_range_for(@period)

  bookings_scope = @date_range ? SchedulerBooking.where(starts_at: @date_range) : SchedulerBooking.all

  @stats = {
    total: bookings_scope.count,
    confirmed: bookings_scope.confirmed.count,
    cancelled: bookings_scope.cancelled.count,
    completed: bookings_scope.where(status: 'completed').count
  }

  @active_profiles = SchedulerProfile.where(scheduler_enabled: true)
                                     .includes(employee: :employee_record)
                                     .order('parties.full_name')

  @booking_pages = SchedulerBookingPage.includes(:scheduler_hosts)
                                       .order(:name)

  @booking_page_counts = bookings_scope.group(:scheduler_booking_page_id).count

  employee_status_counts = bookings_scope
                           .group(:employee_id, :status)
                           .count

  @per_employee = employee_status_counts
                  .each_with_object({}) do |((eid, status), count), hash|
                    hash[eid] ||= { confirmed: 0, cancelled: 0, completed: 0, total: 0 }
                    hash[eid][status.to_sym] = count
                    hash[eid][:total] += count
                  end
                  .sort_by { |_eid, data| -data[:total] }

  employee_ids = @per_employee.map(&:first)
  @employees_by_id = Employee.where(id: employee_ids).index_by(&:id)
end