Sha256: af119430c841918bb59051dfd7ca54676370af77223621a2d422a0ac56631678

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module Spree
  class Calculator::Shipping::TntMercurioBaseCalculator < Spree::ShippingCalculator

    attr_reader :delivery_time

    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

      response[:calcula_frete_response][:out][:vl_total_frete].to_f + Spree::TntMercurioConfig.additional_value
    rescue
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree_zaez_tnt_mercurio-3.0.1 app/models/spree/calculator/shipping/tnt_mercurio_base_calculator.rb