Class: Calculator::TemandoQuote

Inherits:
Calculator
  • Object
show all
Defined in:
app/models/spree/calculator/temando_quote.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descriptionObject



14
15
16
# File 'app/models/spree/calculator/temando_quote.rb', line 14

def self.description
  I18n.t(:temando)
end

Instance Method Details

#available?(object) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/models/spree/calculator/temando_quote.rb', line 18

def available?(object)
  object.line_items.all? { |li| li.variant.temando_quotable? }
end

#cheapest(destination, line_items) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'app/models/spree/calculator/temando_quote.rb', line 22

def cheapest(destination, line_items)
  quotes = find_quotes(destination, line_items)
  return nil if quotes.nil?
  cheapest = quotes.sort_by { |q| q.total_price }.first
  if cheapest then
    pad_etas( :price => cheapest.total_price, :minimum_eta => cheapest.minimum_eta, :maximum_eta => cheapest.maximum_eta, :quote => cheapest )
  else
    nil
  end
end

#compute(object) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/spree/calculator/temando_quote.rb', line 49

def compute(object)
  if object.is_a?(Spree::Shipment) && object.temando_quotes.blank? then
    object.temando_quotes = object.order.temando_quotes
  end

  existing_quote = object.temando_quotes.find_by_calculator_id(self.id)

  if existing_quote.blank? || existing_quote.outdated? then
    Spree::Order.transaction do
      destination = case object
                    when Spree::Order
                      object.shipping_address
                    when Spree::Shipment
                      object.address
                    else
                      raise "Unknown object: #{object.inspect}"
                    end

      if preferred_express then
        data = fastest(destination.to_temando_location, object.line_items)
      else
        data = cheapest(destination.to_temando_location, object.line_items)
      end

      return if data.nil?

      quote = Spree::TemandoQuote.new_or_update_from_quote(self, object, data[:quote], destination, data.slice(:minimum_eta, :maximum_eta))

      # Store the Quote data against the Order and these LineItems if they are persisted
      if object.persisted? then
        quote.save!

        object.line_items
      end

      existing_quote = quote
    end
  end

  existing_quote.total_price
end

#fastest(destination, line_items) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/spree/calculator/temando_quote.rb', line 33

def fastest(destination, line_items)
  quotes = find_quotes(destination, line_items)
  return nil if quotes.nil?
  fastest_eta = quotes.collect(&:maximum_eta).min
  cheapest = quotes.reject { |q| q.maximum_eta > fastest_eta }.sort_by { |q| q.total_price }.first
  if cheapest then
    pad_etas( :price => cheapest.total_price, :minimum_eta => cheapest.minimum_eta, :maximum_eta => cheapest.maximum_eta, :quote => cheapest )
  else
    nil
  end
end

#temando_originObject



45
46
47
# File 'app/models/spree/calculator/temando_quote.rb', line 45

def temando_origin
  Temando::Location.new(:suburb => preferred_origin_suburb, :postcode => preferred_origin_postcode, :country => 'AU')
end