Class: RockBooks::BookSet

Inherits:
Struct
  • Object
show all
Defined in:
lib/rock_books/documents/book_set.rb

Constant Summary collapse

FILTERS =
JournalEntryFilters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run_options, chart_of_accounts, journals) ⇒ BookSet

Returns a new instance of BookSet.



25
26
27
# File 'lib/rock_books/documents/book_set.rb', line 25

def initialize(run_options, chart_of_accounts, journals)
  super
end

Instance Attribute Details

#chart_of_accountsObject

Returns the value of attribute chart_of_accounts

Returns:

  • (Object)

    the current value of chart_of_accounts



20
21
22
# File 'lib/rock_books/documents/book_set.rb', line 20

def chart_of_accounts
  @chart_of_accounts
end

#journalsObject

Returns the value of attribute journals

Returns:

  • (Object)

    the current value of journals



20
21
22
# File 'lib/rock_books/documents/book_set.rb', line 20

def journals
  @journals
end

#run_optionsObject

Returns the value of attribute run_options

Returns:

  • (Object)

    the current value of run_options



20
21
22
# File 'lib/rock_books/documents/book_set.rb', line 20

def run_options
  @run_options
end

Instance Method Details

#all_acct_amountsObject

Note: Unfiltered!



111
112
113
# File 'lib/rock_books/documents/book_set.rb', line 111

def all_acct_amounts
  @all_acct_amounts ||= Journal.acct_amounts_in_documents(journals)
end

#all_entriesObject



116
117
118
# File 'lib/rock_books/documents/book_set.rb', line 116

def all_entries
  @all_entries ||= Journal.entries_in_documents(journals)
end

#all_reports(filter = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rock_books/documents/book_set.rb', line 35

def all_reports(filter = nil)
  context = report_context
  report_hash = context.journals.each_with_object({}) do |journal, report_hash|
    report_hash[journal.short_name] = TransactionReport.new(journal, context).call(filter)
  end
  report_hash['all_txns_by_date'] = MultidocTransactionReport.new(context).call(filter)
  report_hash['all_txns_by_amount'] = MultidocTransactionReport.new(context).call(filter, :amount)
  report_hash['all_txns_by_acct'] = TxByAccount.new(context).call
  report_hash['balance_sheet'] = BalanceSheet.new(context).call
  report_hash['income_statement'] = IncomeStatement.new(context).call

  if run_options.do_receipts
    report_hash['receipts'] = ReceiptsReport.new(context, *missing_and_existing_receipts).call
  end

  chart_of_accounts.accounts.each do ||
    key = 'acct_' + .code
    report = TxOneAccount.new(context, .code).call
    report_hash[key] = report
  end
  report_hash
end

#all_reports_to_files(directory = '.', filter = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rock_books/documents/book_set.rb', line 74

def all_reports_to_files(directory = '.', filter = nil)
  reports = all_reports(filter)

  # "./pdf/short_name.pdf" or "./pdf/single_account/short_name.pdf"
  build_filespec = ->(short_name, file_format) do
    fragments = [directory, file_format, "#{short_name}.#{file_format}"]
    is_acct_report =  /^acct_/.match(short_name)
    if is_acct_report
      fragments.insert(2, SINGLE_ACCT_SUBDIR)
    end
    File.join(*fragments)
  end

  reports.each do |short_name, report_text|
    txt_filespec  = build_filespec.(short_name, 'txt')
    html_filespec = build_filespec.(short_name, 'html')
    pdf_filespec  = build_filespec.(short_name, 'pdf')

    FileUtils.mkdir_p(File.dirname(txt_filespec))
    FileUtils.mkdir_p(File.dirname(html_filespec))
    FileUtils.mkdir_p(File.dirname(pdf_filespec))

    File.write(txt_filespec, report_text)
    run_command("textutil -convert html -font 'Menlo Regular' -fontsize 10 #{txt_filespec} -output #{html_filespec}")
    run_command("cupsfilter #{html_filespec} > #{pdf_filespec}")
    puts "Created reports in txt, html, and pdf for #{"%-20s" % short_name} at #{File.dirname(txt_filespec)}.\n\n\n"
  end
end

#journal_namesObject Also known as: jnames



104
105
106
# File 'lib/rock_books/documents/book_set.rb', line 104

def journal_names
  journals.map(&:short_name)
end

#missing_and_existing_receiptsObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rock_books/documents/book_set.rb', line 126

def missing_and_existing_receipts
  missing = []; existing = []
  all_entries.each do |entry|
    entry.receipts.each do |receipt|
      file_exists = File.file?(receipt_full_filespec(receipt))
      list = (file_exists ? existing : missing)
      list << { receipt: receipt, journal: entry.doc_short_name }
    end
  end
  [missing, existing]
end

#receipt_full_filespec(receipt_filespec) ⇒ Object



121
122
123
# File 'lib/rock_books/documents/book_set.rb', line 121

def receipt_full_filespec(receipt_filespec)
  File.join(run_options.receipt_dir, receipt_filespec)
end

#report_contextObject



30
31
32
# File 'lib/rock_books/documents/book_set.rb', line 30

def report_context
  @report_context ||= ReportContext.new(chart_of_accounts, journals, nil, nil, 80)
end

#run_command(command) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rock_books/documents/book_set.rb', line 59

def run_command(command)
  puts "\n----\nRunning command: #{command}"
  stdout, stderr, status = Open3.capture3(command)
  puts "Status was #{status}."
  unless stdout.size == 0
    puts "\nStdout was:\n\n#{stdout}"
  end
  unless stderr.size == 0
    puts "\nStderr was:\n\n#{stderr}"
  end
  puts
  stdout
end