Class: Inventory::RoomConfigurationRemainingItems
- Inherits:
-
BaseService
- Object
- BaseService
- Inventory::RoomConfigurationRemainingItems
- Defined in:
- app/services/inventory/room_configuration_remaining_items.rb
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
Instance Method Details
#line_items(room_configuration) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 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 |
# File 'app/services/inventory/room_configuration_remaining_items.rb', line 13 def line_items(room_configuration) total_items = [] total_line_items = [] item_groups = [] room_configuration.line_items.non_shipping.includes(:item, { 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. RuboCop autocorrect once collapsed this to a # `... if cond` modifier (returns nil for the else branch) and # crashed the next outer iteration. See quoted_remaining_items.rb # for the matching fix. 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 |