Class: BrDanfe::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/br_danfe/helper.rb

Class Method Summary collapse

Class Method Details

.format_cep(cep) ⇒ Object



37
38
39
# File 'lib/br_danfe/helper.rb', line 37

def self.format_cep(cep)
  cep.sub(/(\d{2})(\d{3})(\d{3})/, '\\1.\\2-\\3')
end

.format_datetime(xml_datetime, with_time_zone: false) ⇒ Object



26
27
28
29
# File 'lib/br_danfe/helper.rb', line 26

def self.format_datetime(xml_datetime, with_time_zone: false)
  formated = with_time_zone ? '%d/%m/%Y %H:%M:%S%:z' : '%d/%m/%Y %H:%M:%S'
  xml_datetime.present? ? Time.parse(xml_datetime).strftime(formated) : ''
end

.homologation?(xml) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/br_danfe/helper.rb', line 7

def self.homologation?(xml)
  xml.css('nfeProc/NFe/infNFe/ide/tpAmb').text == '2'
end

.nfe?(xml) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/br_danfe/helper.rb', line 31

def self.nfe?(xml)
  nfe_code = '55'

  xml['ide > mod'] == nfe_code
end

.no_fiscal_value?(xml) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/br_danfe/helper.rb', line 3

def self.no_fiscal_value?(xml)
  homologation?(xml) || unauthorized?(xml)
end

.numerify(number) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/br_danfe/helper.rb', line 15

def self.numerify(number)
  return '' if !number || number == ''

  separated_number = number.to_s.split('.')
  integer_part = separated_number[0].reverse.gsub(/\d{3}(?=\d)/, '\&.').reverse
  decimal_part = separated_number[1] || '00'
  decimal_part += '0' if decimal_part.size < 2

  "#{integer_part},#{decimal_part}"
end

.unauthorized?(xml) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/br_danfe/helper.rb', line 11

def self.unauthorized?(xml)
  xml.css('nfeProc/protNFe/infProt/dhRecbto').empty?
end