module Returnly module Builders class ReturnItem attr_accessor :order def initialize(order) @order = order end def build_by_requested_line_items(requested_line_items) requested_line_items.each_with_object([]) do |request_line_item, return_items| quantity = request_line_item[:units].to_i next return_items unless quantity > 0 inventory_units_by(request_line_item[:order_line_item_id]).take(quantity).each do |inventory_unit| return_items << build_by_inventory_unit(inventory_unit, request_line_item) end return_items end end def build_by_inventory_unit(inventory_unit, options = {}) Spree::ReturnItem.new( amount: inventory_unit.line_item.price, acceptance_status: 'accepted', inventory_unit_id: inventory_unit.id, reception_status_event: 'receive', resellable: options[:restock] ) end private def inventory_units_by(line_item_ids) order.inventory_units.includes(:line_item) .where(line_item_id: line_item_ids) .where.not(state: 'returned') end end end end