Sha256: 78171e739a773a2a8cd64adca94aca15db0523168b38f16089f4712ea07dcf25

Contents?: true

Size: 1.78 KB

Versions: 19

Compression:

Stored size: 1.78 KB

Contents

module Spree
  module Stock
    class Coordinator
      attr_reader :order, :inventory_units

      def initialize(order, inventory_units = nil)
        @order = order
        @inventory_units = inventory_units || InventoryUnitBuilder.new(order).units
      end

      def shipments
        packages.map do |package|
          package.to_shipment.tap { |s| s.address = order.ship_address }
        end
      end

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

      def build_packages(packages = Array.new)
        stock_locations_with_requested_variants.each do |stock_location|
          packages += build_packer(stock_location, inventory_units).packages
        end

        packages
      end

      private

      def stock_locations_with_requested_variants
        Spree::StockLocation.active.joins(:stock_items).
          where(spree_stock_items: { variant_id: requested_variant_ids })
      end

      def requested_variant_ids
        inventory_units.map(&:variant_id).uniq
      end

      def prioritize_packages(packages)
        prioritizer = Prioritizer.new(inventory_units, 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, inventory_units)
        Packer.new(stock_location, inventory_units, 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

19 entries across 19 versions & 1 rubygems

Version Path
spree_core-3.1.14 app/models/spree/stock/coordinator.rb
spree_core-3.1.13 app/models/spree/stock/coordinator.rb
spree_core-3.1.12 app/models/spree/stock/coordinator.rb
spree_core-3.1.11 app/models/spree/stock/coordinator.rb
spree_core-3.1.10 app/models/spree/stock/coordinator.rb
spree_core-3.1.9 app/models/spree/stock/coordinator.rb
spree_core-3.1.8 app/models/spree/stock/coordinator.rb
spree_core-3.1.7 app/models/spree/stock/coordinator.rb
spree_core-3.1.6 app/models/spree/stock/coordinator.rb
spree_core-3.1.5 app/models/spree/stock/coordinator.rb
spree_core-3.1.4 app/models/spree/stock/coordinator.rb
spree_core-3.1.3 app/models/spree/stock/coordinator.rb
spree_core-3.1.2 app/models/spree/stock/coordinator.rb
spree_core-3.1.1 app/models/spree/stock/coordinator.rb
spree_core-3.1.0 app/models/spree/stock/coordinator.rb
spree_core-3.1.0.rc4 app/models/spree/stock/coordinator.rb
spree_core-3.1.0.rc3 app/models/spree/stock/coordinator.rb
spree_core-3.1.0.rc2 app/models/spree/stock/coordinator.rb
spree_core-3.1.0.rc1 app/models/spree/stock/coordinator.rb