Class: PartnerService::Reports::BranchCoversService

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/partner_service/reports/branch_covers_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#params, #restaurants

Instance Method Summary collapse

Methods inherited from BaseService

#build_reports_by_day, #initialize

Methods included from CopperHelper

#copper_sort_restaurants

Constructor Details

This class inherits a constructor from PartnerService::Reports::BaseService

Instance Method Details

#build_reports_by_hourObject



4
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
# File 'app/services/partner_service/reports/branch_covers_service.rb', line 4

def build_reports_by_hour
  Rails.cache.fetch(cache_key(active_reservations_filter_by('day').cache_key)) do
    reservations = active_reservations_filter_by('day')
    reservations = filter_by_time(reservations, params[:start_time], params[:end_time])
    reservations_group = reservations.group_by(&:start_time_format)
    hours = reservations_group.keys.sort
    total_package_covers = Hash.new { |h, k| h[k] = { total_covers: 0 } }
    total_package_seats = Hash.new { |h, k| h[k] = { total_seats: 0 } }
    grand_total_covers = 0
    grand_total_seats = 0
    seat_lefts = calculate_seat_lefts(hours)

    row_data = hours.map do |hour|
      restaurant_data = aggregate_restaurant_data(reservations_group[hour], hour)
      total = 0

      data = restaurants_list.map do |p|
        covers = restaurant_data.dig(p[:id], :quantity) || 0
        total += covers
        update_total_package_covers(total_package_covers, p[:id], covers)
        grand_total_covers += covers

        {
          id: p[:id],
          name: p[:name],
          covers: covers,
        }
      end

      total_seat_available, data_seats = aggregate_seat_data(seat_lefts, hour, total_package_seats,
                                                             grand_total_seats)

      [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