Class: RockBooks::MultidocTransactionReport

Inherits:
Object
  • Object
show all
Includes:
Reporter
Defined in:
lib/rock_books/reports/multidoc_transaction_report.rb

Constant Summary collapse

SORT_BY_VALID_OPTIONS =
%i(date_and_account  amount)

Constants included from Reporter

Reporter::SHORT_NAME_FORMAT_STRING, Reporter::SHORT_NAME_MAX_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Reporter

account_code_name_type_string, banner_line, center, format_account_code, format_acct_amount, format_amount, format_multidoc_entry, generate_account_type_section, generate_and_format_totals, max_account_code_length, page_width

Constructor Details

#initialize(report_context) ⇒ MultidocTransactionReport

Returns a new instance of MultidocTransactionReport.



15
16
17
# File 'lib/rock_books/reports/multidoc_transaction_report.rb', line 15

def initialize(report_context)
  @context = report_context
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



11
12
13
# File 'lib/rock_books/reports/multidoc_transaction_report.rb', line 11

def context
  @context
end

Instance Method Details

#generate_header(sort_by) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rock_books/reports/multidoc_transaction_report.rb', line 20

def generate_header(sort_by)
  lines = [banner_line]
  lines << center(context.entity || 'Unspecified Entity')
  lines << center('Multi Document Transaction Report')
  lines << center('Sorted by Amount Descending') if sort_by == :amount
  lines << ''
  lines << center('Source Documents:')
  lines << ''
  context.journals.each do |document|
    short_name = SHORT_NAME_FORMAT_STRING % document.short_name
    lines << center("#{short_name} -- #{document.title}")
  end
  lines << banner_line
  lines << ''
  lines << '   Date     Document           Amount      Account'
  lines << '   ----     --------           ------      -------'
  lines.join("\n") << "\n\n"
end

#generate_report(filter = nil, sort_by = :date_and_account) ⇒ Object Also known as: to_s, call



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rock_books/reports/multidoc_transaction_report.rb', line 40

def generate_report(filter = nil, sort_by = :date_and_account)
  unless SORT_BY_VALID_OPTIONS.include?(sort_by)
    raise Error.new("sort_by option '#{sort_by}' not in valid choices of #{SORT_BY_VALID_OPTIONS}.")
  end

  entries = Journal.entries_in_documents(context.journals, filter)

  if sort_by == :amount
    JournalEntry.sort_entries_by_amount_descending!(entries)
  end

  sio = StringIO.new
  sio << generate_header(sort_by)
  entries.each { |entry| sio << format_multidoc_entry(entry) << "\n" }

  totals = AcctAmount.(JournalEntry.entries_acct_amounts(entries))
  sio << generate_and_format_totals('Totals', totals)

  sio << "\n"
  sio.string
end