Sha256: 28a781c97c0d6f83dd293d9529738f06f6dc28ad9888721e6fbde212f831afda

Contents?: true

Size: 1.28 KB

Versions: 59

Compression:

Stored size: 1.28 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.sum if on_hand.present?

          # Allocate remaining desired inventory from backorders
          backordered = allocate_backordered(desired)
          desired -= backordered.values.sum 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

59 entries across 59 versions & 1 rubygems

Version Path
solidus_core-3.1.9 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.1.8 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.0.8 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.0.7 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-2.11.17 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.1.7 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-2.11.16 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.0.6 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.1.6 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-2.11.15 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-2.11.14 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.0.5 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.1.5 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-2.11.13 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.0.4 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.1.4 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-2.11.12 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.0.3 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.1.3 app/models/spree/stock/allocator/on_hand_first.rb
solidus_core-3.1.2 app/models/spree/stock/allocator/on_hand_first.rb