Class: Calculator::CorreiosBaseCalculator

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

Direct Known Subclasses

PAC, SEDEX, SEDEX10

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#delivery_timeObject (readonly)

Returns the value of attribute delivery_time.



10
11
12
# File 'app/models/spree/calculator/correios_base_calculator.rb', line 10

def delivery_time
  @delivery_time
end

Instance Method Details

#available?(order) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/spree/calculator/correios_base_calculator.rb', line 44

def available?(order)
  !compute(order).zero?
end

#compute(object) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/spree/calculator/correios_base_calculator.rb', line 12

def compute(object)
  return unless object.present? and object.line_items.present?
  order = object.is_a?(Spree::Order) ? object : object.order
  
  package = ::Correios::Frete::Pacote.new
  order.line_items.map do |item|
    weight = item.product.weight.to_f
    depth  = item.product.depth.to_f
    width  = item.product.width.to_f
    height = item.product.height.to_f
    package_item = ::Correios::Frete::PacoteItem.new(peso: weight, comprimento: depth, largura: width, altura: height)
    package.add_item(package_item)
  end
  
  calculator = ::Correios::Frete::Calculador.new do |c| 
    c.cep_origem = preferred_zipcode
    c.cep_destino = order.ship_address.zipcode
    c.encomenda = package
    c.valor_declarado = order.amount.to_f if prefers?(:declared_value)
    c.mao_propria = prefers?(:receive_in_hands)
    c.aviso_recebimento = prefers?(:receipt_notification)
    c.codigo_empresa = preferred_token if preferred_token.present?
    c.senha = preferred_password if preferred_password.present?
  end
  
  webservice = calculator.calculate(shipping_method)
  return 0.0 if webservice.erro?
  @delivery_time = webservice.prazo_entrega
  webservice.valor
rescue 0.0
end

#has_contract?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/spree/calculator/correios_base_calculator.rb', line 48

def has_contract?
  preferred_token.present? && preferred_password.present?
end