Class: Item::CycleCountPrioritizer
- Inherits:
-
BaseService
- Object
- BaseService
- Item::CycleCountPrioritizer
- Defined in:
- app/services/item/cycle_count_prioritizer.rb
Overview
Service object: cycle count prioritizer.
Instance Attribute Summary
Attributes inherited from BaseService
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ CycleCountPrioritizer
constructor
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.
- #potential_store_items ⇒ Object
- #process ⇒ Object
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
12 13 14 15 |
# File 'app/services/item/cycle_count_prioritizer.rb', line 12 def initialize( = {}) @options = @logger = [:logger] || Rails.logger end |
Class Method Details
.calculate_store_item_priority(si) ⇒ Object
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 |
# File 'app/services/item/cycle_count_prioritizer.rb', line 31 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_items ⇒ Object
78 79 80 81 82 83 |
# File 'app/services/item/cycle_count_prioritizer.rb', line 78 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 |
#process ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/services/item/cycle_count_prioritizer.rb', line 17 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 |