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
42
43
44
45
46
47
48
49
50
|
# File 'app/workers/inventory/create_or_update_inventories_worker.rb', line 4
def perform(data, restaurant_id)
restaurant = Restaurant.find_by(id: restaurant_id)
date = Time.zone.today + 1.day
data = JSON.load(data.gsub('=>', ':'))
(restaurant.days_in_advance - 1).times do
if data[date.strftime('%A').downcase].blank?
restaurant.inventories.where(date: date).destroy_all
date += 1.day
next
end
data_select = []
updated_dine_in_inventories = []
updated_take_away_inventories = []
data[date.strftime('%A').downcase].each do |dt|
return false if dt[0] >= dt[1]
data_select += period(dt[0], dt[1])
period(dt[0], dt[1]).each do |start_time|
next if date == Time.zone.today && start_time < Time.now_in_tz(restaurant.time_zone).to_s(:time)
inv = Inventory.find_or_initialize_by restaurant_id: restaurant.id, date: date,
start_time: Time.zone.parse(start_time),
end_time: Time.zone.parse(start_time) + 15.minutes
inv.attributes = { quantity_available: dt.last, total_booked_seat: 0 }
inv.save!
updated_dine_in_inventories << { restaurant_id: restaurant.id, inv: inv }
inv = InventoryTakeAway.find_or_initialize_by restaurant_id: restaurant.id, date: date,
start_time: Time.zone.parse(start_time),
end_time: Time.zone.parse(start_time) + 15.minutes
inv.attributes = { quantity_available: dt.last, total_booked_seat: 0 }
inv.save!
updated_take_away_inventories << { restaurant_id: restaurant.id, inv: inv }
end
end
restaurant.inventories.where(date: date).where.not(start_time: data_select).destroy_all
date += 1.day
send_updated_inventory_event(updated_dine_in_inventories) if updated_dine_in_inventories.present?
send_updated_inventory_event(updated_take_away_inventories) if updated_take_away_inventories.present?
end
::Vendors::Getyourguide::NotifyWorker.perform_async([restaurant.id])
end
|