Sha256: 9eb374bcf98150445acfef45907418a20a8690747f2d995833edcc905f32ceff

Contents?: true

Size: 1.3 KB

Versions: 52

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Spree
  module Stock
    module Allocator
      class OnHandFirst < Spree::Stock::Allocator::Base
        def allocate_inventory(desired)
          # Allocate any available on hand inventory
          on_hand = allocate_on_hand(desired)
          desired -= on_hand.values.reduce(&:+) if on_hand.present?

          # Allocate remaining desired inventory from backorders
          backordered = allocate_backordered(desired)
          desired -= backordered.values.reduce(&:+) if backordered.present?

          # If all works at this point desired must be empty
          [on_hand, backordered, desired]
        end

        protected

        def allocate_on_hand(desired)
          allocate(availability.on_hand_by_stock_location_id, desired)
        end

        def allocate_backordered(desired)
          allocate(availability.backorderable_by_stock_location_id, desired)
        end

        def allocate(availability_by_location, desired)
          availability_by_location.transform_values do |available|
            # Find the desired inventory which is available at this location
            packaged = available & desired
            # Remove found inventory from desired
            desired -= packaged
            packaged
          end
        end
      end
    end
  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
solidus_core-4.5.1 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.5.0 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.3.6 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.2.5 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.1.6 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.4.2 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.4.1 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.4.0 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.3.4 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.2.4 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.1.5 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.3.3 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.3.2 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.1.4 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.3.1 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.3.0 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.2.3 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.1.3 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-4.0.4 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.4.6 app/models/spree/stock/allocator/on_hand_first.rb