Module: Utils

Includes:
ActiveSupport::NumberHelper, SunatBooks::CommonUtils
Included in:
SunatBooks::Pdf::Base
Defined in:
lib/sunat_books/pdf/utils.rb

Constant Summary collapse

MONTHS =
{ 1 => "Enero", 2 => "Febrero", 3 => "marzo", 4 => "abril",
5 => "mayo", 6 => "junio", 7 => "julio", 8 => "agosto",
9 => "setiembre", 10 => "octubre", 11 => "noviembre",
12 => "diciembre" }.freeze

Instance Method Summary collapse

Methods included from SunatBooks::CommonUtils

#available_value?

Instance Method Details

#add_align(aligns, options, key) ⇒ Object



32
33
34
35
36
37
# File 'lib/sunat_books/pdf/utils.rb', line 32

def add_align(aligns, options, key)
  cell_style = options[:cell_style]
  aligns.map do |a|
    cell_style.merge!(align: a[key][0].to_sym) unless a[key].nil?
  end
end

#add_widths(column_widths, options, width) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/sunat_books/pdf/utils.rb', line 39

def add_widths(column_widths, options, width)
  if column_widths.empty?
    options[:cell_style][:width] = width
  else
    options.merge!(column_widths: column_widths)
  end
end

#field_value(ticket, field) ⇒ Object



63
64
65
66
67
# File 'lib/sunat_books/pdf/utils.rb', line 63

def field_value(ticket, field)
  value = available_value?(ticket, field)
  value = formated_number(value) if value.instance_of? BigDecimal
  value
end

#formated_number(float) ⇒ Object



16
17
18
# File 'lib/sunat_books/pdf/utils.rb', line 16

def formated_number(float)
  number_to_currency(float, unit: "")
end

#get_column_widths(widths, key) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/sunat_books/pdf/utils.rb', line 47

def get_column_widths(widths, key)
  obj = {}
  widths&.each do |w|
    obj = w[key].flatten unless w[key].nil?
  end
  obj
end

#get_date(year, month, day) ⇒ Object



20
21
22
# File 'lib/sunat_books/pdf/utils.rb', line 20

def get_date(year, month, day)
  parse_day(Date.new(year.to_i, month.to_i, day))
end

#get_period(month, year) ⇒ Object



24
25
26
# File 'lib/sunat_books/pdf/utils.rb', line 24

def get_period(month, year)
  "#{MONTHS[month.to_i].upcase} #{year}"
end

#get_row_sums(tickets, counts, total_sums) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sunat_books/pdf/utils.rb', line 88

def get_row_sums(tickets, counts, total_sums)
  # given an array of counts and tickets get sums by each count
  row_counts = get_mother_counts tickets
  count_sums = row_counts.map { |count| Books::CountSum.new(count) }

  # get totals
  tickets.each do |ticket|
    count_sums.each do |count_sum|
      count_sum.add get_value(ticket, count_sum.count)
    end
  end

  # get ordered row
  order_data_row(counts, count_sums, total_sums)
end

#order_data_row(counts, count_sums, total_sums) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/sunat_books/pdf/utils.rb', line 77

def order_data_row(counts, count_sums, total_sums)
  data = []
  counts.each_with_index do |count, i|
    sum = sum_count(count_sums, count)
    value = sum ? sum.total : 0
    total_sums[i].add value
    data << { content: formated_number(value), align: :right }
  end
  data
end

#parse_day(day) ⇒ Object



28
29
30
# File 'lib/sunat_books/pdf/utils.rb', line 28

def parse_day(day)
  day.strftime("%d-%m").to_s
end

#sum_count(count_sums, count) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/sunat_books/pdf/utils.rb', line 69

def sum_count(count_sums, count)
  sum = nil
  count_sums.each do |count_sum|
    sum = count_sum if count_sum.count == count
  end
  sum
end

#txt(txt) ⇒ Object



55
56
57
# File 'lib/sunat_books/pdf/utils.rb', line 55

def txt(txt)
  text txt, size: 8
end

#zeroObject



59
60
61
# File 'lib/sunat_books/pdf/utils.rb', line 59

def zero
  formated_number(0)
end