Sha256: c8d1cbd8bba71fb3d2b0450bf074986ece303377103b44287cb778ffcc160047

Contents?: true

Size: 1.24 KB

Versions: 29

Compression:

Stored size: 1.24 KB

Contents

module Spree
  module Stock
    class Packer
      attr_reader :stock_location, :order, :splitters

      def initialize(stock_location, order, splitters=[Splitter::Base])
        @stock_location = stock_location
        @order = order
        @splitters = splitters
      end

      def packages
        if splitters.empty?
          [default_package]
        else
          build_splitter.split [default_package]
        end
      end

      def default_package
        package = Package.new(stock_location, order)
        order.line_items.each do |line_item|
          if line_item.should_track_inventory?
            next unless stock_location.stock_item(line_item.variant)

            on_hand, backordered = stock_location.fill_status(line_item.variant, line_item.quantity)
            package.add line_item, on_hand, :on_hand if on_hand > 0
            package.add line_item, backordered, :backordered if backordered > 0
          else
            package.add line_item, line_item.quantity, :on_hand
          end
        end
        package
      end

      private
      def build_splitter
        splitter = nil
        splitters.reverse.each do |klass|
          splitter = klass.new(self, splitter)
        end
        splitter
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
spree_core-2.3.13 app/models/spree/stock/packer.rb
spree_core-2.2.14 app/models/spree/stock/packer.rb
spree_core-2.3.12 app/models/spree/stock/packer.rb
spree_core-2.2.13 app/models/spree/stock/packer.rb
spree_core-2.3.11 app/models/spree/stock/packer.rb
spree_core-2.2.12 app/models/spree/stock/packer.rb
spree_core-2.3.10 app/models/spree/stock/packer.rb
spree_core-2.3.9 app/models/spree/stock/packer.rb
spree_core-2.2.11 app/models/spree/stock/packer.rb
spree_core-2.2.10 app/models/spree/stock/packer.rb
spree_core-2.3.8 app/models/spree/stock/packer.rb
spree_core-2.3.7 app/models/spree/stock/packer.rb
spree_core-2.2.9 app/models/spree/stock/packer.rb
spree_core-2.3.6 app/models/spree/stock/packer.rb
spree_core-2.2.8 app/models/spree/stock/packer.rb
spree_core-2.3.5 app/models/spree/stock/packer.rb
spree_core-2.2.7 app/models/spree/stock/packer.rb
spree_core-2.3.4 app/models/spree/stock/packer.rb
spree_core-2.2.6 app/models/spree/stock/packer.rb
spree_core-2.3.3 app/models/spree/stock/packer.rb