Class: Item::CycleCountPrioritizer

Inherits:
BaseService show all
Defined in:
app/services/item/cycle_count_prioritizer.rb

Overview

Service object: cycle count prioritizer.

Instance Attribute Summary

Attributes inherited from BaseService

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ CycleCountPrioritizer

aim to count every item once per 3 months minimum
count item if drops below item.qty_warn_on_stock and last count more than 4 weeks ago
items with unit_cogs > $200 counted every 2 weeks (46 as of 2/6/18)
never count fixing strip, circuit check, warning label, face plate, brochure, catalog, poster, sample (too many to count)
don't count anything with 0 in stock
count items due to arrive soon more often

Parameters:

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

    Options hash

Options Hash (options):

  • :logger (Logger)

    custom logger (defaults to Rails.logger)



13
14
15
16
# File 'app/services/item/cycle_count_prioritizer.rb', line 13

def initialize(options = {})
  @options = options
  @logger = options[:logger] || Rails.logger
end

Class Method Details

.calculate_store_item_priority(si) ⇒ Object



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
# File 'app/services/item/cycle_count_prioritizer.rb', line 32

def self.calculate_store_item_priority(si)
  priority = 0
  return 0 if /fixing.*strip|circuit.*check|warning.*label|face.*plate|brochure|catalog|poster|sample/i.match?(si.item.name)
  return 0 if si.qty_on_hand.zero?

  # start with 10 so they'll always beat the 0s
  priority += 10
  # +30 if it's in a catalog (or part of a kit in any catalogs)
  active_kit_parent_count = 0
  si.item.kit_parents.each do |k|
    active_kit_parent_count += 1 if CatalogItem.where(state: %w[active active_hidden]).joins(:store_item).where(store_items: { item_id: k.id, location: 'AVAILABLE', store_id: si.store_id }).present?
  end
  # Each active kit parent counts for 10
  priority += 10 * active_kit_parent_count

  priority += 30 if si.catalog_items.main_catalogs.public_catalog_items.any? || si.item.kit_parents.any? do |k|
                      k.catalog_items.main_catalogs.public_catalog_items.joins(:store_item).where(store_items: { store_id: si.store_id }).any?
                    end
  # evaluate how long since last counted
  last_counted = si.last_cycle_count.try(:date_processed)
  if last_counted.nil?
    # +500 if never counted
    priority += 500
  else
    months_since_last_counted = ((Date.current.year * 12) + Date.current.month) - ((last_counted.year * 12) + last_counted.month)
    # + 10 for each months that passed since the last count
    priority += months_since_last_counted * 10
  end
  # +20 if below warning level and last count more than 4 weeks ago
  priority += 20 if si.item.qty_warn_on_stock && (si.qty_on_hand <= si.item.qty_warn_on_stock) && (last_counted.nil? || (last_counted < 4.weeks.ago))
  # +30 if more due to arrive in a shipment in next week and last count more than 2 weeks ago
  priority += 30 if si.on_order.any? do |details|
                      details[:promised_delivery_date] && (details[:promised_delivery_date] >= Date.current) && (details[:promised_delivery_date] <= Date.current + 7)
                    end && (last_counted.nil? || (last_counted < 2.weeks.ago))

  # Active/Public in any catalog ? get a boost
  priority += 20 if si.catalog_items.where(state: 'active').present?

  # Actvive or Active/Hidden in main catalog? get a boost (So active + main catalog essentially get a double boost because of the previous line)
  priority += 20 if si.catalog_items.main_catalogs.where(state: %w[active active_hidden]).present?

  # Manually flagged for cycle counting?
  priority += 100 if si.add_to_next_cycle_count?

  priority
end

Instance Method Details

#potential_store_itemsObject



79
80
81
82
83
84
# File 'app/services/item/cycle_count_prioritizer.rb', line 79

def potential_store_items
  StoreItem.joins(item: :product_category).includes(:item)
           .merge(ProductCategory.non_publications)
           .where(location: 'AVAILABLE', is_discontinued: false,
                  items: { is_discontinued: false, is_kit: false })
end

#processObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/item/cycle_count_prioritizer.rb', line 18

def process
  # set everything to 0 initially
  res = {}
  StoreItem.transaction do
    StoreItem.update_all(cycle_count_priority: 0)
    potential_store_items.each do |si|
      priority = self.class.calculate_store_item_priority(si)
      si.update_attribute!(:cycle_count_priority, priority) if priority > 0
      res[si.id] = priority
    end
  end
  res
end