Class: PartnerService::Inventories::GetInventoryWithTimeService

Inherits:
ApplicationService show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/partner_service/inventories/get_inventory_with_time_service.rb

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(type, time, restaurant) ⇒ GetInventoryWithTimeService

Returns a new instance of GetInventoryWithTimeService.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/partner_service/inventories/get_inventory_with_time_service.rb', line 8

def initialize(type, time, restaurant)
  @restaurant = restaurant
  if type == 'daily'
    @date = time
    inv_model = restaurant.selected_inventory_model
    @inventories_in_day = inv_model.where(restaurant_id: restaurant, date: time) if inv_model.present?
  else
    @start_date = time.beginning_of_month
    @end_date = time.end_of_month
    @date = (@start_date..@end_date).to_a
  end
end

Instance Method Details

#blocked?(date, time, inventory) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/services/partner_service/inventories/get_inventory_with_time_service.rb', line 79

def blocked?(date, time, inventory)
  return false if inventory.blank?
  return false if inventory.quantity_available.to_i.positive?

  check_block = false
  updated_inventories = ::UpdatedInventory.where(restaurant_id: @restaurant.id).where(quantity_available: 0).
    where('( start_date = ? ) OR ( end_date = ? )', date, date)
  updated_inventories.each do |ui|
    start_time = (ui.start_date.to_s + " #{ui.start_time.to_formatted_s(:time)}").to_datetime
    check_time = (date.to_s + " #{time}").to_datetime
    end_time = (ui.end_date.to_s + " #{ui.end_time.to_formatted_s(:time)}").to_datetime
    end_time = "#{ui.end_date} 24:00".to_datetime if ui.start_date == ui.end_date &&
      ui.end_time.to_formatted_s(:time) == '00:00'
    check_block = true if start_time <= check_time && check_time < end_time
  end
  check_block
end

#calc_dashboardObject



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
# File 'app/services/partner_service/inventories/get_inventory_with_time_service.rb', line 22

def calc_dashboard
  # TEMPORARY PERFORMANCE OPTIMIZATION: Returning hardcoded zero values to improve API response time.
  # The actual calculation below is expensive due to database queries on large datasets.
  # TODO: Restore proper calculation after implementing performance optimizations
  # (e.g., caching, aggregated metrics table, or background calculation)
  {
    dashboard: {
      total_allotment: 0,
      booked_allotment: 0,
      available_allotment: 0,
      allotment_capacity_utilization: 0.0,
    },
  }

  # Original calculation (temporarily disabled):
  # inv_model = @restaurant.selected_inventory_model
  # if inv_model.present?
  #   total_allotment = inv_model.where(restaurant_id: @restaurant.id, date: @date).sum(:quantity_available)
  #   booked_allotment = inv_model.where(restaurant_id: @restaurant.id, date: @date).sum(:total_booked_seat)
  #
  #   available_allotment = total_allotment - booked_allotment
  #   allotment_capacity_utilization = (1 - (available_allotment / total_allotment.to_f)) * 100
  #   { dashboard: { total_allotment: total_allotment,
  #                  booked_allotment: booked_allotment,
  #                  available_allotment: available_allotment,
  #                  allotment_capacity_utilization: allotment_capacity_utilization } }
  # end
end

#inventory_dailyObject



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
# File 'app/services/partner_service/inventories/get_inventory_with_time_service.rb', line 52

def inventory_daily
  inventories = calc_dashboard
  period = Reservation::PERIODS_WITHOUT_00_AM

  # Fetch all seat_left data at once using seat_lefts
  seat_left_data = inv_checker.seat_lefts(@date, period)

  period.each do |dt|
    inventories.merge!({ dt => {} })
    inventory = @inventories_in_day.by_hr_min(dt).first
    bookable = inv_checker.bookable?(date: @date, start_time: dt, adult: 1, kids: 0)
    locked_seats = !bookable && seat_left_data[dt].positive?

    inventories[dt].merge!(
      active: (Time.zone.today - 14.days <= @date) && seat_left_data[dt].positive?,
      total: inventory&.quantity_available,
      booked: inventory&.total_booked_seat,
      id: inventory&.id,
      locked_seats: locked_seats,
      block: blocked?(@date, dt, inventory),
    )
  end

  inventories
end

#inventory_in_day(date) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'app/services/partner_service/inventories/get_inventory_with_time_service.rb', line 107

def inventory_in_day(date)
  result = {}
  active = Time.zone.today - 14.days <= date
  total = Inventory.where(restaurant_id: @restaurant.id, date: date).sum(:quantity_available)
  booked = Inventory.where(restaurant_id: @restaurant.id, date: date).sum(:total_booked_seat)

  result[date] = { active: active, total: total, booked: booked, available: total - booked }
  result
end

#inventory_monthlyObject



98
99
100
101
102
103
104
# File 'app/services/partner_service/inventories/get_inventory_with_time_service.rb', line 98

def inventory_monthly
  inventory_monthly = calc_dashboard
  (@start_date..@end_date).each do |date|
    inventory_monthly.merge!(inventory_in_day(date))
  end
  inventory_monthly
end