Module: ModelExt::Reservations::ClassMethods::ClassMethods

Defined in:
lib/model_ext/reservations/class_methods.rb

Overview

Loaded automatically as class methods by active support

Instance Method Summary collapse

Instance Method Details

#admin_cancellation_reasonsObject



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/model_ext/reservations/class_methods.rb', line 167

def admin_cancellation_reasons
  [
    'no seats available',
    'private function',
    'no stocks/ingredients',
    'restaurant is closed',
    'customer asked to cancel',
    'customer changed to walk-in/better deal',
    'refund guarantee',
  ]
end

#booking_history(phone) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/model_ext/reservations/class_methods.rb', line 127

def booking_history(phone)
  phone = if phone[0] == '0'
            phone[1..-1]
          elsif phone[0] == '+'
            phone[3..-1]
          elsif phone.include?('*') || phone == '0'
            ::HH_LOGGER.info 'Phone not detected', phone: phone
            nil
          else
            phone
          end
  return ::Reservation.where('1 = 0') if phone.nil?

  ::Reservation.where('phone LIKE ?', "%#{phone}%")
end

#build_special_request(charged_amount, sr) ⇒ Object



11
12
13
14
15
# File 'lib/model_ext/reservations/class_methods.rb', line 11

def build_special_request(charged_amount, sr)
  return "CC #{charged_amount}" if sr.blank?

  "CC #{charged_amount} - #{sr}"
end

#cancellation_reasonsObject



157
158
159
160
161
162
163
164
165
# File 'lib/model_ext/reservations/class_methods.rb', line 157

def cancellation_reasons
  [
    'I found a better deal',
    'My plan changed',
    'The restaurant/hotel asked me to cancel',
    'I saw negative feedback on this place',
    'I decided to go to another place',
  ]
end

#channel_groupObject

used in admin>reservation report page



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/model_ext/reservations/class_methods.rb', line 94

def channel_group
  [
    {
      id: [0, 1, 8, 11],
      name: 'Hungry Hub',
    },
    {
      id: [5, 6, 7],
      name: 'Manual',
    },
    {
      id: [9],
      name: 'Facebook',
    },
    {
      id: [10],
      name: 'Website',
    },
    {
      id: [12],
      name: 'BKK Menu',
    },
    {
      id: [13],
      name: 'Bangkok.com',
    },
    {
      id: [14],
      name: 'Between',
    },
  ]
end

#find_reservation_ids_by_refs(id) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/model_ext/reservations/class_methods.rb', line 179

def find_reservation_ids_by_refs(id)
  strip_id = id.to_s.strip

  query = begin
    id.to_s.to_i.to_s == strip_id ? strip_id : Reservation.decrypt_id(strip_id)
  rescue StandardError
    strip_id
  end

  sql = <<-SQL
    WITH reservation_cte AS (
      SELECT reservations.id
      FROM reservations
      WHERE reservations.id = :query
      UNION
      SELECT reservations.id
      FROM reservations
      INNER JOIN vendor_reservations ON reservations.id = vendor_reservations.reservation_id
      WHERE vendor_reservations.reference_id = :strip_id
        OR vendor_reservations.reseller_reference_id = :strip_id
    )
    SELECT id FROM reservation_cte
  SQL

  Reservation.find_by_sql([sql, { query: query, strip_id: strip_id }]).pluck(:id)
end

#generate_klick_digital_client_order_id(id) ⇒ String

Generates a Klick Digital client order ID

Parameters:

  • id (String, Integer)

    The vendor order ID to prefix

Returns:

  • (String)

    The generated Klick Digital client order ID



216
217
218
# File 'lib/model_ext/reservations/class_methods.rb', line 216

def generate_klick_digital_client_order_id(id)
  "KD#{id}"
end

#generate_reports_by_channel(is_active = nil, restaurant_id = nil, no_show = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/model_ext/reservations/class_methods.rb', line 37

def generate_reports_by_channel(is_active = nil, restaurant_id = nil, no_show = nil)
  sql = "SELECT MONTH(created_at) AS m, YEAR(created_at) AS y,
#{::Channel.names_to_reservation_report_by_channel}
FROM #{::Reservation.table_name}"
  custom_query = []
  if is_active.present?
    custom_query.push(' active = 1 ') if is_active == 1
    custom_query.push(' active = 0 ') if is_active == 0
  end
  custom_query.push(" restaurant_id = #{restaurant_id}") if restaurant_id.present?

  if no_show.present?
    custom_query.push(' no_show = 1 ') if no_show == 1
    custom_query.push(' no_show = 0 ') if no_show == 0
  end

  custom_query.push(' is_temporary = 0 ')
  sql += ' WHERE ' + custom_query.join(' AND ') if custom_query.present?
  sql += ' GROUP BY m, y ORDER BY y, m ASC'
  ::Reservation.find_by_sql(sql)
end

#generate_reports_by_restaurant(month = nil, year = nil, active = nil, no_show = nil) ⇒ Object



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
# File 'lib/model_ext/reservations/class_methods.rb', line 59

def generate_reports_by_restaurant(month = nil, year = nil, active = nil, no_show = nil)
  restaurant = ::Restaurant.table_name
  restaurant_translation = ::Restaurant::Translation.table_name
  reservation = ::Reservation.table_name
  sql = "SELECT #{restaurant}.id AS res_id,
#{restaurant_translation}.name,
#{::Channel.names_to_reservation_report_by_restaurant}
FROM #{reservation}, #{restaurant}
JOIN #{restaurant_translation} ON #{restaurant_translation}.restaurant_id = #{restaurant}.id
WHERE #{reservation}.restaurant_id = #{restaurant}.id
AND #{restaurant_translation}.locale = 'en'"
  if month.present?
    month = month.to_i
    sql += " AND MONTH(#{reservation}.created_at) = MONTH(STR_TO_DATE(#{month}, '%m')) "
  end

  if year.present?
    year = year.to_i
    sql += " AND YEAR(#{reservation}.created_at) = YEAR(STR_TO_DATE(#{year}, '%Y')) "
  end
  if active.present?
    sql += " AND #{reservation}.active = 1 " if active.to_i == 1
    sql += " AND #{reservation}.active = 0 " if active.to_i == 0
  end

  if no_show.present?
    sql += " AND #{reservation}.no_show = 1 " if no_show.to_i == 1
    sql += " AND #{reservation}.no_show = 0 " if no_show.to_i == 0
  end
  sql += ' AND is_temporary = 0'
  sql += ' GROUP BY res_id  ORDER BY grand_total DESC'
  ::Reservation.find_by_sql(sql)
end

#klick_digital_client_order_id?(id) ⇒ Boolean

Checks if an ID is from Klick Digital client

Parameters:

  • id (String, Integer)

    The ID to check

Returns:

  • (Boolean)

    true if ID starts with 'KD', false otherwise



209
210
211
# File 'lib/model_ext/reservations/class_methods.rb', line 209

def klick_digital_client_order_id?(id)
  id.to_s.start_with?('KD')
end

#search_channel_with_restaurant(channel, restaurant_id, date = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/model_ext/reservations/class_methods.rb', line 21

def search_channel_with_restaurant(channel, restaurant_id, date = nil)
  restaurants_query = '('
  params = ::JSON.parse(channel)
  if params.is_a? ::Array
    params.each do |par|
      restaurants_query += par.to_s
      restaurants_query += ', ' if par != params.last
    end
  end
  restaurants_query += ')'
  r = ::Reservation.includes(:restaurant, :reward, :user).
    where("#{::Restaurant.table_name}.id = ? AND #{::Reservation.table_name}.channel IN #{restaurants_query}", restaurant_id)
  r = r.where(date: ::Date.strptime(date, '%m/%d/%Y')) if date.present?
  r
end

#total_coversObject



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/model_ext/reservations/class_methods.rb', line 143

def total_covers
  Rails.cache.fetch("reservation:total_covers:#{Time.zone.today}:#{Time.zone.now.hour}") do
    score = Reservation.select('sum(party_size) total').reached_goal_scope.where(no_show: false).where.not(channel: Channel.manual).first.total.to_s.split('')
    if score.present?
      score[-3] = 0
      score[-2] = 0
      score[-1] = 0
      score.join.to_i
    else
      0
    end
  end
end

#total_covers_seatedObject



17
18
19
# File 'lib/model_ext/reservations/class_methods.rb', line 17

def total_covers_seated
  hungry_hub_active.map(&:party_size).sum
end