Sha256: f2c1a83f0020550a54ac6c524d5891a3a43ed0aee2e42ae4bccf31d1a0b9b55b

Contents?: true

Size: 1.76 KB

Versions: 46

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Spree
  module Stock
    class Quantifier
      attr_reader :stock_items

      # @param [Variant] variant The variant to check inventory for.
      # @param [StockLocation, Integer] stock_location_or_id
      #        The stock_location or stock location ID to check inventory in.
      #        If unspecified it will check inventory in all available StockLocations
      def initialize(variant, stock_location_or_id = nil)
        @variant = variant
        @stock_items = variant.stock_items.select do |stock_item|
          if stock_location_or_id
            stock_item.stock_location == stock_location_or_id ||
              stock_item.stock_location_id == stock_location_or_id
          else
            stock_item.stock_location.active?
          end
        end
      end

      # Returns the total number of inventory units on hand for the variant.
      #
      # @return [Fixnum] number of inventory units on hand, or infinity if
      #   inventory is not tracked on the variant.
      def total_on_hand
        if @variant.should_track_inventory?
          stock_items.sum(&:count_on_hand)
        else
          Float::INFINITY
        end
      end

      # Checks if any of its stock items are backorderable.
      #
      # @return [Boolean] true if any stock items are backorderable
      def backorderable?
        stock_items.any?(&:backorderable)
      end

      # Checks if it is possible to supply a given number of units.
      #
      # @param required [Fixnum] the number of required stock units
      # @return [Boolean] true if we have the required amount on hand or the
      #   variant is backorderable, otherwise false
      def can_supply?(required)
        total_on_hand >= required || backorderable?
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
solidus_core-4.4.1 app/models/spree/stock/quantifier.rb
solidus_core-4.4.0 app/models/spree/stock/quantifier.rb
solidus_core-4.3.4 app/models/spree/stock/quantifier.rb
solidus_core-4.2.4 app/models/spree/stock/quantifier.rb
solidus_core-4.1.5 app/models/spree/stock/quantifier.rb
solidus_core-4.3.3 app/models/spree/stock/quantifier.rb
solidus_core-4.3.2 app/models/spree/stock/quantifier.rb
solidus_core-4.1.4 app/models/spree/stock/quantifier.rb
solidus_core-4.3.1 app/models/spree/stock/quantifier.rb
solidus_core-4.3.0 app/models/spree/stock/quantifier.rb
solidus_core-4.2.3 app/models/spree/stock/quantifier.rb
solidus_core-4.1.3 app/models/spree/stock/quantifier.rb
solidus_core-4.0.4 app/models/spree/stock/quantifier.rb
solidus_core-3.4.6 app/models/spree/stock/quantifier.rb
solidus_core-4.0.3 app/models/spree/stock/quantifier.rb
solidus_core-4.1.2 app/models/spree/stock/quantifier.rb
solidus_core-4.2.2 app/models/spree/stock/quantifier.rb
solidus_core-3.4.5 app/models/spree/stock/quantifier.rb
solidus_core-4.2.1 app/models/spree/stock/quantifier.rb
solidus_core-4.2.0 app/models/spree/stock/quantifier.rb