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-4.4.2 app/models/spree/stock/differentiator.rb
solidus_core-4.4.1 app/models/spree/stock/differentiator.rb
solidus_core-4.4.0 app/models/spree/stock/differentiator.rb
solidus_core-4.3.4 app/models/spree/stock/differentiator.rb
solidus_core-4.2.4 app/models/spree/stock/differentiator.rb
solidus_core-4.1.5 app/models/spree/stock/differentiator.rb
solidus_core-4.3.3 app/models/spree/stock/differentiator.rb
solidus_core-4.3.2 app/models/spree/stock/differentiator.rb
solidus_core-4.1.4 app/models/spree/stock/differentiator.rb
solidus_core-4.3.1 app/models/spree/stock/differentiator.rb
solidus_core-4.3.0 app/models/spree/stock/differentiator.rb
solidus_core-4.2.3 app/models/spree/stock/differentiator.rb
solidus_core-4.1.3 app/models/spree/stock/differentiator.rb
solidus_core-4.0.4 app/models/spree/stock/differentiator.rb
solidus_core-3.4.6 app/models/spree/stock/differentiator.rb
solidus_core-4.0.3 app/models/spree/stock/differentiator.rb
solidus_core-4.1.2 app/models/spree/stock/differentiator.rb
solidus_core-4.2.2 app/models/spree/stock/differentiator.rb
solidus_core-3.4.5 app/models/spree/stock/differentiator.rb
solidus_core-4.2.1 app/models/spree/stock/differentiator.rb