Sha256: a31fab817fdc7b4830ef019bcfe545431832f125133c06b27a3649768d0fec3c

Contents?: true

Size: 1.06 KB

Versions: 90

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Spree
  module Stock
    class Differentiator
      attr_reader :missing, :packed, :required, :packages, :order

      def initialize(order, packages)
        @order = order
        @packages = packages
        build_packed
        build_required
        build_missing
      end

      def missing?
        missing.values.any? { |value| value > 0 }
      end

      private

      def build_missing
        @missing = Hash.new(0)
        required.keys.each do |variant|
           missing = required[variant] - packed[variant]
           @missing[variant] = missing if missing > 0
        end
      end

      def build_packed
        @packed = Hash.new(0)
        packages.each do |package|
          package.contents.each do |content_item|
            @packed[content_item.variant] += content_item.quantity
          end
        end
      end

      def build_required
        @required = Hash.new(0)
        order.line_items.each do |line_item|
          @required[line_item.variant] = line_item.quantity
        end
      end
    end
  end
end

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
solidus_core-3.3.0 app/models/spree/stock/differentiator.rb
solidus_core-3.2.5 app/models/spree/stock/differentiator.rb
solidus_core-3.2.4 app/models/spree/stock/differentiator.rb
solidus_core-3.2.3 app/models/spree/stock/differentiator.rb
solidus_core-3.1.8 app/models/spree/stock/differentiator.rb
solidus_core-3.0.8 app/models/spree/stock/differentiator.rb
solidus_core-3.2.2 app/models/spree/stock/differentiator.rb
solidus_core-3.2.1 app/models/spree/stock/differentiator.rb
solidus_core-3.2.0 app/models/spree/stock/differentiator.rb
solidus_core-3.2.0.alpha app/models/spree/stock/differentiator.rb
solidus_core-3.0.7 app/models/spree/stock/differentiator.rb
solidus_core-2.11.17 app/models/spree/stock/differentiator.rb
solidus_core-3.1.7 app/models/spree/stock/differentiator.rb
solidus_core-2.11.16 app/models/spree/stock/differentiator.rb
solidus_core-3.0.6 app/models/spree/stock/differentiator.rb
solidus_core-3.1.6 app/models/spree/stock/differentiator.rb
solidus_core-2.11.15 app/models/spree/stock/differentiator.rb
solidus_core-2.11.14 app/models/spree/stock/differentiator.rb
solidus_core-3.0.5 app/models/spree/stock/differentiator.rb
solidus_core-3.1.5 app/models/spree/stock/differentiator.rb