Sha256: 45fcccc791a11442f2bd2a94459cc6d9d04e6f93586513a09084563872d3a695

Contents?: true

Size: 1.49 KB

Versions: 11

Compression:

Stored size: 1.49 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

11 entries across 11 versions & 1 rubygems

Version Path
spree_core-3.6.6 app/models/spree/stock/packer.rb
spree_core-3.6.5 app/models/spree/stock/packer.rb
spree_core-3.6.4 app/models/spree/stock/packer.rb
spree_core-3.6.3 app/models/spree/stock/packer.rb
spree_core-3.6.2 app/models/spree/stock/packer.rb
spree_core-3.6.1 app/models/spree/stock/packer.rb
spree_core-3.6.0 app/models/spree/stock/packer.rb
spree_core-3.5.0 app/models/spree/stock/packer.rb
spree_core-3.6.0.rc1 app/models/spree/stock/packer.rb
spree_core-3.5.0.rc2 app/models/spree/stock/packer.rb
spree_core-3.5.0.rc1 app/models/spree/stock/packer.rb