Class: Item::Materials::Checks::TowelWarmer

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/item/materials/checks/towel_warmer.rb

Overview

Service object: towel warmer.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#process(container:, options: {}) ⇒ Object



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
# File 'app/services/item/materials/checks/towel_warmer.rb', line 13

def process(container:, options: {})
  return Result.new(status: :skipped) unless qualify?(container)

  material_alerts = []

  num_towel_warmers_dual = container.line_items.parents_only.select { |l| l.item.is_towel_warmer_dual_connect? }.sum(&:quantity)
  num_towel_warmers_plug_in = container.line_items.parents_only.select { |l| l.item.is_towel_warmer_plug_in? }.sum(&:quantity)
  num_towel_warmers_hardwired = container.line_items.parents_only.select { |l| l.item.is_towel_warmer_hardwired? }.sum(&:quantity)
  num_towel_warmer_controls_plug_in = container.line_items.parents_only.select { |l| l.item.is_towel_warmer_plug_in_control? }.sum(&:quantity)
  num_towel_warmer_controls_hardwired = container.line_items.parents_only.select { |l| l.item.is_towel_warmer_hardwired_control? }.sum(&:quantity)

  # test if we don't match the number of plugin controls to plugin towel warmers as well as that we don't match the number of plugin and dual connect towel warmers
  if (num_towel_warmer_controls_plug_in != num_towel_warmers_plug_in) && (num_towel_warmer_controls_plug_in != num_towel_warmers_plug_in + num_towel_warmers_dual) && container.catalog.catalog_items.by_skus(Item.towel_warmer_controls_plug_in.pluck(:sku)).public_catalog_items.any?
    diff_qty = num_towel_warmer_controls_plug_in - (num_towel_warmers_plug_in + num_towel_warmers_dual)
    ma_options = {}.merge(options)
    ma_options[:room] = container
    ma_options[:recommended_qty] = diff_qty.positive? ? (num_towel_warmers_plug_in + num_towel_warmers_dual) : num_towel_warmers_plug_in # recommended should reflect the number of plugin and dual connect towel warmers, only recommend the smallest change necessary
    ma_options[:actual_qty] = num_towel_warmer_controls_plug_in
    ma_options[:items] = if (ctrl_lis = container.line_items.parents_only.select { |l| l.item.is_towel_warmer_plug_in_control? }).any?
                           ctrl_lis.map(&:item)
                         else
                           Item.towel_warmer_controls_plug_in.to_a
                         end
    ma_options[:unmaskable] = true
    ma_options[:name] = "One plug in control per plug in towel warmer is recommended."
    material_alerts << Item::Materials::Alert.new(ma_options)
  end

  # test if we don't match the number of hardwired controls to hardwired towel warmers as well as that we  don't match the number of hardwired and dual connect towel warmers
  if (num_towel_warmer_controls_hardwired != num_towel_warmers_hardwired) && (num_towel_warmer_controls_hardwired != num_towel_warmers_hardwired + num_towel_warmers_dual) && container.catalog.catalog_items.by_skus(Item.towel_warmer_controls_hardwired.pluck(:sku)).public_catalog_items.any?
    diff_qty = num_towel_warmer_controls_hardwired - (num_towel_warmers_hardwired + num_towel_warmers_dual)
    ma_options = {}.merge(options)
    ma_options[:room] = container
    ma_options[:recommended_qty] = diff_qty.positive? ? (num_towel_warmers_hardwired + num_towel_warmers_dual) : num_towel_warmers_hardwired # recommended should reflect the number of hardwired and dual connect towel warmers, only recommend the smallest change necessary
    ma_options[:actual_qty] = num_towel_warmer_controls_hardwired
    ma_options[:items] = if (ctrl_lis = container.line_items.parents_only.select { |l| l.item.is_towel_warmer_hardwired_control? }).any?
                           ctrl_lis.map(&:item)
                         else
                           Item.towel_warmer_controls_hardwired.order_towel_warmer_controls_hardwired.to_a
                         end
    ma_options[:unmaskable] = true
    ma_options[:name] = "One hardwired control per hardwired towel warmer is recommended."
    material_alerts << Item::Materials::Alert.new(ma_options)
  end

  Result.new(status: :ok, alerts: material_alerts)
end

#qualify?(container) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'app/services/item/materials/checks/towel_warmer.rb', line 9

def qualify?(container)
  container.line_items.any? { |li| li.item.is_towel_warmer? }
end