Sha256: cd788793493a0a60b5be4785b9e92ee422b70c593b2ef434d717efd7f5d600c0

Contents?: true

Size: 1.5 KB

Versions: 102

Compression:

Stored size: 1.5 KB

Contents

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

      def initialize(stock_location, inventory_units, splitters = [Splitter::Base])
        @stock_location = stock_location
        @inventory_units = inventory_units
        @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)

        # Group by variant_id as grouping by variant fires cached query.
        inventory_units.index_by(&:variant_id).each do |variant_id, inventory_unit|
          variant = Spree::Variant.find(variant_id)
          unit = inventory_unit.dup # Can be used by others, do not use directly
          if variant.should_track_inventory?
            next unless stock_location.stocks? variant

            on_hand, backordered = stock_location.fill_status(variant, unit.quantity)
            package.add(InventoryUnit.split(unit, backordered), :backordered) if backordered.positive?
            package.add(InventoryUnit.split(unit, on_hand), :on_hand) if on_hand.positive?
          else
            package.add unit
          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

102 entries across 102 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/models/spree/stock/packer.rb
spree_core-4.10.0 app/models/spree/stock/packer.rb
spree_core-4.9.0 app/models/spree/stock/packer.rb
spree_core-4.8.3 app/models/spree/stock/packer.rb
spree_core-4.8.2 app/models/spree/stock/packer.rb
spree_core-4.7.3 app/models/spree/stock/packer.rb
spree_core-4.6.6 app/models/spree/stock/packer.rb
spree_core-4.5.5 app/models/spree/stock/packer.rb
spree_core-4.0.9 app/models/spree/stock/packer.rb
spree_core-4.1.15 app/models/spree/stock/packer.rb
spree_core-4.2.7 app/models/spree/stock/packer.rb
spree_core-4.3.3 app/models/spree/stock/packer.rb
spree_core-4.4.1 app/models/spree/stock/packer.rb
spree_core-4.5.4 app/models/spree/stock/packer.rb
spree_core-4.6.5 app/models/spree/stock/packer.rb
spree_core-4.7.2 app/models/spree/stock/packer.rb
spree_core-4.7.1 app/models/spree/stock/packer.rb
spree_core-4.6.4 app/models/spree/stock/packer.rb
spree_core-4.7.0 app/models/spree/stock/packer.rb
spree_core-4.6.3 app/models/spree/stock/packer.rb