Module: Modules::Owners::Reports::BranchCoversReport

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

Instance Method Summary collapse

Instance Method Details

#build_reports_branch_covers_by_dayObject



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
38
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
73
74
75
# File 'app/my_lib/modules/owners/reports/branch_covers_report.rb', line 5

def build_reports_branch_covers_by_day
  reservations = active_reservations_filter_by('day')
  reservations = filter_by_time(reservations, params[:start_time], params[:end_time])
  Rails.cache.fetch("build_reports_branch_covers_by_day:#{restaurants_cache_key}:#{reservations.cache_key}:#{I18n.locale}") do
    reservations_group = reservations.group_by(&:start_time_format)
    hours = reservations_group.keys.sort
    total_package_covers = {}
    total_package_seats = {}
    grand_total_covers = 0
    grand_total_seats = 0
    seat_lefts = {}
    restaurants_list.each do |item|
      inv_checker = InvCheckerFactory.new(
        item[:instance].id,
        item[:instance].time_zone,
      ).create_inv_checker_service
      seat_lefts[item[:id]] = inv_checker.seat_lefts(single_date_params, hours)
    end
    row_data = hours.map do |hour|
      restaurant_data = {}
      reservations_group[hour].each do |reservation|
        restaurants_list.each do |item|
          restaurant_data[item[:id]] ||= {}
          quantity = restaurant_data[item[:id]][:quantity] || 0
          quantity += reservation.party_size if reservation.restaurant_id == item[:id]
          restaurant_data[item[:id]] = {
            name: item[:name],
            quantity: quantity,
          }
        end
      end

      total = 0
      data = restaurants_list.map do |p|
        covers = if restaurant_data[p[:id]].present?
                   restaurant_data[p[:id]][:quantity].to_i
                 else
                   0
                 end

        total += covers
        total_package_covers[p[:id]] ||= {}
        total_package_covers[p[:id]][:total_covers] ||= 0
        total_package_covers[p[:id]][:total_covers] += covers
        grand_total_covers += covers
        {
          id: p[:id],
          name: p[:name],
          covers: covers,
        }
      end
      total_seat_available = 0
      data_seats = restaurants_list.map do |p|
        seat_available = seat_lefts[p[:id]][hour].to_i

        total_seat_available += seat_available
        total_package_seats[p[:id]] ||= {}
        total_package_seats[p[:id]][:total_seats] ||= 0
        total_package_seats[p[:id]][:total_seats] += seat_available
        grand_total_seats += seat_available
        {
          id: p[:id],
          name: p[:name],
          seats: seat_available,
        }
      end
      [hour, data, total, data_seats, total_seat_available]
    end
    [row_data, total_package_covers.to_a, grand_total_covers, total_package_seats.to_a, grand_total_seats]
  end
end