Class: Inventory::WayfairSkuStockReporter

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/inventory/wayfair_sku_stock_reporter.rb

Constant Summary collapse

SKUS =

Uses LtreePaths constants for efficient ltree queries (no DB lookup needed)

{
  'WAYEM-120'  => { pl_path: LtreePaths::PL_FLOOR_HEATING_TEMPZONE_EASY_MAT_TWIN, pc_path: LtreePaths::PC_HEATING_ELEMENTS },
  'WAYTRT-120' => { pl_path: LtreePaths::PL_FLOOR_HEATING_TEMPZONE_FLEX_ROLL_TWIN_120V, pc_path: LtreePaths::PC_HEATING_ELEMENTS },
  'WAYTRT-240' => { pl_path: LtreePaths::PL_FLOOR_HEATING_TEMPZONE_FLEX_ROLL_TWIN_240V, pc_path: LtreePaths::PC_HEATING_ELEMENTS }
}.freeze

Instance Method Summary collapse

Instance Method Details

#processObject



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
# File 'app/services/inventory/wayfair_sku_stock_reporter.rb', line 9

def process
  SKUS.map do |wayfair_sku, filters|
    item = Item.find_by(sku: wayfair_sku)
    sku_report = { sku: wayfair_sku, catalog_item_stocks: [] }

    # Go through each catalog item
    sku_report[:catalog_item_stocks] = item.catalog_items.public_catalog_items.map do |ci|
      # Find the qty available for this group using ltree paths (no URL lookup)
      related_items = Item.by_product_line_path(filters[:pl_path]).by_product_category_path(filters[:pc_path]).active_in_catalog_ids(ci.catalog_id)
      # What's the total coverage for these items
      total_sqft = ci.store_item.store.store_items.joins(:item).merge(related_items).map do |si|
        unit_sqft = si.item.sqft || si.item.rendered_product_specifications.dig('coverage_at_3_75_inch_spacing', 'raw') || 10
        si.qty_available * unit_sqft
      end.sum
      # This translates to this many units (5sqft per unit)
      total_units = (total_sqft / 5).floor
      # Which we can now store in the catalog item
      ci.update_attribute(:min_stock_to_report, total_units)
      logger.info "#{item.sku} -> catalog item id #{ci.id} will now report #{total_units} total units"
      { ci.id => total_units }
    end

    sku_report
  end
end