Class: Calculator::Shipping::TntMercurioBaseCalculator

Inherits:
Spree::ShippingCalculator
  • Object
show all
Defined in:
app/models/spree/calculator/shipping/tnt_mercurio_base_calculator.rb

Direct Known Subclasses

TntMercurioANC, TntMercurioRNC

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#delivery_timeObject (readonly)

Returns the value of attribute delivery_time.



4
5
6
# File 'app/models/spree/calculator/shipping/tnt_mercurio_base_calculator.rb', line 4

def delivery_time
  @delivery_time
end

Instance Method Details

#compute_package(object) ⇒ Object



6
7
8
9
10
11
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
# File 'app/models/spree/calculator/shipping/tnt_mercurio_base_calculator.rb', line 6

def compute_package(object)
  return if object.nil?
  order = if object.is_a?(Spree::Order) then object else object.order end

  stock_location = object.stock_location

  cgc_customer = order.user.attributes[Spree::TntMercurioConfig.customer_field]
  cgc_type = if cgc_customer.delete('.').delete('-').size <= 11 then 'F' else 'J' end

  params = {in0: {login: Spree::TntMercurioConfig.email,
                  senha: Spree::TntMercurioConfig.password,
                  nr_identif_cliente_rem: Spree::TntMercurioConfig.cgc,
                  nr_inscricao_estadual_remetente: Spree::TntMercurioConfig.state_registry,
                  nr_identif_cliente_dest: cgc_customer,
                  tp_situacao_tributaria_remetente: Spree::TntMercurioConfig.tax_situation,
                  tp_pessoa_remetente: Spree::TntMercurioConfig.type_cgc,
                  tp_pessoa_destinatario: cgc_type,
                  tp_situacao_tributaria_destinatario: 'NC',
                  cep_origem: stock_location.zipcode,
                  cep_destino: order.ship_address.zipcode,
                  vl_mercadoria: order.amount.to_s,
                  ps_real: object.weight.to_s,
                  tp_servico: shipping_method,
                  tp_frete: Spree::TntMercurioConfig.billet_type,
                  cd_divisao_cliente: Spree::TntMercurioConfig.division}}

  client = Savon.client(wsdl: 'http://ws.tntbrasil.com.br/servicos/CalculoFrete?wsdl')
  response = client.call(:calcula_frete, message: params).body

  @delivery_time = response[:calcula_frete_response][:out][:prazo_entrega].to_i + Spree::TntMercurioConfig.additional_days

  cost = response[:calcula_frete_response][:out][:vl_total_frete].to_f + Spree::TntMercurioConfig.additional_value
  {cost: cost, delivery_time: @delivery_time}
rescue
  {}
end