Class: Teri::ReportGenerator
- Inherits:
-
Object
- Object
- Teri::ReportGenerator
- Defined in:
- lib/teri/report_generator.rb
Overview
Handles report generation for balance sheets and income statements
Instance Method Summary collapse
-
#balance_sheet_unbalanced?(output) ⇒ Boolean
Helper method to check if a balance sheet is unbalanced.
- #generate_balance_sheet(options = {}) ⇒ Object
- #generate_income_statement(options = {}) ⇒ Object
-
#initialize(options, logger) ⇒ ReportGenerator
constructor
A new instance of ReportGenerator.
Constructor Details
#initialize(options, logger) ⇒ ReportGenerator
Returns a new instance of ReportGenerator.
7 8 9 10 |
# File 'lib/teri/report_generator.rb', line 7 def initialize(, logger) @options = @logger = logger end |
Instance Method Details
#balance_sheet_unbalanced?(output) ⇒ Boolean
Helper method to check if a balance sheet is unbalanced
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/teri/report_generator.rb', line 135 def balance_sheet_unbalanced?(output) # With --no-total option, we need to calculate the total ourselves assets = 0.0 liabilities = 0.0 equity = 0.0 # Extract amounts for each section output.each_line do |line| case line when /^\s*([\-\$\d,\.]+)\s+USD\s+Assets/ assets_str = ::Regexp.last_match(1).gsub(/[\$,]/, '') assets = assets_str.to_f when /^\s*([\-\$\d,\.]+)\s+USD\s+Liabilities/ liabilities_str = ::Regexp.last_match(1).gsub(/[\$,]/, '') liabilities = liabilities_str.to_f when /^\s*([\-\$\d,\.]+)\s+USD\s+Equity/ equity_str = ::Regexp.last_match(1).gsub(/[\$,]/, '') equity = equity_str.to_f end end # Calculate the balance (Assets = Liabilities + Equity) balance = assets - (liabilities + equity) # Consider it balanced if the difference is less than 1 cent balance.abs > 0.01 end |
#generate_balance_sheet(options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 62 63 64 65 66 67 68 69 70 71 72 73 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/teri/report_generator.rb', line 12 def generate_balance_sheet( = {}) # Merge provided options with default options opts = @options.merge() # Get the specified year from options, defaulting to current year specified_year = opts[:year] || Date.today.year # Get the number of periods from options, defaulting to 2 if not specified periods = opts[:periods] || 2 puts "Balance Sheet for #{specified_year}" if opts[:month] puts "Month: #{opts[:month]}" else puts "Including previous #{periods} years" end puts '=' * 50 puts '' # Check if coding.ledger exists unless File.exist?('coding.ledger') puts "Warning: coding.ledger file does not exist. Please run 'teri code' first to process and code your transactions." puts 'Without coding, the balance sheet cannot be generated correctly.' return end # Common ledger options for consistent formatting = '--exchange USD --no-total --collapse' # If a specific month is provided, show just that month if opts[:month] month = opts[:month].to_i end_date = Date.new(specified_year, month, -1) # Last day of the month puts "Balance Sheet as of #{end_date.strftime('%Y-%m-%d')}" puts '-' * 50 cmd = 'ledger -f coding.ledger' Dir.glob('transactions/*.ledger').each do |file| cmd += " -f #{file}" end cmd += " balance #{} --end #{end_date.strftime('%Y/%m/%d')} ^Assets ^Liabilities ^Equity" output = `#{cmd}` if $?.success? puts output # Check if the balance sheet is balanced if balance_sheet_unbalanced?(output) puts "Note: The balance sheet is not balanced. You may want to run 'teri fix-balance' to create adjustment entries." end else puts "Error generating balance sheet (exit code: #{$?.exitstatus})" puts "Command was: #{cmd}" end return end # Generate balance sheet for the specified year and previous periods start_year = specified_year - periods end_year = specified_year - 1 # Show previous years (start_year..end_year).each do |year| puts "Balance Sheet as of #{year}-12-31" puts '-' * 50 # Use the --file option to specify files instead of a glob pattern cmd = 'ledger -f coding.ledger' Dir.glob('transactions/*.ledger').each do |file| cmd += " -f #{file}" end cmd += " balance #{} --end #{year}/12/31 ^Assets ^Liabilities ^Equity" output = `#{cmd}` if $?.success? puts output # Check if the balance sheet is balanced if balance_sheet_unbalanced?(output) puts "Note: The balance sheet is not balanced. You may want to run 'teri fix-balance' to create adjustment entries." end else puts "Error generating balance sheet (exit code: #{$?.exitstatus})" puts "Command was: #{cmd}" end end # Show current balance sheet for specified year current_date = Date.today if specified_year == current_date.year # If specified year is current year, show as of today puts "Balance Sheet as of #{current_date.strftime('%Y-%m-%d')}" end_date = current_date else # If specified year is not current year, show as of year end puts "Balance Sheet as of #{specified_year}-12-31" end_date = Date.new(specified_year, 12, 31) end puts '-' * 50 cmd = 'ledger -f coding.ledger' Dir.glob('transactions/*.ledger').each do |file| cmd += " -f #{file}" end cmd += " balance #{} --end #{end_date.strftime('%Y/%m/%d')} ^Assets ^Liabilities ^Equity" output = `#{cmd}` if $?.success? puts output # Check if the balance sheet is balanced if balance_sheet_unbalanced?(output) puts "Note: The balance sheet is not balanced. You may want to run 'teri fix-balance' to create adjustment entries." end else puts "Error generating balance sheet (exit code: #{$?.exitstatus})" puts "Command was: #{cmd}" end end |
#generate_income_statement(options = {}) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/teri/report_generator.rb', line 163 def generate_income_statement( = {}) # Merge provided options with default options opts = @options.merge() # Get the specified year from options, defaulting to current year specified_year = opts[:year] || Date.today.year # Get the number of periods from options, defaulting to 2 if not specified periods = opts[:periods] || 2 puts "Income Statement for #{specified_year}" if opts[:month] puts "Month: #{opts[:month]}" else puts "Including previous #{periods} years" end puts '=' * 50 puts '' # Check if coding.ledger exists unless File.exist?('coding.ledger') puts "Warning: coding.ledger file does not exist. Please run 'teri code' first to process and code your transactions." puts 'Without coding, the income statement cannot be generated correctly.' return end # Common ledger options for consistent formatting = '--exchange USD --no-total --collapse' # If a specific month is provided, show just that month if opts[:month] month = opts[:month].to_i start_date = Date.new(specified_year, month, 1) end_date = Date.new(specified_year, month, -1) # Last day of the month puts "Income Statement for #{start_date.strftime('%B %Y')}" puts '-' * 50 cmd = 'ledger -f coding.ledger' Dir.glob('transactions/*.ledger').each do |file| cmd += " -f #{file}" end cmd += " balance #{} --begin #{start_date.strftime('%Y/%m/%d')} --end #{end_date.strftime('%Y/%m/%d')} ^Income ^Expenses" else # Generate income statement for the specified year and previous periods start_year = specified_year - periods end_year = specified_year - 1 # Show previous years (start_year..end_year).each do |year| puts "Income Statement for #{year}" puts '-' * 50 cmd = 'ledger -f coding.ledger' Dir.glob('transactions/*.ledger').each do |file| cmd += " -f #{file}" end cmd += " balance #{} --begin #{year}/01/01 --end #{year}/12/31 ^Income ^Expenses" output = `#{cmd}` if $?.success? puts output else puts "Error generating income statement (exit code: #{$?.exitstatus})" puts "Command was: #{cmd}" end end # Show current year income statement current_date = Date.today if specified_year == current_date.year # If specified year is current year, show as of today puts "Income Statement as of #{current_date.strftime('%Y-%m-%d')}" else # If specified year is not current year, show as of year end puts "Income Statement as of #{specified_year}-12-31" end cmd = 'ledger -f coding.ledger' Dir.glob('transactions/*.ledger').each do |file| cmd += " -f #{file}" end cmd += " balance #{} --begin #{specified_year}/01/01 --end #{specified_year}/12/31 ^Income ^Expenses" end output = `#{cmd}` if $?.success? puts output else puts "Error generating income statement (exit code: #{$?.exitstatus})" puts "Command was: #{cmd}" end end |