Skip to content

Commit

Permalink
#4 - Criado classe e regras para o download da NF-e
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunomm committed Dec 21, 2016
1 parent 2bd8b61 commit 8e0178d
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/br_nfe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ module Evento
autoload :NfeAutorizacao
autoload :NfeConsultaAutorizacao
autoload :NfeConsultaProtocolo
autoload :NfeDownloadNf
autoload :NfeInutilizacao
autoload :NfeRecepcaoEvento
end
Expand Down
5 changes: 2 additions & 3 deletions lib/br_nfe/product/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def inicio_contingencia
validates :tipo_emissao, inclusion: {in: [:normal, :svc]}
validates :inicio_contingencia, presence: true, if: :contingencia?
validates :motivo_contingencia, length: { minimum: 15, maximum: 256 }, if: :contingencia?



def default_values
{
tipo_emissao: :normal,
Expand All @@ -48,7 +47,7 @@ def ssl_request?
# <b>Tipo de retorno: </b> _Object (Gateway::Base ou derivados)_
#
def gateway
case tipo_emissao
@gateway ||= case tipo_emissao
when :normal
get_gateway_by_normal_operation
when :svc
Expand Down
84 changes: 84 additions & 0 deletions lib/br_nfe/product/nfe_download_nf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module BrNfe
module Product
class NfeDownloadNf < BrNfe::Product::Base
# UTILIZADO PARA FORÇAR A UTILIZAÇÃO DE OUTRO GATEWAY
# O Gateway padrão da GEM para o download da NF é o
# webservice do AN(Ambiente Nacional) (Exceto para o estado do
# Ceará(CE), na qual possui url própria para download da NF).
# Outro Webservice que pode ser utilizado é do SVAN (Servidor Virtual do
# Ambiente Nacional).
# Pode ser modificado o servidor passando o attr :force_gateway com
# um dos seguintes valores:
# - :SVAN - Na qual irá forçar o uso do Servidor Virtual do Ambiente Nacional
# - :AN - Na qual irá forçar o uso do Ambiente Nacional (apenas para o estado
# do Ceará, já que esse é o servidor padrão para os demais estados)
#
# <b>Type: </b> _Symbol_
# <b>Required: </b> _No_
# <b>Example: </b> _:SVAN_
# <b>Default: </b> _nil_
#
attr_accessor :force_gateway
def force_gateway=(value)
@force_gateway = "#{value}".to_sym
end

# CHAVE DE ACESSO DA NF-E.
#
# <b>Type: </b> _Number_
# <b>Required: </b> _Yes_
# <b>Example: </b> _42000082176983000186550000000000006313331836_
# <b>Length: </b> _44_
# <b>tag: </b> chNFe
#
attr_accessor :chave_nfe
alias_attribute :chNFe, :chave_nfe

validates :chave_nfe, presence: true
validates :chave_nfe, length: {is: 44}

def gateway
@gateway ||= case force_gateway
when :SVAN
BrNfe::Product::Gateway::WebServiceSVAN.new({env: env})
when :AN
BrNfe::Product::Gateway::Base.new({env: env})
else
super
end
end


# URL do webservice para enviar as informações.
def wsdl
gateway.wsdl_download_nf
end

# Método SOAP que será chamado para enviar o XML
def method_wsdl
gateway.operation_download_nf
end

# Versão utilizada pelo webservice do estado para determinada ação.
def gateway_xml_version
gateway.version_xml_download_nf
end

# URL que será setada no atribto xmlns do XML;
def url_xmlns
gateway.url_xmlns_download_nf
end

# Versão SSL utilizada pelo webservice
def ssl_version
gateway.ssl_version_download_nf
end

# XML que será enviado no body da requisição SOAP contendo as informações
# específicas de cada operação.
def xml_builder
# render_xml 'nfe_consulta_autorizacao'
end
end
end
end
91 changes: 91 additions & 0 deletions test/br_nfe/product/nfe_download_nf_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require 'test_helper'

describe BrNfe::Product::NfeDownloadNf do
subject { FactoryGirl.build(:product_nfe_download_nf) }
let(:gateway) { FactoryGirl.build(:product_gateway_web_service_svrs) }

before do
subject.stubs(:gateway).returns(gateway)
end

describe '#aliases' do
it { must_have_alias_attribute :chNFe, :chave_nfe }
end

describe 'Validations' do
describe '#chave_nfe' do
before { subject.stubs(:generate_key) }
it { must validate_presence_of(:chave_nfe) }
it { must validate_length_of(:chave_nfe).is_equal_to(44) }
end
end

describe '#gateway' do
before do
subject.unstub(:gateway)
subject.ibge_code_of_issuer_uf = 42
end
it "Por padrão deve pegar o gateway conforme o estado" do
subject.gateway.must_be_kind_of BrNfe::Product::Gateway::WebServiceSVRS
subject.class.new(ibge_code_of_issuer_uf: 23).gateway.must_be_kind_of BrNfe::Product::Gateway::WebServiceCE
end
it "Deve ser possível forçar a utilizar o gateway do SVAN" do
subject.force_gateway = :SVAN
subject.gateway.must_be_kind_of BrNfe::Product::Gateway::WebServiceSVAN
end
it "Deve ser possível forçar a utilizar o gateway do AN - Vai pegar o base" do
subject.force_gateway = 'AN'
subject.gateway.class.must_equal BrNfe::Product::Gateway::Base
end
end

describe 'gateway methods' do
it 'o método #wsdl deve pegar o valor do método wsdl_download_nf do gateway ' do
gateway.expects(:wsdl_download_nf).returns('http://teste.wsdl_download_nf.com')
subject.wsdl.must_equal 'http://teste.wsdl_download_nf.com'
end

it 'o método #method_wsdl deve pegar o valor do método operation_download_nf do gateway ' do
gateway.expects(:operation_download_nf).returns(:operation_download_nf)
subject.method_wsdl.must_equal :operation_download_nf
end

it 'o método #gateway_xml_version deve pegar o valor do método version_xml_download_nf do gateway ' do
gateway.expects(:version_xml_download_nf).returns(:v3_20)
subject.gateway_xml_version.must_equal :v3_20
end

it 'o método #url_xmlns deve pegar o valor do método url_xmlns_download_nf do gateway ' do
gateway.expects(:url_xmlns_download_nf).returns('http://teste.url_xmlns_download_nf.com')
subject.url_xmlns.must_equal 'http://teste.url_xmlns_download_nf.com'
end

it 'o método #ssl_version deve pegar o valor do método ssl_version_download_nf do gateway ' do
gateway.expects(:ssl_version_download_nf).returns(:SSLv1)
subject.ssl_version.must_equal :SSLv1
end
end



# describe "Validação do XML através do XSD" do
# let(:schemas_dir) { BrNfe.root+'/lib/br_nfe/product/xml/v3_10/XSD' }
# def validate_schema
# Dir.chdir(schemas_dir) do
# schema = Nokogiri::XML::Schema(IO.read('inutNFe_v3.10.xsd'))
# document = Nokogiri::XML(subject.xml_builder)
# errors = schema.validate(document)
# errors.must_be_empty
# end
# end
# it "Deve ser válido em ambiente de produção" do
# subject.env = :production
# validate_schema
# end
# it "Deve ser válido em ambiente de homologação" do
# subject.env = :test
# validate_schema
# end
# end

end
15 changes: 15 additions & 0 deletions test/factories/product/nfe_download_nf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FactoryGirl.define do
factory :product_nfe_download_nf, class: BrNfe::Product::NfeDownloadNf do
transient do
endereco_emitente { FactoryGirl.build(:endereco, codigo_municipio: '4216008') }
end
env :test
emitente { FactoryGirl.build(:product_emitente, endereco: endereco_emitente) }
ibge_code_of_issuer_uf '42'
certificate_pkcs12_path { "#{BrNfe.root}/test/cert.pfx" }
certificate_pkcs12_password 'associacao'

chave_nfe 42000082176983000186550000000000006313331836

end
end

0 comments on commit 8e0178d

Please sign in to comment.