Sha256: 29f83fbb57b06fe01522088c292a5cc190b19a4272b69a9e902fc5a53959442f

Contents?: true

Size: 937 Bytes

Versions: 8

Compression:

Stored size: 937 Bytes

Contents

module Spree
  module Stock
    class Quantifier
      attr_reader :stock_items

      def initialize(variant)
        @variant = resolve_variant_id(variant)
        @stock_items = Spree::StockItem.joins(:stock_location).where(:variant_id => @variant, Spree::StockLocation.table_name =>{ :active => true})
      end

      def total_on_hand
        if @variant.should_track_inventory?
          stock_items.sum(:count_on_hand)
        else
          Float::INFINITY
        end
      end

      def backorderable?
        stock_items.any?(&:backorderable)
      end

      def can_supply?(required)
        total_on_hand >= required || backorderable?
      end

      private

      # return variant when passed either variant object or variant id
      def resolve_variant_id(variant)
        variant = Spree::Variant.find_by_id(variant) unless variant.respond_to?(:should_track_inventory?)
        variant
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spree_core-2.0.10 app/models/spree/stock/quantifier.rb
spree_core-2.1.6 app/models/spree/stock/quantifier.rb
spree_core-2.2.1 app/models/spree/stock/quantifier.rb
spree_core-2.2.0 app/models/spree/stock/quantifier.rb
spree_core-2.1.5 app/models/spree/stock/quantifier.rb
spree_core-2.0.9 app/models/spree/stock/quantifier.rb
spree_core-2.1.4 app/models/spree/stock/quantifier.rb
spree_core-2.0.8 app/models/spree/stock/quantifier.rb