Class: ScheduleWorkers::Homes::BaseWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/schedule_workers/homes/base_worker.rb

Direct Known Subclasses

PopularBranch

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#flexible_start_date(restaurant_id, start_date) ⇒ Object

if current time is after 11 pm, then it will return 2 dates today and tomorrow, it's required to prevent empty report data between 00:00 - 01:00



9
10
11
12
13
14
15
# File 'app/workers/schedule_workers/homes/base_worker.rb', line 9

def flexible_start_date(restaurant_id, start_date)
  restaurant = Restaurant.fetch(restaurant_id)
  now = Time.now_in_tz(restaurant.time_zone)
  return [start_date, start_date + 1.day] if now.hour >= 23

  [start_date]
end

#generate_restaurant_revenues(reservations, date, report_name, restaurant_id = nil) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/workers/schedule_workers/homes/base_worker.rb', line 17

def generate_restaurant_revenues(reservations, date, report_name, restaurant_id = nil)
  month = date.strftime('%m')
  year = date.strftime('%Y')
  reports = []
  reservations = reservations.joins(:restaurant)
                             .select('restaurant_id, SUM(reservation_properties.revenue) revenue')
                             .group(:restaurant_id)
  if restaurant_id.present?
    restaurant = Restaurant.fetch restaurant_id
    if restaurant.branch.blank?
      reservations = reservations.where(restaurants: { branch_id: nil })
      # elsif restaurant.branch.present? && restaurant.branch.restaurants.active.not_expired.count == 1
      # no operation
      # if we filter by branch, it will return empty records
    end
  end
  if reservations.present?
    reservations.each do |reservation|
      flexible_start_date(reservation.restaurant_id, date).each do |start_date|
        reports.push(
          Report.new(
            restaurant_id: reservation.restaurant_id,
            name: report_name,
            day: start_date.strftime('%d'),
            month: month,
            year: year,
            data: reservation['revenue'].to_i
          )
        )
      end
    end
  elsif restaurant_id.present?
    flexible_start_date(restaurant_id, date).each do |start_date|
      reports.push(
        Report.new(
          restaurant_id: restaurant_id,
          name: report_name,
          day: start_date.strftime('%d'),
          month: month,
          year: year,
          data: 0
        )
      )
    end
  end
  Report.transaction do
    Report.import! reports, on_duplicate_key_update: %i[restaurant_id name day month year], batch_size: 1000, raise_error: true
  end

  branches = {}
  reports = []
  reservations.joins(:restaurant).where.not(restaurants: { branch_id: nil })
              .select('restaurant_id, SUM(reservation_properties.revenue) revenue, branch_id')
              .group(:restaurant_id).each do |reservation|
    branch_key = reservation.branch_id.to_s
    branches[branch_key] ||= []
    branches[branch_key].push(
      [reservation.restaurant_id, reservation['revenue'].to_i]
    )
  end
  branches.each do |_branch_key, revenues|
    selected_restaurant = revenues.max_by do |revenue|
      revenue[1]
    end

    flexible_start_date(selected_restaurant[0], date).each do |start_date|
      reports.push(
        Report.new(
          restaurant_id: selected_restaurant[0],
          name: report_name,
          day: start_date.strftime('%d'),
          month: month,
          year: year,
          data: selected_restaurant[1]
        )
      )
    end
  end
  Report.transaction do
    Report.import! reports, on_duplicate_key_update: %i[restaurant_id name day month year], batch_size: 1000, raise_error: true
  end

  # touch home restaurant section cache to make total restaurants up to date
  HomeSection.find_each(&:touch)
end

#generate_restaurant_tag_revenues(reservations, date, report_name, restaurant_tag_id) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/workers/schedule_workers/homes/base_worker.rb', line 103

def generate_restaurant_tag_revenues(reservations, date, report_name, restaurant_tag_id)
  month = date.strftime('%m')
  year = date.strftime('%Y')

  now = Time.now_in_tz(Time::THAILAND_ZONE)
  days = now.hour >= 23 ? [date, date + 1.day] : [date]

  reports = []
  reservations.select('restaurant_id, SUM(reservation_properties.revenue) revenue')
              .group(:restaurant_id).each do |reservation|
    days.each do |day|
      day = day.strftime('%d')
      reports.push(
        Report.new(
          restaurant_id: restaurant_tag_id,
          name: report_name,
          day: day,
          month: month,
          year: year,
          data: reservation['revenue'].to_i
        )
      )
    end
  end
  Report.transaction do
    Report.import! reports, on_duplicate_key_update: %i[restaurant_id name day month year], batch_size: 1000, raise_error: true
  end

  # touch home restaurant section cache to make total restaurants up to date
  HomeRestaurantSection.find_each(&:touch)
end