Sha256: 481392f1cf95e4b4e295214677e058426867d8e00f607e7b59ba87066d6bbbf1

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module Spree
  module Stock
    class Estimator
      attr_reader :order, :currency

      def initialize(order)
        @order = order
        @currency = order.currency
      end

      def shipping_rates(package)
        shipping_rates = Array.new
        shipping_methods = shipping_methods(package)
        return [] unless shipping_methods
        shipping_methods.each do |shipping_method|
          cost = calculate_cost(shipping_method, package)

          shipping_rates << ShippingRate.new( :shipping_method => shipping_method,
                                              :cost => cost)
        end
        shipping_rates.first.selected = true unless shipping_rates.empty?
        shipping_rates
      end

      private
      def shipping_methods(package)
        shipping_methods = package.shipping_methods
        shipping_methods.delete_if { |ship_method| !ship_method.calculator.available?(package.contents) }
        shipping_methods.delete_if { |ship_method| !ship_method.include?(order.ship_address) }
        shipping_methods.delete_if { |ship_method| !(ship_method.calculator.preferences[:currency].nil? || ship_method.calculator.preferences[:currency] == currency) }
        shipping_methods
      end

      def calculate_cost(shipping_method, package)
        shipping_method.calculator.compute(package)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_core-2.0.0 app/models/spree/stock/estimator.rb
spree_core-2.0.0.rc1 app/models/spree/stock/estimator.rb