Class: Shipping::Md5HashItem
- Inherits:
-
Object
- Object
- Shipping::Md5HashItem
- Defined in:
- app/services/shipping/md5_hash_item.rb
Overview
Service object: md5 hash item.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- RELEVANT_ITEM_VOLUME_RATIO_THRESHOLD =
Threshold for relevant item volume ratio.
0.005- RELEVANT_ITEM_WEIGHT_RATIO_THRESHOLD =
Threshold for relevant item weight ratio.
0.002
Class Method Summary collapse
Class Method Details
.process(item_hash, qty_override: nil) ⇒ Object
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 |
# File 'app/services/shipping/md5_hash_item.rb', line 24 def self.process(item_hash, qty_override: nil) # Shortcut if an item is passed directly is_item = false if item_hash.instance_of?(::Item) item_hash = { item_hash => qty_override || 1 } is_item = true end # FIRST PASS ALL GOODS ITEMS: Eliminate non goods item_hash.delete_if { |item, _qty| !item.is_goods? } tot_shipping_volume = begin item_hash.sum { |item, qty| item.total_shipping_volume * qty } rescue StandardError nil end # Iterate through our item hash and explode kits tot_shipping_weight = begin item_hash.sum { |item, qty| item.total_shipping_weight * qty } rescue StandardError nil end # Iterate through our item hash and explode kits r1 = Shipping::ItemKitExploder.process(item_hash) full_item_id_hash = r1.full_item_id_hash kit_item_ids = r1.kit_item_ids items_string = full_item_id_hash.map { |k, v| "#{k}:#{v}" }.join(',') md5 = Digest::MD5.hexdigest(items_string.to_s) Rails.logger.debug { "Shipping::Md5HashItem: #{items_string} -> #{md5}" } # SECOND PASS ALL RELEVANT ITEMS: Eliminate small items based on if it contributes less than 0.5% by volume and 2% by weight of the total, taking quantities into account if is_item || tot_shipping_volume.nil? || tot_shipping_volume.zero? || tot_shipping_weight.nil? || tot_shipping_weight.zero? # skip, we have no item weight defined whatsoever, so just keep this packing which might be useful for what can only be a limited one (or two)-off item, and move on relevant_full_item_id_hash = full_item_id_hash relevant_kit_item_ids = kit_item_ids relevant_items_string = items_string relevant_md5 = md5 Rails.logger.debug { "Shipping::Md5HashItem: item volume and/or weight not defined, skipping! relevant #{relevant_items_string} -> #{relevant_md5}" } else Rails.logger.debug { "Shipping::Md5HashItem: tot_shipping_volume: #{tot_shipping_volume} in^3, tot_shipping_weight: #{tot_shipping_weight} lbs" } Rails.logger.debug do "Shipping::Md5HashItem: sku, volume, percentage, weight, percentage: #{item_hash.map do |item, qty| [item.sku, item.shipping_volume, (100.0 * item.shipping_volume * qty / tot_shipping_volume).round(4), item.shipping_weight || item.base_weight, (100.0 * (item.shipping_weight || item.base_weight) * qty / tot_shipping_weight).round(3)] end}" end item_hash.delete_if { |item, qty| ((item.shipping_volume * qty) / tot_shipping_volume) < RELEVANT_ITEM_VOLUME_RATIO_THRESHOLD && (((item.shipping_weight || item.base_weight) * qty) / tot_shipping_weight) < RELEVANT_ITEM_WEIGHT_RATIO_THRESHOLD } # Iterate through our item hash and explode kits r2 = Shipping::ItemKitExploder.process(item_hash) relevant_full_item_id_hash = r2.full_item_id_hash relevant_kit_item_ids = r2.kit_item_ids relevant_items_string = relevant_full_item_id_hash.map { |k, v| "#{k}:#{v}" }.join(',') relevant_md5 = Digest::MD5.hexdigest(relevant_items_string.to_s) Rails.logger.debug do "Shipping::Md5HashItem: relevant sku, volume, percentage, weight, percentage: #{item_hash.map do |item, qty| [item.sku, item.shipping_volume, (100.0 * item.shipping_volume * qty / tot_shipping_volume).round(4), item.shipping_weight || item.base_weight, (100.0 * (item.shipping_weight || item.base_weight) * qty / tot_shipping_weight).round(3)] end}" end Rails.logger.debug { "Shipping::Md5HashItem: relevant #{relevant_items_string} -> #{relevant_md5}" } end Result.new(md5: md5, items_string: items_string, full_item_id_hash: full_item_id_hash, kit_item_ids: kit_item_ids, relevant_md5: relevant_md5, relevant_items_string: relevant_items_string, relevant_full_item_id_hash: relevant_full_item_id_hash, relevant_kit_item_ids: relevant_kit_item_ids) end |