Sha256: e0ab5357efc448a3e0cc94fb4def1326e43ea5cb852d616a1832dc19d751708b

Contents?: true

Size: 1.64 KB

Versions: 86

Compression:

Stored size: 1.64 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 The stock_location to check inventory in. If unspecified it will check inventory in all available StockLocations
      def initialize(variant, stock_location = nil)
        @variant = variant
        @stock_items = Spree::StockItem.where(variant_id: variant)
        if stock_location
          @stock_items.where!(stock_location: stock_location)
        else
          @stock_items.joins!(:stock_location).merge!(Spree::StockLocation.active)
        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

86 entries across 86 versions & 2 rubygems

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