Sha256: 4373eddfe3e72143d811b84871e6691f0d341d23a9839f54fa2b9e65232d30ba

Contents?: true

Size: 1.12 KB

Versions: 43

Compression:

Stored size: 1.12 KB

Contents

module Spree
  module Stock
    class OrderCounter
      attr_reader :order

      def initialize(order)
        @order = order
        @ordered_counts = count_line_items
        @assigned_counts = count_inventory_units
      end

      def variants
        @ordered_counts.keys
      end

      def variants_with_remaining
        variants.select { |variant| remaining(variant) > 0 }
      end

      def remaining?
        not variants_with_remaining.empty?
      end

      def ordered(variant)
        @ordered_counts[variant]
      end

      def assigned(variant)
        @assigned_counts[variant]
      end

      def remaining(variant)
        @ordered_counts[variant] - @assigned_counts[variant]
      end

      private
      def count_line_items
        counts = Hash.new(0)
        order.line_items.each do |line_item|
          counts[line_item.variant] += line_item.quantity
        end
        counts
      end

      def count_inventory_units
        counts = Hash.new(0)
        order.inventory_units.each do |inventory_unit|
          counts[inventory_unit.variant] += 1
        end
        counts
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
spree_core-2.2.14 app/models/spree/stock/order_counter.rb
spree_core-2.2.13 app/models/spree/stock/order_counter.rb
spree_core-2.2.12 app/models/spree/stock/order_counter.rb
spree_core-2.2.11 app/models/spree/stock/order_counter.rb
spree_core-2.2.10 app/models/spree/stock/order_counter.rb
spree_core-2.2.9 app/models/spree/stock/order_counter.rb
spree_core-2.1.12 app/models/spree/stock/order_counter.rb
spree_core-2.2.8 app/models/spree/stock/order_counter.rb
spree_core-2.1.11 app/models/spree/stock/order_counter.rb
spree_core-2.2.7 app/models/spree/stock/order_counter.rb
spree_core-2.1.10 app/models/spree/stock/order_counter.rb
spree_core-2.0.13 app/models/spree/stock/order_counter.rb
spree_core-2.1.9 app/models/spree/stock/order_counter.rb
spree_core-2.2.6 app/models/spree/stock/order_counter.rb
spree_core-2.2.5 app/models/spree/stock/order_counter.rb
spree_core-2.1.8 app/models/spree/stock/order_counter.rb
spree_core-2.0.12 app/models/spree/stock/order_counter.rb
spree_core-2.2.4 app/models/spree/stock/order_counter.rb
spree_core-2.2.3 app/models/spree/stock/order_counter.rb
spree_core-2.2.2 app/models/spree/stock/order_counter.rb