Sha256: 506c14ba93c8618e673cf44e607b30a7fe2b35230e9d61282e1689f0fe591db8

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 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_for_estimate(requested_line_items)
        build_by_requested_line_items(requested_line_items)
      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

1 entries across 1 versions & 1 rubygems

Version Path
solidus-returnly-0.10.0 lib/returnly/builders/return_item.rb