Class: Nfse::Pdf::Helper

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

Class Method Summary collapse

Class Method Details

.extract_time_from_date(string) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/danfe/helper.rb', line 69

def self.extract_time_from_date(string)
  formated_time = ""

  if not string.empty?
    date = extract_date_time(string)
    formated_time = date.strftime("%H:%M:%S")
  end

  formated_time
end

.format_cnpj(string) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/danfe/helper.rb', line 80

def self.format_cnpj(string)
  formated_cnpj = ""

  if not (string.empty? || string.size != 14)
    formated_cnpj = string[0,2] + '.' + string[2,3] + '.' + string[5,3] +
                    '/' + string[8,4] + '-' + string[12,2]
  end

  formated_cnpj
end

.format_date(string) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/danfe/helper.rb', line 47

def self.format_date(string)
  formated_date = ""

  if not string.empty?
    date = Date.strptime(string, "%Y-%m-%d")
    formated_date = date.strftime("%d/%m/%Y")
  end

  formated_date
end

.format_datetime(string) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/danfe/helper.rb', line 36

def self.format_datetime(string)
  formated_datetime = ""

  if not string.empty?
    date = extract_date_time(string)
    formated_datetime = date.strftime("%d/%m/%Y %H:%M:%S")
  end

  formated_datetime
end

.format_quantity(qty) ⇒ Object



29
30
31
32
33
34
# File 'lib/danfe/helper.rb', line 29

def self.format_quantity(qty)
  return Helper.numerify(qty, RubyDanfe.options.quantity_decimals) if RubyDanfe.options.numerify_prod_qcom
  qty.gsub!(",", ".")
  qty[qty.rindex('.')] = ',' if qty.rindex('.')
  qty
end

.format_time(string) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/danfe/helper.rb', line 58

def self.format_time(string)
  formated_time = ""

  if not string.empty?
    time = Time.strptime(string, "%H:%M:%S")
    formated_time = time.strftime("%H:%M:%S")
  end

  formated_time
end

.invert(y) ⇒ Object



25
26
27
# File 'lib/danfe/helper.rb', line 25

def self.invert(y)
  28.7.cm - y
end

.numerify(number, decimals = 2) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/danfe/helper.rb', line 5

def self.numerify(number, decimals = 2)
  number = number.tr("\n","").delete(" ")
  return "" if !number || number == ""
  int, frac = ("%.#{decimals}f" % number).split(".")
  int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1\.")
  int + "," + frac
  rescue
    number
end

.numerify_default_zero(number, decimals = 2) ⇒ Object



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

def self.numerify_default_zero(number, decimals = 2)
  number = number.tr("\n","").delete(" ")
  return "0,00" if !number || number == ""
  int, frac = ("%.#{decimals}f" % number).split(".")
  int.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1\.")
  int + "," + frac
  rescue
    number
end