Sha256: 911fbe46478c97d9cbe7bcb7605ade7f1e945e07efd909936c02aed4eed481eb

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spree-returnly-0.13.1 lib/returnly/builders/return_item.rb
solidus-returnly-0.13.1 lib/returnly/builders/return_item.rb
spree-returnly-0.13.0 lib/returnly/builders/return_item.rb
solidus-returnly-0.13.0 lib/returnly/builders/return_item.rb
solidus-returnly-0.12.0 lib/returnly/builders/return_item.rb
solidus-returnly-0.9.0 lib/returnly/builders/return_item.rb