Class: Inventory::QuotedRemainingItems
- Inherits:
-
BaseService
- Object
- BaseService
- Inventory::QuotedRemainingItems
- Defined in:
- app/services/inventory/quoted_remaining_items.rb
Overview
Service object: quoted remaining items.
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
- #line_item_children(quote) ⇒ Object
- #line_item_parents(quote) ⇒ Object
- #line_items(quote) ⇒ Object
- #process(quote, type) ⇒ Object
Instance Method Details
#line_item_children(quote) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/services/inventory/quoted_remaining_items.rb', line 67 def line_item_children(quote) item_children_groups = quote.line_items.where.not(parent_id: nil).non_shipping.includes(:item, :room_configuration, { catalog_item: :store_item }).each_with_object({}) do |line_item, hsh| parent_item_id = LineItem.find(line_item.parent_id).item_id hsh[parent_item_id] ||= { line_item.item_id => { sku: line_item.item.sku, name: line_item.item.name } } hsh[parent_item_id][line_item.item_id] ||= { sku: line_item.item.sku, name: line_item.item.name } hsh[parent_item_id][line_item.item_id][:original_qty] ||= 0 hsh[parent_item_id][line_item.item_id][:original_qty] += line_item.quantity hsh[parent_item_id][line_item.item_id][:ordered_qty] ||= 0 # Figure out the ordered quantity, is this line in an order? if line_item.room_configuration&.orders&.invoiced&.exists? hsh[parent_item_id][line_item.item_id][:ordered_qty] += line_item.quantity hsh[parent_item_id][line_item.item_id][:orders] = line_item.room_configuration.orders.pluck(:reference_number) end # Store on hand quantity hsh[parent_item_id][line_item.item_id][:on_hand_qty] ||= line_item.catalog_item.store_item.qty_on_hand # Store committed quantity hsh[parent_item_id][line_item.item_id][:committed_qty] ||= line_item.catalog_item.store_item.qty_committed # Store available quantity hsh[parent_item_id][line_item.item_id][:available_qty] ||= line_item.catalog_item.store_item.qty_available hsh[parent_item_id][line_item.item_id][:is_kit] ||= line_item.catalog_item.store_item.is_kit? # hsh[line_item.item_id][:parent_item_id] ||= LineItem.find(line_item.parent_id).item_id if line_item.parent_id? hsh[parent_item_id][line_item.item_id][:on_order] ||= line_item.catalog_item.store_item.on_order hsh[parent_item_id][line_item.item_id][:backordered_qty] ||= LineItem.joins(:order).merge(Order.back_order).where(catalog_item_id: line_item.catalog_item_id).sum(:quantity) item_available_by_branch = Item.find(line_item.item_id).store_items.available.where(store_id: [1, 2]) store_available = [] item_available_by_branch.each do |r| warehouse = r[:store_id] == 1 ? 'US Warehouse' : 'CA Warehouse' store_available << { store: warehouse, qty_available: (r[:qty_on_hand] - r[:qty_committed]) } end hsh[parent_item_id][line_item.item_id][:store_available] ||= store_available end # Computer remaining item_children_groups.each do |parent_item_id, parent_hsh| parent_hsh.each do |item_id, hsh| item_children_groups[parent_item_id][item_id][:remaining_qty] = hsh[:original_qty] - hsh[:ordered_qty] if hsh[:original_qty] && hsh[:ordered_qty] if item_children_groups[parent_item_id][item_id][:remaining_qty] && hsh[:available_qty] remaining = hsh[:available_qty] - item_children_groups[parent_item_id][item_id][:remaining_qty] item_children_groups[parent_item_id][item_id][:shortage] = [remaining, 0].min end end item_children_groups[parent_item_id] = parent_hsh.sort_by { |_item_id, hsh| [hsh[:shortage], -hsh[:remaining_qty]] }.to_h end end |
#line_item_parents(quote) ⇒ Object
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 |
# File 'app/services/inventory/quoted_remaining_items.rb', line 25 def line_item_parents(quote) item_parent_groups = quote.line_items.where(parent_id: nil).non_shipping.includes(:item, :room_configuration, { catalog_item: :store_item }).each_with_object({}) do |line_item, hsh| hsh[line_item.item_id] ||= { sku: line_item.item.sku, name: line_item.item.name } hsh[line_item.item_id][:original_qty] ||= 0 hsh[line_item.item_id][:original_qty] += line_item.quantity hsh[line_item.item_id][:ordered_qty] ||= 0 # Figure out the ordered quantity, is this line in an order? if line_item.room_configuration&.orders&.invoiced&.exists? hsh[line_item.item_id][:ordered_qty] += line_item.quantity hsh[line_item.item_id][:orders] = line_item.room_configuration.orders.pluck(:reference_number) end # Store on hand quantity hsh[line_item.item_id][:on_hand_qty] ||= line_item.catalog_item.store_item.qty_on_hand # Store committed quantity hsh[line_item.item_id][:committed_qty] ||= line_item.catalog_item.store_item.qty_committed # Store available quantity hsh[line_item.item_id][:available_qty] ||= line_item.catalog_item.store_item.qty_available hsh[line_item.item_id][:is_kit] ||= line_item.catalog_item.store_item.is_kit? # hsh[line_item.item_id][:parent_item_id] ||= LineItem.find(line_item.parent_id).item_id if line_item.parent_id? hsh[line_item.item_id][:on_order] ||= line_item.catalog_item.store_item.on_order hsh[line_item.item_id][:backordered_qty] ||= LineItem.joins(:order).merge(Order.back_order).where(catalog_item_id: line_item.catalog_item_id).sum(:quantity) item_available_by_branch = Item.find(line_item.item_id).store_items.available.where(store_id: Company.where(market_zone: quote.company.market_zone).where.not(id: quote.company.id).pluck(:id)) store_available = [] item_available_by_branch.each do |r| warehouse = "#{r.store.short_name} Warehouse" store_available << { store: warehouse, qty_available: (r[:qty_on_hand] - r[:qty_committed]) } end hsh[line_item.item_id][:store_available] ||= store_available end # Computer remaining item_parent_groups.each do |item_id, hsh| item_parent_groups[item_id][:remaining_qty] = hsh[:original_qty] - hsh[:ordered_qty] if hsh[:original_qty] && hsh[:ordered_qty] if item_parent_groups[item_id][:remaining_qty] && hsh[:available_qty] remaining = hsh[:available_qty] - item_parent_groups[item_id][:remaining_qty] item_parent_groups[item_id][:shortage] = [remaining, 0].min end end item_parent_groups = item_parent_groups.sort_by { |_item_id, hsh| [hsh[:shortage], -hsh[:remaining_qty]] }.to_h end |
#line_items(quote) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/services/inventory/quoted_remaining_items.rb', line 113 def line_items(quote) total_items = [] total_line_items = [] item_groups = [] quote.line_items.non_shipping.includes(:item, :room_configuration, { catalog_item: :store_item }).find_each do |line_item| # Store available quantity per item total_items << [line_item.item_id, line_item.catalog_item.store_item.qty_available] # Qty per line item total_line_items << [line_item.id, line_item.item_id, line_item.quantity] end total_items = total_items.uniq total_line_items = total_line_items.uniq total_line_items.each do |li| line_item_id = li[0] line_item_item_id = li[1] line_item_qty = li[2] item_qty_available_updated = 0 total_items.each do |i| item_id = i[0] item_qty_available = i[1] next unless line_item_item_id == item_id current_item_qty_available = item_qty_available - line_item_qty item_qty_available_updated = current_item_qty_available item_groups << [line_item_id, line_item_item_id, line_item_qty, current_item_qty_available] end # Update only the matched item's qty in place; pass everything else # through unchanged. Earlier RuboCop autocorrect collapsed this to a # `... if cond` modifier which returned nil for unmatched rows and # blew up the next outer-loop iteration with NoMethodError. total_items = total_items.map do |r| r[0] == line_item_item_id ? [r[0], item_qty_available_updated] : r end end total_items = total_items.sort_by { |a, _b, _c, _d| a } item_groups end |
#process(quote, type) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/services/inventory/quoted_remaining_items.rb', line 9 def process(quote, type) if type == 1 @item_groups = line_item_parents(quote) lic = line_item_children(quote) @item_groups.each do |item_id, _hsh| lic.each do |parent_item_id, c_hsh| @item_groups[item_id][:kit_contents] = c_hsh if item_id == parent_item_id end end elsif type == 2 @item_groups = line_items(quote) end Result.new(item_groups: @item_groups) end |