Sha256: 566bc5cb4d43df2d30201263fe600866927359c9aed9d72d3fde066b68eb68ad

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

module Spree
  module Stock
    class Quantifier
      attr_reader :variant, :stock_location

      def initialize(variant, stock_location = nil)
        @variant        = variant
        @stock_location = stock_location
      end

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

      def backorderable?
        @backorderable ||= stock_items.any?(&:backorderable)
      end

      def can_supply?(required = 1)
        variant.available? && (total_on_hand >= required || backorderable?)
      end

      def stock_items
        @stock_items ||= scope_to_location(variant.stock_items)
      end

      private

      def scope_to_location(collection)
        return collection.with_active_stock_location unless stock_location.present?

        collection.where(stock_location: stock_location)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spree_core-4.3.3 app/models/spree/stock/quantifier.rb
spree_core-4.3.2 app/models/spree/stock/quantifier.rb
spree_core-4.3.1 app/models/spree/stock/quantifier.rb
spree_core-4.3.0 app/models/spree/stock/quantifier.rb
spree_core-4.3.0.rc3 app/models/spree/stock/quantifier.rb
spree_core-4.3.0.rc2 app/models/spree/stock/quantifier.rb
spree_core-4.3.0.rc1 app/models/spree/stock/quantifier.rb