Sha256: dafb2a5b6e6a0b17877a4195c107c740c9d1eeb990c61fbf69189a3f3b320f59

Contents?: true

Size: 1.44 KB

Versions: 28

Compression:

Stored size: 1.44 KB

Contents

module Spree
  module Stock
    class Quantifier
      attr_reader :stock_items

      def initialize(variant, stock_location = nil)
        @variant = variant
        where_args = { variant_id: @variant }
        if stock_location
          where_args.merge!(stock_location: stock_location)
        else
          where_args.merge!(Spree::StockLocation.table_name => { active: true })
        end
        @stock_items = Spree::StockItem.joins(:stock_location).where(where_args)
      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

28 entries across 28 versions & 1 rubygems

Version Path
solidus_core-1.2.3 app/models/spree/stock/quantifier.rb
solidus_core-1.1.4 app/models/spree/stock/quantifier.rb
solidus_core-1.0.7 app/models/spree/stock/quantifier.rb
solidus_core-1.0.6 app/models/spree/stock/quantifier.rb
solidus_core-1.2.2 app/models/spree/stock/quantifier.rb
solidus_core-1.2.1 app/models/spree/stock/quantifier.rb
solidus_core-1.1.3 app/models/spree/stock/quantifier.rb
solidus_core-1.0.5 app/models/spree/stock/quantifier.rb
solidus_core-1.2.0 app/models/spree/stock/quantifier.rb
solidus_core-1.2.0.rc2 app/models/spree/stock/quantifier.rb
solidus_core-1.0.4 app/models/spree/stock/quantifier.rb
solidus_core-1.1.2 app/models/spree/stock/quantifier.rb
solidus_core-1.2.0.rc1 app/models/spree/stock/quantifier.rb
solidus_core-1.2.0.beta1 app/models/spree/stock/quantifier.rb
solidus_core-1.0.3 app/models/spree/stock/quantifier.rb
solidus_core-1.1.1 app/models/spree/stock/quantifier.rb
solidus_core-1.1.0 app/models/spree/stock/quantifier.rb
solidus_core-1.1.0.pre2 app/models/spree/stock/quantifier.rb
solidus_core-1.1.0.pre1 app/models/spree/stock/quantifier.rb
solidus_core-1.1.0.beta1 app/models/spree/stock/quantifier.rb