Sha256: 57e48d882d9c154b9481a61af88268667f703df1354f87a30eb7bb3227fd0985

Contents?: true

Size: 1.75 KB

Versions: 51

Compression:

Stored size: 1.75 KB

Contents

module Spree
  module Stock
    class Coordinator
      attr_reader :order

      def initialize(order)
        @order = order
      end

      def packages
        packages = build_packages
        packages = prioritize_packages(packages)
        packages = estimate_packages(packages)
      end

      # Build packages as per stock location
      #
      # It needs to check whether each stock location holds at least one stock
      # item for the order. In case none is found it wouldn't make any sense
      # to build a package because it would be empty. Plus we avoid errors down
      # the stack because it would assume the stock location has stock items
      # for the given order
      # 
      # Returns an array of Package instances
      def build_packages(packages = Array.new)
        StockLocation.active.each do |stock_location|
          next unless stock_location.stock_items.where(:variant_id => order.line_items.pluck(:variant_id)).exists?

          packer = build_packer(stock_location, order)
          packages += packer.packages
        end
        packages
      end

      private
      def prioritize_packages(packages)
        prioritizer = Prioritizer.new(order, packages)
        prioritizer.prioritized_packages
      end

      def estimate_packages(packages)
        estimator = Estimator.new(order)
        packages.each do |package|
          package.shipping_rates = estimator.shipping_rates(package)
        end
        packages
      end

      def build_packer(stock_location, order)
        Packer.new(stock_location, order, splitters(stock_location))
      end

      def splitters(stock_location)
        # extension point to return custom splitters for a location
        Rails.application.config.spree.stock_splitters
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
spree_core-2.3.13 app/models/spree/stock/coordinator.rb
spree_core-2.2.14 app/models/spree/stock/coordinator.rb
spree_core-2.3.12 app/models/spree/stock/coordinator.rb
spree_core-2.2.13 app/models/spree/stock/coordinator.rb
spree_core-2.3.11 app/models/spree/stock/coordinator.rb
spree_core-2.2.12 app/models/spree/stock/coordinator.rb
spree_core-2.3.10 app/models/spree/stock/coordinator.rb
spree_core-2.3.9 app/models/spree/stock/coordinator.rb
spree_core-2.2.11 app/models/spree/stock/coordinator.rb
spree_core-2.2.10 app/models/spree/stock/coordinator.rb
spree_core-2.3.8 app/models/spree/stock/coordinator.rb
spree_core-2.3.7 app/models/spree/stock/coordinator.rb
spree_core-2.2.9 app/models/spree/stock/coordinator.rb
spree_core-2.1.12 app/models/spree/stock/coordinator.rb
spree_core-2.3.6 app/models/spree/stock/coordinator.rb
spree_core-2.2.8 app/models/spree/stock/coordinator.rb
spree_core-2.1.11 app/models/spree/stock/coordinator.rb
spree_core-2.3.5 app/models/spree/stock/coordinator.rb
spree_core-2.2.7 app/models/spree/stock/coordinator.rb
spree_core-2.1.10 app/models/spree/stock/coordinator.rb