module DiaryEntries

Public Instance Methods

buys_entry(tickets, counts, total_sums, data, period_date) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 17
def buys_entry(tickets, counts, total_sums, data, period_date)
  buys = tickets.where(operation_type: "compras")
  title = "COMPRAS DEL PERIODO"
  return unless buys.count.positive?

  buys_sum = get_row_sums(buys, counts, total_sums)
  data << [period_date, title, buys_sum].flatten
end
calculate_totals(tickets, count_sums) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 75
def calculate_totals(tickets, count_sums)
  # get totals
  tickets.each do |ticket|
    count_sums.each do |count_sum|
      count_sum.add get_value(ticket, count_sum.count)
    end
  end
end
close_entry(tickets, counts, total_sums, data) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 49
def close_entry(tickets, counts, total_sums, data)
  close = tickets.where(operation_type: "cierre")
  close.each do |ticket|
    ticket_data = []
    counts.each_with_index do |count, i|
      value = mother_count?(count, ticket) ? get_value(ticket, count) : 0
      increase_value(ticket_data, total_sums, i, value)
    end
    ref = ticket.reference
    data << [parse_day(ticket.operation_date), ref, ticket_data].flatten
  end
end
current_sum_count(count_sums, count) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 84
def current_sum_count(count_sums, count)
  sum_count = nil
  count_sums.each do |count_sum|
    sum_count = count_sum if count_sum.count == count
  end
  sum_count
end
get_row_sums(tickets, counts, total_sums) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 92
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| SunatBooks::Pdf::CountSum.new(count) }

  calculate_totals(tickets, count_sums)
  # get ordered row
  row_data = []
  counts.each_with_index do |count, i|
    sum_count = current_sum_count(count_sums, count)
    value = sum_count ? sum_count.total : 0
    increase_value(row_data, total_sums, i, value)
  end
  row_data
end
increase_value(row_data, total_sums, index, value) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 108
def increase_value(row_data, total_sums, index, value)
  total_sums[index].add value
  row_data << { content: formated_number(value), align: :right }
end
initial_entry(tickets, counts, total_sums) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 4
def initial_entry(tickets, counts, total_sums)
  initial = tickets.where(operation_type: "inicial")
  if !initial.empty?
    initial_data = get_row_sums(initial, counts, total_sums)
  else
    initial_data = []
    total_sums.map do |sum|
      initial_data << { content: formated_number(sum.total), align: :right }
    end
  end
  initial_data
end
mother_count?(count, ticket) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 71
def mother_count?(count, ticket)
  ticket.uniq_mother_counts.include?(count)
end
other_entry(tickets, counts, total_sums, data) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 35
def other_entry(tickets, counts, total_sums, data)
  others = tickets.where(operation_type: "otros")
  # others_row = get_row_sums(others, counts, total_sums)
  others&.each do |ticket|
    ticket_data = []
    counts.each_with_index do |count, i|
      value = mother_count?(count, ticket) ? get_value(ticket, count) : 0
      increase_value(ticket_data, total_sums, i, value)
    end
    ref = ticket.reference
    data << [parse_day(ticket.operation_date), ref, ticket_data].flatten
  end
end
sales_entry(tickets, counts, total_sums, data, period_date) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 26
def sales_entry(tickets, counts, total_sums, data, period_date)
  sales = tickets.where(operation_type: "ventas")
  title = "VENTAS DEL PERIODO"
  return unless sales.count.positive?

  sales_sum = get_row_sums(sales, counts, total_sums)
  data << [period_date, title, sales_sum].flatten
end
total_entry(total_sums, data) click to toggle source
# File lib/sunat_books/pdf/diary_entries.rb, line 62
def total_entry(total_sums, data)
  # totals
  total_data = []
  total_sums.map do |sum|
    total_data << { content: formated_number(sum.total), align: :right }
  end
  data << [{ content: "TOTALES", colspan: 2 }, total_data].flatten
end