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.2.4 app/models/spree/stock/packer.rb
spree_core-3.7.14.1 app/models/spree/stock/packer.rb
spree_core-4.2.3.1 app/models/spree/stock/packer.rb
spree_core-4.1.13.1 app/models/spree/stock/packer.rb
spree_core-4.0.7.1 app/models/spree/stock/packer.rb
spree_core-4.2.3 app/models/spree/stock/packer.rb
spree_core-4.2.2 app/models/spree/stock/packer.rb
spree_core-4.2.1 app/models/spree/stock/packer.rb
spree_core-4.0.7 app/models/spree/stock/packer.rb
spree_core-3.7.14 app/models/spree/stock/packer.rb
spree_core-4.2.0 app/models/spree/stock/packer.rb
spree_core-4.2.0.rc5 app/models/spree/stock/packer.rb
spree_core-4.2.0.rc4 app/models/spree/stock/packer.rb
spree_core-4.2.0.rc3 app/models/spree/stock/packer.rb
spree_core-4.1.13 app/models/spree/stock/packer.rb
spree_core-4.0.6 app/models/spree/stock/packer.rb
spree_core-4.2.0.rc2 app/models/spree/stock/packer.rb
spree_core-3.7.13 app/models/spree/stock/packer.rb
spree_core-4.0.5 app/models/spree/stock/packer.rb
spree_core-4.1.12 app/models/spree/stock/packer.rb