Class: NfeReader::Nfe

Inherits:
Object
  • Object
show all
Includes:
AttributeHelper, CreatorHelper
Defined in:
lib/nfe_reader/nfe.rb

Constant Summary

Constants included from AttributeHelper

AttributeHelper::WITHELIST

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CreatorHelper

#create_resource, #create_resources, #to_array

Methods included from AttributeHelper

#attributes, #attributes_to_hash

Constructor Details

#initialize(file) ⇒ Nfe

Returns a new instance of Nfe.



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
43
44
45
46
47
48
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nfe_reader/nfe.rb', line 12

def initialize(file)

  xml = file.is_a?(Nokogiri::XML::Document) ? file : Nokogiri::XML(file)
  xml = xml.to_hash

  if xml[:nfeProc]
    # Versao da NFe
    @version_schema = xml[:nfeProc][:versao]
    # Assinatura
    @signature = xml[:nfeProc][:NFe][:Signature]

    # Protocolo
    if xml[:nfeProc][:protNFe]
      protocol = xml[:nfeProc][:protNFe][:infProt]

      @enviroment = protocol[:tpAmb]
      @version_app = protocol[:verAplic]
      @key = protocol[:chNFe]
      @date = protocol[:dhRecbto]
      @protocol = protocol[:nProt]
      @digest = protocol[:digVal]
      @status = protocol[:cStat]
      @description = protocol[:xMotivo]
    end

    xml = xml[:nfeProc][:NFe][:infNFe]
  else
    xml = xml[:NFe][:infNFe]
  end

  # Numero da Nfe
  @number = xml[:Id]
  # Versao da NFe
  @version = xml[:versao]

  # Identificação da Nota Fiscal eletrônica
  @header = Header.new(xml[:ide])

  # Identificação do Emitente da Nota Fiscal eletrônica
  @provider = Provider.new(xml[:emit])

  # Identificação do Fisco Emitente da NF-e
  if xml[:avulsa]
    @fiscal = Fiscal.new(xml[:avulsa])
  end

  # Identificação do Destinatário da Nota Fiscal eletrônica
  @customer = Customer.new(xml[:dest])

  # Informacoes Adicional
  if xml[:infAdic]
    @information = Information.new(xml[:infAdic])
  end

  # Detalhamento de Produtos e Serviços da NF-e
  @products = create_resources(Product, xml[:det])

  # Totalizadores
  @total = Total.new(xml[:total])

  # Informacao de Pagamento
  if xml[:cobr]
    @billing = Billing.new(xml[:cobr])
  end

  # Transporte
  if xml[:transp]
    @transport = Transport.new(xml[:transp])
  end

  # Identificação do Local de Entrega
  if xml[:retirada]
    @removal = Removal.new(xml[:retirada])
  end

  # Identificação do Local de Retirada
  if xml[:entrega]
    @delivery = Delivery.new(xml[:entrega])
  end

  # Autorização para obter XML
  @authorizations = create_resources(Authorization, xml[:autXML])

  # Informacoes de Comercio Exterior
  if xml[:exporta]
    @export = Export.new(xml[:exporta])
  end

  # Informacoes de Compra
  if xml[:compra]
    @purchase = Purchase.new(xml[:compra])
  end

  # Cana de Acucar
  if xml[:cana]
    @cane = Cane.new(xml[:cana])
  end

rescue => exception
  @error = exception
  @trace = exception.backtrace
ensure
  file.close if file.respond_to? :close
end

Instance Attribute Details

#authorizationsObject (readonly)

Returns the value of attribute authorizations.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def authorizations
  @authorizations
end

#billingObject (readonly)

Returns the value of attribute billing.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def billing
  @billing
end

#caneObject (readonly)

Returns the value of attribute cane.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def cane
  @cane
end

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def client
  @client
end

#customerObject (readonly)

Returns the value of attribute customer.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def customer
  @customer
end

#dateObject (readonly)

Returns the value of attribute date.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def date
  @date
end

#deliveryObject (readonly)

Returns the value of attribute delivery.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def delivery
  @delivery
end

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def description
  @description
end

#digestObject (readonly)

Returns the value of attribute digest.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def digest
  @digest
end

#enviromentObject (readonly)

Returns the value of attribute enviroment.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def enviroment
  @enviroment
end

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def error
  @error
end

#exportObject (readonly)

Returns the value of attribute export.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def export
  @export
end

#fiscalObject (readonly)

Returns the value of attribute fiscal.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def fiscal
  @fiscal
end

#headerObject (readonly)

Returns the value of attribute header.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def header
  @header
end

#informationObject (readonly)

Returns the value of attribute information.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def information
  @information
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def key
  @key
end

#numberObject (readonly)

Returns the value of attribute number.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def number
  @number
end

#productsObject (readonly)

Returns the value of attribute products.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def products
  @products
end

#protocolObject (readonly)

Returns the value of attribute protocol.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def protocol
  @protocol
end

#providerObject (readonly)

Returns the value of attribute provider.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def provider
  @provider
end

#purchaseObject (readonly)

Returns the value of attribute purchase.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def purchase
  @purchase
end

#removalObject (readonly)

Returns the value of attribute removal.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def removal
  @removal
end

#signatureObject (readonly)

Returns the value of attribute signature.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def signature
  @signature
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def status
  @status
end

#totalObject (readonly)

Returns the value of attribute total.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def total
  @total
end

#traceObject (readonly)

Returns the value of attribute trace.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def trace
  @trace
end

#transportObject (readonly)

Returns the value of attribute transport.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def transport
  @transport
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def version
  @version
end

#version_appObject (readonly)

Returns the value of attribute version_app.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def version_app
  @version_app
end

#version_schemaObject (readonly)

Returns the value of attribute version_schema.



6
7
8
# File 'lib/nfe_reader/nfe.rb', line 6

def version_schema
  @version_schema
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/nfe_reader/nfe.rb', line 117

def error?
  !error.nil?
end