Class: InventoryReporterWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/inventory_reporter_worker.rb

Overview

typed: ignore frozen_string_literal: true

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(for_next_x_days = 3, restaurant_id = 0) ⇒ Object



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
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
# File 'app/workers/inventory_reporter_worker.rb', line 6

def perform(for_next_x_days = 3, restaurant_id = 0)
  blocked_restaurants = []
  start_date = Time.zone.today.to_date
  end_date = start_date + for_next_x_days.to_i.days

  if restaurant_id.zero?
    Restaurant.active.find_each
  else
    [Restaurant.find(restaurant_id)].each
  end.each do |restaurant|
    next if restaurant.inventory_template_groups.blank?

    inventories = restaurant.inventories.where(date: start_date..end_date).pluck(:quantity_available).uniq

    if inventories == [0] || inventories.blank?
      blocked_restaurants.push restaurant
      next
    end

    inv_checker = InvCheckerFactory.new(restaurant.id, restaurant.time_zone).create_inv_checker_service

    available = false
    (start_date..end_date).each do |date|
      next if available

      restaurant.inventory_template_groups.each do |itg|
        next if available

        it = if date.wday == 0 && restaurant.sun == itg.id
               itg.inventory_templates
             elsif date.wday == 1 && restaurant.mon == itg.id
               itg.inventory_templates
             elsif date.wday == 2 && restaurant.tue == itg.id
               itg.inventory_templates
             elsif date.wday == 3 && restaurant.wed == itg.id
               itg.inventory_templates
             elsif date.wday == 4 && restaurant.thu == itg.id
               itg.inventory_templates
             elsif date.wday == 5 && restaurant.fri == itg.id
               itg.inventory_templates
             elsif date.wday == 6 && restaurant.sat == itg.id
               itg.inventory_templates
             else
               []
             end
        it.each do |inv|
          next if available

          # TODO: use InvCheckerFactory#seat_lefts
          available = true if inv_checker.seat_left(date, inv.start_time.strftime('%H:%M')).positive?
        end
      end
    end

    next if available

    blocked_restaurants.push restaurant
  end

  report = "<html><body><h1>Inventory Report for next #{for_next_x_days} days (#{(start_date..end_date).to_a.to_sentence})</h1>"
  return if blocked_restaurants.blank?

  blocked_restaurants.each do |r|
    report += "<h4>#{r.id} - #{r.name}</h4>"

    inv_report = ''
    (start_date..end_date).each do |date|
      blocked_inventories = UpdatedInventory.where(restaurant_id: r.id).where('start_date >= ? OR end_date >= ?',
                                                                              date, date)
      blocked_inventories.each do |ui|
        next unless date.between?(ui.start_date, ui.end_date)

        inv_report += "<ul><li>ID\t\t\t: #{ui.id}</li>"
        inv_report += "<li>Start date\t\t\t: #{ui.start_date}</li>"
        inv_report += "<li>Start time\t\t\t: #{ui.start_time.strftime('%H:%M')}</li>"
        inv_report += "<li>End date\t\t\t: #{ui.end_date}</li>"
        inv_report += "<li>End time\t\t\t: #{ui.end_time.strftime('%H:%M')}</li></ul>"
      end
    end
    if inv_report.blank?
      report += '<p>There is no inventory block history, might be something went wrong for this restaurant</p>'
    else
      report += "<p>Inventory block data:</p>\n"
      report += inv_report
    end
    report += "\n------------------------------<br />"
  end

  report += '</body></html>'
  StaffMailer.inventory_reporter(start_date.to_s, end_date.to_s, report).deliver_later!
end