Module: Modules::Owners::Reports::DineInReport

Included in:
Modules::Owners::Reports
Defined in:
app/my_lib/modules/owners/reports/dine_in_report.rb

Instance Method Summary collapse

Instance Method Details

#build_reports_by_dayObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/my_lib/modules/owners/reports/dine_in_report.rb', line 39

def build_reports_by_day
  reservations = reservations_filter_by('day')
  # rubocop:disable Layout/LineLength
  cache_key = "#{self.class}:build_reports_by_day:#{restaurants_cache_key}:#{reservations.cache_key}:#{I18n.locale}:#{report_params[:start_date]}:#{report_params[:end_date]}"
  # rubocop:enable Layout/LineLength
  Rails.cache.fetch(cache_key) do
    reservations_group = reservations.group_by(&:date_format)
    dates = (report_params[:start_date].to_date..report_params[:end_date].to_date).to_a
    row_data = dates.map do |date|
      date_format = date.strftime('%d/%m/%Y')
      {
        date: date.strftime('%d-%b'),
        arrived: total_and_cover(reservations_group[date_format]&.select do |r|
                                   r.reached_goal? && r.is_past?
                                 end),
        pending: total_and_cover(reservations_group[date_format]&.select { |r| r.pending? || r.upcoming? }),
        rejected: total_and_cover(reservations_group[date_format]&.select { |r| r.rejected? }),
        no_show: total_and_cover(reservations_group[date_format]&.select { |r| r.no_show? }),
        cancel: total_and_cover(reservations_group[date_format]&.select { |r| !r.active }),
        total_booking: total_and_cover(reservations_group[date_format])
      }
    end
    total_data = {
      arrived: total_and_cover(reservations&.select { |r| r.reached_goal? && r.is_past? }),
      pending: total_and_cover(reservations&.select { |r| r.pending? || r.upcoming? }),
      rejected: total_and_cover(reservations&.select { |r| r.rejected? }),
      no_show: total_and_cover(reservations&.select { |r| r.no_show? }),
      cancel: total_and_cover(reservations&.select { |r| !r.active }),
      total_booking: total_and_cover(reservations)
    }

    [row_data, total_data]
  end
end

#build_reports_by_hourObject



5
6
7
8
9
10
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
# File 'app/my_lib/modules/owners/reports/dine_in_report.rb', line 5

def build_reports_by_hour
  reservations = active_reservations_filter_by('hour')
  # rubocop:disable Layout/LineLength
  cache_key = "#{self.class}:build_reports_by_hour:#{restaurants_cache_key}:#{reservations.cache_key}:#{report_params[:start_time]}:#{report_params[:end_time]}:#{I18n.locale}"
  # rubocop:enable Layout/LineLength
  Rails.cache.fetch(cache_key) do
    reservations_group = reservations.group_by(&:start_time_format)

    hours = reservations_group.keys.sort
    total_seat_available = 0
    data = hours.map do |hour|
      total_seat_available += calculate_seat_available(restaurant, single_date_params, hour).to_i
      {
        hour: hour,
        count: reservations_group[hour].select { |r| r.service_type == 'dine_in' }.size,
        orders: reservations_group[hour].reject { |r| r.service_type != 'dine_in' }.size,
        adult: reservations_group[hour].sum(&:adult),
        kids: reservations_group[hour].sum(&:kids),
        seat_available: calculate_seat_available(restaurant, single_date_params, hour),
        covers: reservations_group[hour].sum(&:party_size)
      }
    end
    total_data = {
      count: reservations.select { |r| r.service_type == 'dine_in' }.size,
      orders: reservations.reject { |r| r.service_type != 'dine_in' }.size,
      adult: reservations.sum(&:adult),
      kids: reservations.sum(&:kids),
      seat_available: total_seat_available,
      covers: reservations.sum(&:party_size)
    }
    [data, total_data]
  end
end