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.



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

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



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

def chart_of_accounts
  @chart_of_accounts
end

#journalsObject

Returns the value of attribute journals

Returns:

  • (Object)

    the current value of journals



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

def journals
  @journals
end

#run_optionsObject

Returns the value of attribute run_options

Returns:

  • (Object)

    the current value of run_options



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

def run_options
  @run_options
end

Instance Method Details

#all_acct_amountsObject

Note: Unfiltered!



134
135
136
# File 'lib/rock_books/documents/book_set.rb', line 134

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

#all_entriesObject



139
140
141
# File 'lib/rock_books/documents/book_set.rb', line 139

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

#all_reports(filter = nil) ⇒ Object



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

def all_reports(filter = nil)

  periods_to_underscores = ->(string) { string.gsub('.', '_') }

  context = report_context
  report_hash = context.journals.each_with_object({}) do |journal, report_hash|
    key = periods_to_underscores.(journal.short_name).to_sym
    report_hash[key] = 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_' + periods_to_underscores.(.code)).to_sym
    report = TxOneAccount.new(context, .code).call
    report_hash[key] = report
  end
  report_hash
end

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rock_books/documents/book_set.rb', line 79

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

  create_directories = -> do
    %w(txt pdf html).each do |format|
      dir = File.join(directory, format, SINGLE_ACCT_SUBDIR)
      FileUtils.mkdir_p(dir)
    end
  end

  # "./pdf/short_name.pdf" or "./pdf/single_account/short_name.pdf"
  build_filespec = ->(directory, 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

  create_index_html = -> do
    filespec = build_filespec.(directory, 'index', 'html')
    File.write(filespec, index_html_content)
    puts "Created index.html"
  end

  write_reports = ->do

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

      File.write(txt_filespec, report_text)
      # Use smaller size for the PDF but larger size for the web pages:
      run_command("textutil -convert html -font 'Courier New Bold' -fontsize 11 #{txt_filespec} -output #{html_filespec}")
      run_command("cupsfilter #{html_filespec} > #{pdf_filespec}")
      run_command("textutil -convert html -font 'Courier New Bold' -fontsize 14 #{txt_filespec} -output #{html_filespec}")
      puts "Created reports in txt, html, and pdf for #{"%-20s" % short_name} at #{File.dirname(txt_filespec)}.\n\n\n"
    end
  end

  create_directories.()
  create_index_html.()
  write_reports.()
end

#index_html_contentObject



161
162
163
164
165
166
167
168
# File 'lib/rock_books/documents/book_set.rb', line 161

def index_html_content
  erb_filespec = File.join(File.dirname(__FILE__), 'index.html.erb')
  erb = ERB.new(File.read(erb_filespec))
  erb.result_with_hash(
      journals: journals,
      chart_of_accounts: chart_of_accounts,
      run_options: run_options)
end

#journal_namesObject Also known as: jnames



127
128
129
# File 'lib/rock_books/documents/book_set.rb', line 127

def journal_names
  journals.map(&:short_name)
end

#missing_and_existing_receiptsObject



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rock_books/documents/book_set.rb', line 149

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



144
145
146
# File 'lib/rock_books/documents/book_set.rb', line 144

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

#report_contextObject



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

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

#run_command(command) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rock_books/documents/book_set.rb', line 64

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