Class: Item::Materials::Checks::Recommended

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

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

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



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

def process(container:, options:{})
  container_rec_materials = container.get_recommended_materials(options)

  return Result.new(status: :skipped) unless container_rec_materials.present?

  material_alerts = []
  rec_materials_hash = container_rec_materials.group_by{|p| p['exclusive_group_type']}.sort_by{|k,v| (ExclusiveItemGroup.find_by(key: k).position rescue 99)}.to_h
  rec_materials_hash.each do |group_type, materials|
    sorted_materials = materials.sort_by{|a| [a['sku'], (Item.find_by(sku: a['sku']).product_category.priority rescue 99)]}
    Rails.logger.debug "#{container.class.name}:#{container.id}: get_material_alerts, group_type: #{group_type}, materials: #{materials.inspect}"
    if group_type.present?
      exclusive_item_group = ExclusiveItemGroup.find_by(key: group_type)
      sku_list = sorted_materials.map{|a| a['sku']}
      recommended_qty = sorted_materials.first['qty']
      actual_qty = container.line_items.select{|l| sku_list.include?(l.sku)}.sum{|l| l.quantity}
      if exclusive_item_group.per_thermostat
        recommended_qty = container.line_items.thermostats.to_a.sum{|l| l.quantity}
      else
        recommended_qty = 1
        actual_qty = container.line_items.select{|l| sku_list.include?(l.sku)}.sum{|l| l.quantity}
      end
      Rails.logger.debug "#{container.class.name}:#{container.id}: get_material_alerts, container.line_items.map{|li| li.sku}: #{container.line_items.map{|li| li.sku}.inspect}"
      Rails.logger.debug "#{container.class.name}:#{container.id}: get_material_alerts, sku_list: #{sku_list.inspect}, recommended_qty: #{recommended_qty}, actual_qty: #{actual_qty}"
      if actual_qty != recommended_qty and sorted_materials.any? and sorted_materials.all?{|a| container.catalog.catalog_items.by_skus(a['sku']).first.present? and !IqAccessoryFilter.for_item_or_exclusive_item_group(exclusive_item_group).for_customer(container.customer).first.present?}
        ma_options = {}.merge(options)
        ma_options[:room] = container
        ma_options[:group_type] = exclusive_item_group.key
        ma_options[:group_name] = exclusive_item_group.name
        ma_options[:recommended_qty] = recommended_qty
        ma_options[:actual_qty] = actual_qty
        ma_options[:items] = sorted_materials.map{|a| Item.find_by(sku: a['sku'])}.compact
        material_alerts << Item::Materials::Alert.new(ma_options)
      end
    else
      sorted_materials.each do |a|
        # here we skip alerts for membrane and instead use these to calculate total membrane coverage recommended vs actual total membrane coverage
        dont_skip_alert = true
        item = Item.find_by(sku: a['sku'])
        if item.is_membrane?
          dont_skip_alert = false
        end
        recommended_qty = a['qty']
        actual_qty = container.line_items.select{|l| l.sku == a['sku']}.sum{|l| l.quantity}
        Rails.logger.debug "#{container.class.name}: #{container.id}: get_material_alerts, container.line_items.map{|li| li.sku}: #{container.line_items.map{|li| li.sku}.inspect}"
        Rails.logger.debug "#{container.class.name}:#{container.id}: get_material_alerts, a['sku']: #{a['sku']}, recommended_qty: #{recommended_qty}, actual_qty: #{actual_qty}"
        if dont_skip_alert and actual_qty != recommended_qty and container.catalog.catalog_items.by_skus(a['sku']).first.present? and item.present? and !IqAccessoryFilter.for_item_or_exclusive_item_group(item).for_customer(container.customer).first.present?
          ma_options = {}.merge(options)
          ma_options[:room] = container
          ma_options[:recommended_qty] = recommended_qty
          ma_options[:actual_qty] = actual_qty
          item = Item.find_by(sku: a['sku'])
          ma_options[:items] = [item]
          underlayment_alert = nil
          underlayment_warning_for_www = nil
          underlayment_alert = if item.is_underlayment?
            if item.is_thermalsheet?
              "ThermalSheet"
            elsif item.is_cork?
              "Cork"
            elsif item.is_cerazorb?
              "CeraZorb®"
            end
          end
          if item.is_junction_box? # we make these unmaskable, ie part of a system
            ma_options[:unmaskable] = true
          end
          if underlayment_alert.present? and (recommended_qty > actual_qty) and options[:for_www]
            underlayment_warning_for_www = "We always recommend insulating a concrete subfloor from the radiant heating system using our #{underlayment_alert} underlayment for optimal performance and efficiency. "
          end
          ma_options[:name] = underlayment_warning_for_www
          material_alerts << Item::Materials::Alert.new(ma_options)
        end
      end
    end
  end

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

end