Class: Item::Materials::Check

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

Defined Under Namespace

Classes: Result

Constant Summary collapse

CHECKS =
%i(
  engineering_alert
  control_capacity
  wood_shake_roof
  floor_heating
  snow_melting
  roof_deicing
  radiant_panel
  towel_warmer
  recommended
  membrane
  moq
  tz_cable_tape_roll
)
COMMON_SKUS =

Common SKUs used across material checks - preload these once to avoid N+1 Item.find_by calls

[
  ItemConstants::POWER_MODULE_SKU,
  ItemConstants::TZ_CABLE_TAPE_SKU
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_item_by_sku(sku) ⇒ Object

Class method to get a preloaded item by SKU (called by individual check classes)



72
73
74
75
# File 'app/services/item/materials/check.rb', line 72

def self.find_item_by_sku(sku)
  preloaded = Thread.current[:material_check_preloaded_items]
  preloaded&.[](sku) || Item.find_by(sku: sku)
end

Instance Method Details

#applied_stock_threshold_and_customer_filters_pass?(container, material_alert) ⇒ Boolean (protected)

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
# File 'app/services/item/materials/check.rb', line 112

def applied_stock_threshold_and_customer_filters_pass?(container,material_alert)
  return true if material_alert.unmaskable
  return true unless material_alert.items.present? #If no item are in the material alert, do not run this filter
  catalog = container.customer.catalog
  material_alert.items.delete_if do |item|
    ci = catalog.catalog_items.by_skus(item.sku).first
    !(ci and ci.item.suggested_item_applies_to(container.customer) and ci.qty_available > item.suggested_item_stock_threshold)
  end
  return material_alert.items.present?
end

#apply_stock_threshold_and_customer_filters(container, material_alerts = []) ⇒ Object (protected)



108
109
110
# File 'app/services/item/materials/check.rb', line 108

def apply_stock_threshold_and_customer_filters(container, material_alerts=[])
  return material_alerts.select{|ma| applied_stock_threshold_and_customer_filters_pass?(container,ma)}
end

#perform_checks_raw(container:, options: {}) ⇒ Array<Hash>

Returns raw check results as an array of hashes for database persistence.
This is called by Pickable#get_material_alerts to generate fresh alerts.

Parameters:

  • container (Quote, Order, RoomConfiguration)

    the resource to check

  • options (Hash) (defaults to: {})

    options hash with :for_www and :for_cart flags

Returns:

  • (Array<Hash>)

    array of alert data hashes with keys:
    :name, :items, :recommended_qty, :actual_qty, :group_name, :group_type, :unmaskable



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

def perform_checks_raw(container:, options: {})
  # Preload commonly used items to avoid N+1 queries in individual checks
  preload_common_items

  material_alerts = []
  CHECKS.each do |check|
    check_class = "Item::Materials::Checks::#{check.to_s.classify}".constantize
    check_instance = check_class.new
    result = check_instance.process(container: container, options: options)
    if result.status == :ok
      material_alerts += result.alerts
    end
  end
  material_alerts = apply_stock_threshold_and_customer_filters(container, material_alerts)

  # Convert Virtus Alert objects to plain hashes for database storage
  material_alerts.map do |alert|
    {
      name: alert.name,
      items: alert.items.to_a.compact,
      recommended_qty: alert.recommended_qty,
      actual_qty: alert.actual_qty,
      group_name: alert.group_name,
      group_type: alert.group_type,
      unmaskable: alert.unmaskable || false
    }
  end
end

#process(container:, for_www: false, for_cart: false, _cache: true) ⇒ Object

Deprecated.

Use Pickable#get_material_alerts which uses database persistence

Legacy method - kept for backward compatibility during transition



30
31
32
33
# File 'app/services/item/materials/check.rb', line 30

def process(container:, for_www: false, for_cart: false, _cache: true)
  options = { for_www: for_www, for_cart: for_cart }
  perform_checks(container, options)
end