Class: Treasurer::Reporter

Inherits:
Object
  • Object
show all
Includes:
Analysis
Defined in:
lib/treasurer/report.rb,
lib/treasurer/accounts.rb,
lib/treasurer/analysis.rb

Defined Under Namespace

Modules: Analysis Classes: Account, Equity, ReportAccount, SubAccount

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Analysis

#account_expenditure, #accounts_with_averages, #accounts_with_projections, #get_projected_accounts, #sum_future, #sum_regular

Constructor Details

#initialize(runner, options) ⇒ Reporter

Returns a new instance of Reporter.



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
# File 'lib/treasurer/report.rb', line 84

def initialize(runner, options)
  @runner = runner
  
  @today = options[:today]||Date.today
  @days_ahead = case da = options[:days_ahead]
                  when NilClass, ""
                    180
                  when Integer
                    da
                  when String
                    da =~ /^\d+$/ ? da.to_i : Date.parse(da) - @today
                  else
                    raise ArgumentError.new("days_ahead")
                  end
  @days_before = case da = options[:days_before]
                  when NilClass, ""
                    360
                  when Integer
                    da
                  when String
                    da =~ /^\d+$/ ? da.to_i : @today - Date.parse(da) 
                  else
                    raise ArgumentError.new("days_before")
                  end
  puts @days_before, options[:days_before]
  sleep 2

  @start_date = @today - @days_before
  @end_date = @today + @days_ahead
  @runs = runner.component_run_list.values
  @currencies = ACCOUNT_INFO.map{|k,v| v[:currencies]}.flatten.uniq
  @report_currency = options[:report_currency] 

  if run = @runs.find{|r| not r.}
    raise "External_account not specified for #{run.data_line}"
  end
  @indateruns = @runs.find_all{|r| r.days_ago(@today) < @days_before}
  @stable_discretionary_account_factors = {}
  @in_limit_discretionary_account_factors = {}
  @runner = runner
  #p 'accounts256',@runs.size, @runs.map{|r| r.account}.uniq

end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



77
78
79
# File 'lib/treasurer/report.rb', line 77

def accounts
  @accounts
end

#accounts_hashObject (readonly)

Returns the value of attribute accounts_hash.



82
83
84
# File 'lib/treasurer/report.rb', line 82

def accounts_hash
  @accounts_hash
end

#days_beforeObject (readonly)

Returns the value of attribute days_before.



80
81
82
# File 'lib/treasurer/report.rb', line 80

def days_before
  @days_before
end

#end_dateObject (readonly)

include LocalCustomisations



73
74
75
# File 'lib/treasurer/report.rb', line 73

def end_date
  @end_date
end

#equitiesObject (readonly)

Returns the value of attribute equities.



78
79
80
# File 'lib/treasurer/report.rb', line 78

def equities
  @equities
end

#equityObject (readonly)

Returns the value of attribute equity.



78
79
80
# File 'lib/treasurer/report.rb', line 78

def equity
  @equity
end

#in_limit_discretionary_account_factorsObject (readonly)

Returns the value of attribute in_limit_discretionary_account_factors.



74
75
76
# File 'lib/treasurer/report.rb', line 74

def 
  @in_limit_discretionary_account_factors
end

#projected_account_factorObject

Returns the value of attribute projected_account_factor.



76
77
78
# File 'lib/treasurer/report.rb', line 76

def 
  @projected_account_factor
end

#projected_accounts_infoObject (readonly)

Returns the value of attribute projected_accounts_info.



79
80
81
# File 'lib/treasurer/report.rb', line 79

def projected_accounts_info
  @projected_accounts_info
end

#report_currencyObject (readonly)

Returns the value of attribute report_currency.



81
82
83
# File 'lib/treasurer/report.rb', line 81

def report_currency
  @report_currency
end

#runnerObject (readonly)

Returns the value of attribute runner.



83
84
85
# File 'lib/treasurer/report.rb', line 83

def runner
  @runner
end

#stable_discretionary_account_factorsObject (readonly)

Returns the value of attribute stable_discretionary_account_factors.



75
76
77
# File 'lib/treasurer/report.rb', line 75

def 
  @stable_discretionary_account_factors
end

#start_dateObject (readonly)

include LocalCustomisations



73
74
75
# File 'lib/treasurer/report.rb', line 73

def start_date
  @start_date
end

#todayObject (readonly)

include LocalCustomisations



73
74
75
# File 'lib/treasurer/report.rb', line 73

def today
  @today
end

Instance Method Details

#account_and_transfer_graphs(accounts, options = {}) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/treasurer/report.rb', line 529

def (accounts, options={})
  "#{accounts.map{||
     = .info
    #ep ['accountbadf', account, account_info]
    start_dates, dates, expenditures, _items = ()
    ep ['accountbadf', .name_c, , expenditures]
    if dates.size == 0
      ""
    else
      #ep ['account', account, dates, expenditures]
      plotdates = dates.zip(start_dates).map{|d, s| (d.to_time.to_i + s.to_time.to_i)/2.0}
      kit = GraphKit.quick_create([plotdates, expenditures])
      kit.data.each{|dk| dk.gp.with="boxes"}
      kit.gp.style = "fill solid"
      kit.xlabel = nil
      kit.ylabel = "Expenditure"
      kit.data[0].gp.with = 'boxes'
      dat = kit.data[0].x.data
      barsize = (dat.max.to_f  - dat.min.to_f)/dat.size * 0.8
      kit.gp.boxwidth = "#{barsize} absolute"
      dat.map!{|d| d - barsize*1.1}
      kit.gp.yrange = "[#{[kit.data[0].y.data.min,0].min}:]"
      #kit.gp.xrange = "[-1:#{labels.size+1}]"
      #kit.gp.xrange = "[-1:1]" if labels.size==1
      kit.gp.grid = "ytics"
      kit.xlabel = nil
      kit.ylabel = nil
      unless options[:transfers]
        kits = accounts_with_averages({ => }).map{|acco, acco_info|
          #ep 'Budget is ', account
          kit2 = GraphKit.quick_create([
            [dates[0], dates[-1]].map{|d| d.to_time.to_i - barsize},
            [acco.average, acco.average]
          ])
          kit2.data[0].gp.with = 'l lw 5'
          kit2
        }
        #$debug_gnuplot = true
        #kits.sum.gnuplot
        kit += kits.sum

      else
        kit.data[0].y.data.map!{|expen| expen*-1.0}
      end
      kit.title = "#{.name_c} Expenditure with average (Total = #{kit.data[0].y.data.sum})"
      CodeRunner::Budget.kit_time_format_x(kit)
      kit.gp.decimalsign = 'locale "en_GB.UTF-8"'
      kit.gp.format.push %[y "%'.2f"]
      #kit.gnuplot
      #ep ['kit1122', account, kit]
      fork do
        kit.gnuplot_write("#{.name_c_file}2.eps", size: "4.0in,2.0in")
        system "ps2epsi #{.name_c_file}2.eps #{.name_c_file}.eps"
        exec "epstopdf #{.name_c_file}.eps"
      end
      #%x[ps2eps #{account}.ps]
      #"\\begin{center}\\includegraphics[width=3.0in]{#{account}.eps}\\vspace{1em}\\end{center}"
      #"\\begin{center}\\includegraphics[width=0.9\\textwidth]{#{account}.eps}\\vspace{1em}\\end{center}"
      "\\myfigure{#{.name_c_file}.pdf}"
    end
  }.join("\n\n")
  }"
end

#account_balance_graphsObject



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/treasurer/report.rb', line 261

def 
  <<EOF
\\section{Graphs of Recent Balances}
  #{[:Equity, :Asset, :Liability].map{|typ|
  "\\subsection{#{typ}}\\vspace{3em}" +
    @accounts.find_all{|acc| acc.type == typ}.map{|acc|
    acc.write_balance_graph(@today, @days_before, @days_ahead)
    acc.balance_graph_string
  }.join("\n\n")
  }.join("\n\n")
  }
EOF
end

#account_breakdownObject



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/treasurer/report.rb', line 658

def 
  <<EOF
\\section{SubAccount Breakdown}
  #{(@actual_accounts).map{|, |
  _start_dates, dates, expenditures,  = (, )
  #pp account, account_items.map{|items| items.map{|i| i.date.to_s}}
  "\\subsection{#{}}" +
    .zip(dates, expenditures).map{|items, date, expenditure|
      if items.size > 0
      "
  \\small
  \\setlength{\\parindent}{0cm}\n\n\\begin{tabulary}{0.99\\textwidth}{ #{"c " * 3 + " L " + " r " * 2 + " c " }}
  %\\hline
      #{date.to_s.latex_escape} & & & Total & #{expenditure} &  \\\\
  \\hline
  \\Tstrut
      #{items.map{|r|
      ( CodeRunner::Budget.rcp.component_results + [:] -  [:sc, :balance ]).map{|res|
        r.send(res).to_s.latex_escape
      }.join(" & ")
      }.join("\\\\\n")
      }
  \\\\
  \\hline
  \\end{tabulary}
  \\normalsize
  \\vspace{1em}\n\n"
      else
      ""
      end
  }.join("\n\n")
  }.join("\n\n")
  }
EOF
end

#account_expenditure_graphsObject



519
520
521
522
523
524
525
526
527
528
# File 'lib/treasurer/report.rb', line 519

def 
  <<EOF
\\section{Expenditure by Account Period}
  #{currency_list.map{|curr|
  (@expense_accounts.find_all{|acc|
    acc.info and acc.info[:period] and acc.currency == curr and acc.should_report?
  }.map{|acc| acc.})
  }.join("\n")}
EOF
end

#account_resolutionsObject



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/treasurer/report.rb', line 593

def 
  <<EOF
\\section{Budget Resolutions}

This section sums items from accounts drawn from an alternate account, i.e. it determines how much should be transferred from one account to another as a result of expenditure from a given account.

  #{@actual_accounts.map{|, |

  "\\subsection{#{} }
\\setlength{\\parindent}{0cm}\n\n\\begin{tabulary}{0.99\\textwidth}{r l}
  %\\hline
  Account Owed & Amount  \\\\
  \\hline
  \\Tstrut
  #{ = @indateruns.find_all{|r| r. == }
alternate_accounts = .map{|r| r.}.uniq - [[:]]
alternate_accounts.map{|acc|
  alternate_items = .find_all{|r| r. == acc}
  total = alternate_items.map{|r| r.withdrawal - r.deposit}.sum
  "#{acc} & #{total} \\\\"
}.join("\n\n")
}

  \\\\
  \\hline
  \\end{tabulary}
  \\normalsize
  \\vspace{1em}\n\n

#{	alternate_accounts.map{|acc|
alternate_items = .find_all{|r| r. == acc}
alternate_items.pieces((alternate_items.size.to_f/50.to_f).ceil).map{|piece|
  "\\footnotesize\\setlength{\\parindent}{0cm}\n\n\\begin{tabulary}{0.99\\textwidth}{ #{"c " * 4 + " L " + " r " * 3 }}
  #{}: & #{[:]} & owes & #{acc} &&&&\\\\
      \\hline

      \\Tstrut

  #{piece.map{|r|
  ([:id] + CodeRunner::Budget.rcp.component_results - [:sc]).map{|res|
    r.send(res).to_s.latex_escape
    #rcp.component_results.map{|res| r.send(res).to_s.gsub(/(.{20})/, '\1\\\\\\\\').latex_escape
  }.join(" & ")
  }.join("\\\\\n")
  }
  \\end{tabulary}\\normalsize"}.join("\n\n")
}.join("\n\n")}
"
  }.join("\n\n")
}
EOF
end

#account_summariesObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/treasurer/report.rb', line 239

def 
  #ep 'accounts', @accounts.map{|a| a.name}

  <<EOF
\\section{Summary of Accounts}
  #{[:Equity, :Asset, :Liability, :Income, :Expense].map{|type|
  accs = @accounts.find_all{|acc| acc.type == type and acc.should_report?}
  "\\subsection{#{type}}
\\begin{tabulary}{0.9\\textwidth}{ R | r | r | r}
Account & Balance & Deposited & Withdrawn \\\\
\\hline
\\Tstrut
  #{(accs.map{|acc| acc..summary_line(@today, @days_before)} +
  (type == :Asset ? ASSETS.map{|n,details| "#{n} (#{details[:currency]}) & #{details[:size].to_tex} & & "} : [])).join("\\\\\n")}
  #{type!=:Equity&&false ? "
  \\\\ \\hline
  \\Tstrut
  Totals & #{accs.map{|a| a.balance}.sum.to_tex} & #{accs.map{|a| a.deposited(@today, @days_before)}.sum.to_tex} & #{accs.map{|a| a.withdrawn(@today, @days_before)}.sum.to_tex} \\\\ " : "\\\\"}
\\end{tabulary}"
  }.join("\n\n")}
EOF
end

#assumptionsObject



645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/treasurer/report.rb', line 645

def assumptions
  <<EOF
\\section{Assumptions for Projection}
\\subsection{Regular Transfers}
\\begin{lstlisting}[language=ruby]
  #{REGULAR_TRANSFERS.pretty_inspect.latex_escape}
\\end{lstlisting}
\\subsection{One-off Transfers}
\\begin{lstlisting}[language=ruby]
  #{FUTURE_TRANSFERS.pretty_inspect.latex_escape}
\\end{lstlisting}
EOF
end

#available_balances_tableObject

A table showing the available balance for each kind of account, being the amount that can be spent today.



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/treasurer/report.rb', line 503

def available_balances_table
  <<EOF
\\section{Available Balances}
\\begin{tabulary}{0.9\\textwidth}{R | r r c}
Account & Balance & Available & Type\\\\
\\hline\\Tstrut
  #{
  [:Equity, :Asset, :Liability].map{|type|
    @accounts.find_all{|acc| acc.type == type}.map{|acc|
      [acc.name_c, acc.balance(@today).to_tex, acc.available(@today).to_tex, type.to_s].join(" & ")
    }.join("\\\\ \n")
  }.join("\\\\ \n\n")
  }
\\end{tabulary}
EOF
end

#currency_list(accounts = @accounts, &block) ⇒ Object

EOF



299
300
301
302
303
304
305
306
# File 'lib/treasurer/report.rb', line 299

def currency_list(accounts=@accounts, &block)
  p accounts.map{|acc| acc.name rescue nil}
  if block_given?
    accounts.find_all{|acc| acc.runs.find{|r| yield(r)}}.map{|acc| acc.currency}.uniq.compact
  else
    accounts.find_all{|acc| not acc.runs or acc.runs.find{|r| r.in_date(acc.info)}}.map{|acc| acc.currency}.uniq.compact
  end
end

#discretionary_account_table(currency) ⇒ Object

exit



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/treasurer/report.rb', line 465

def (currency)
  discretionary_accounts = accounts_with_averages(
    @projected_accounts_info.find_all{|acc,inf| acc.currency == currency and acc.should_report?}.map{|acc,inf| [acc.,inf]}.to_h)
  accounts_with_projections(discretionary_accounts.keys)

  <<EOF
\\section{Discretionary Budget Summary (#{currency})}
\\begin{tabulary}{0.9\\textwidth}{ R | r  r  r r  }
Budget & Average & Projection & Limit & Stable \\\\
\\hline\\Tstrut
  #{rows = 
    discretionary_accounts.map{|, info|
      [
        .name_c, 
        .average, 
        .projection,
        .projection * @in_limit_discretionary_account_factors[currency],
        .projection * @stable_discretionary_account_factors[currency]
      ]
    }
    tls = rows.transpose.slice(1..4).map{|vals| vals.sum}
    rows.map{|dat|
      "#{dat[0]} & #{dat[1].to_tex} & #{dat[2].to_tex} & #{dat[3].to_tex} & #{dat[4].to_tex}  \\\\"
    }.join("\n\n")
  }
  #{if @report_currency 
      "
\\hline\\Tstrut
Totals & #{tls.map{|v| v.to_tex}.join(" & ")}
      "
    end
    }
\\end{tabulary}
EOF
end

#expense_account_summaryObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/treasurer/report.rb', line 274

def 
  <<EOF
\\section{Expense and Income Details}
\\subsection{Expense totals for #@days_before-day Budget Period}
  #{expense_pie_charts_by_currency('accountperiod_expense', @expense_accounts){|r| r.days_ago(@today) < @days_before}}
\\subsection{Income totals for #@days_before-day Budget Period}
  #{expense_pie_charts_by_currency('accountperiod_income', @accounts.find_all{|acc| acc.type==:Income}){|r| r.days_ago(@today) < @days_before}}
\\subsection{Expense Account Breakdown}
  #{@expense_accounts.find_all{|exaccount| exaccount.should_report?}.map{|exaccount|
       = exaccount.
      "
\\subsubsection{#{.name_c}}
  #{expense_pie_chart(.name_c_file + 'breakdown', .sub_accounts, ){|r|r.days_ago(@today) < @days_before }}"
  }.join("\n\n")}
\\subsection{Income Account Breakdown}
  #{@accounts.find_all{|inaccount| inaccount.type==:Income and inaccount.should_report?}.map{|inaccount|
       = inaccount.
      "
\\subsubsection{#{.name_c}}
  #{expense_pie_chart(.name_c_file + 'breakdown', .sub_accounts, ){|r|r.days_ago(@today) < @days_before }}"
  }.join("\n\n")}
EOF

  #EOF
end

#expense_pie_chart(name, accounts, subacc = nil, &block) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/treasurer/report.rb', line 319

def expense_pie_chart(name, accounts, subacc=nil, &block)
  #expaccs = accounts.find_all{|acc| acc.type == :Expense}
  labels = accounts.map{|acc| acc.name}
  #ep ['labels22539', name, labels, exps]

  kit = if subacc
          start_dates, end_dates, _exps, _items = (subacc)
          end_dates = end_dates.reverse #Now from earliest to latest
          start_dates = start_dates.reverse
          #pp ['DATES', start_dates, end_dates, subacc.name]
          return "No expenditure in account period." if end_dates.size==0
          #keylabels = []
          k = (
            end_dates.size.times.map do |i|
              exps = []
              if subacc.type==:Expense
                exps = accounts.map{|acc| acc.deposited(end_dates[i], end_dates[i] - start_dates[i], &block)}
              else
                exps = accounts.map{|acc| acc.withdrawn(end_dates[i], end_dates[i] - start_dates[i], &block) - 
                                    acc.deposited(end_dates[i], end_dates[i] - start_dates[i], &block)
                }
              end
              kt = GraphKit.quick_create([labels.size.times.to_a.map{|l| l.to_f + i.to_f/(end_dates.size+1).to_f}, exps])
              kt.data[0].gp.title = "Ending #{end_dates[i].strftime("#{end_dates[i].mday.ordinalize} %B")}; total = #{exps.sum}"
              kt.gp.key = "rmargin"
              #keylabels.push "'Ending #{end_dates[i].strftime("#{end_dates[i].mday.ordinalize} %B")}; total = #{exps.sum}'"
              #kt.gp.key = "off rotate by 90"
              kt
            end
          ).sum
          subaccount_datagroups = k.data.map{|dk| dk.y.data}.transpose
          k.gp.label = subaccount_datagroups.size.times.map{|i|
            "'#{sprintf("%.2f", subaccount_datagroups[i].mean)}' at #{i.to_f + 0.25}, graph 0.99 right rotate by 90 front textcolor rgb 'black' boxed" 
          }
          k.gp.label.push(
            #"'Mean per budget period' at #{subaccount_datagroups.size.to_f}, graph 0.99 right rotate by 90 front textcolor rgb 'black' boxed " 
            "'Mean expenditure per budget period' at -1.0, graph 0.99 right rotate by 90 front textcolor rgb 'black' boxed " 
          )
          k.gp.xrange = "[:#{subaccount_datagroups.size + 1.0}]"
          k.gp.xrange = "[-1.5:]"
          k
        else
          exps = accounts.map{|acc| acc.type==:Expense ? acc.deposited(@today, 50000, &block) - acc.withdrawn(@today, 50000, &block): acc.withdrawn(@today, 50000, &block) - acc.deposited(@today, 50000, &block)}
          labels, exps = [labels, exps].transpose.find_all{|l, e| e != 0.0}.transpose
          return "No expenditure in account period." if not labels #<F8> labels.size==0
          k = GraphKit.quick_create([labels.size.times.to_a, exps])
          k.data[0].gp.title = "Cumulative over budget period. Total = #{exps.sum}"
          k
        end


  #sum = exps.sum
  #angles = exps.map{|ex| ex/sum * 360.0}
  #start_angles = angles.dup #inject(-angles[0]){|o,n| o+n}
  ##start_angles.map!{|a| a+(start_angles
  #end_angles = angles.inject(-angles[0]){|o,n| o+n}


  kit.data.each{|dk| dk.gp.with = 'boxes'}
  kit.gp.boxwidth = "#{0.8/kit.data.size} absolute"
  kit.gp.style = ["fill solid", "textbox opaque noborder fillcolor rgb 'white'"]
  kit.gp.yrange = "[#{[kit.data[0].y.data.min,0].min}:]"
  #kit.gp.xrange = "[-1:#{labels.size+1}]"
  kit.gp.xrange = "[-1.5:1]" if labels.size==1
  kit.gp.mytics = "5"
  kit.gp.ytics = "autofreq rotate by 45"
  kit.gp.grid = "ytics mytics lw 2,lw 1"
  kit.xlabel = nil
  kit.ylabel = nil
  kit.gp.decimalsign = 'locale "en_GB.UTF-8"'
  kit.gp.format = [%["%'.2f"]]
  i = -1
  kit.gp.xtics = "(#{labels.map{|l| %["#{l}" #{i+=1}]}.join(', ')}) rotate by 90 right"
  #pp ['kit222', kit, labels]
  fork do

    kit.gp.key = "off"
    kit.gnuplot_write("#{name}2.eps", size: "#{[[labels.size.to_f/4.2, 5.0].min, 1.0].max}in,4.5in")
    system %[ps2epsi #{name}2.eps #{name}.eps]
    system %[epstopdf #{name}.eps]

  end
  fork do
    kit.gp.key = "tmargin left Left reverse"
    kit.gp.border = "unset"
    kit.gp.xtics = "unset"
    kit.gp.ytics = "unset"
    kit.gp.title = "unset"
    kit.gp.xlabel = "unset"
    kit.gp.ylabel = "unset"
    kit.gp.label = "unset"
    #kit.gp.xrange = "[-100:-10]"
    kit.gp.boxwidth = "#{0.0/kit.data.size} absolute"
    kit.gp.object = " rect from screen 0, screen 0 to screen 1, graph 1 front fc rgb 'white' fillstyle solid noborder"     
    #kit.gp.style = "fill empty noborder"
    #kit.gp.yrange = "[-10:10]"
    kit.gnuplot_write("#{name}_key.eps", size: "4.0in,1.5in")
    system %[convert -density 500 #{name}_key.eps -resize 4000 -bordercolor white -border 20x20 -background white -flatten -trim  +repage #{name}_key.pdf]
    #%x[convert -density 500 #{name}.eps -resize 4000 -bordercolor white -border 20x20 -background white -flatten -trim  +repage #{name}.pdf]
  end

  #"\\begin{center}\\includegraphics[width=3.0in]{#{name}.eps}\\vspace{1em}\\end{center}"
  #"\\begin{center}\\includegraphics[width=0.9\\textwidth]{#{name}.eps}\\vspace{1em}\\end{center}"
  "\\myfigurerot{#{name}.pdf}{#{name}_key.pdf}{270}"
end

#expense_pie_charts_by_currency(name, accounts, &block) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/treasurer/report.rb', line 308

def expense_pie_charts_by_currency(name, accounts, &block)
  currencies = currency_list(accounts, &block)
  return (
    currencies.map do |curr|
      str = ""
      str << "\\subsubsection{#{curr}}\n" if curr
      str << expense_pie_chart(name + curr.to_s, accounts.find_all{|acc| acc.currency == curr and acc.should_report?}.map{|acc| acc.}, &block)
      str
    end
  ).join("\n\n")
end


792
793
794
795
796
# File 'lib/treasurer/report.rb', line 792

def footer
  <<EOF
\\end{document}
EOF
end

#generate_accountsObject

Construct the list of accounts. Because of double entry accounting # every entry has two accounts. External accounts are abstract accounts that do not correspond to a real account: e.g. “Food Expenditure”



130
131
132
133
134
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
162
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
# File 'lib/treasurer/report.rb', line 130

def generate_accounts
  accounts = @runs.map{|r| r.}.uniq.map{|acc| Account.new(acc, self, @runner, @runs, false)}
  external_accounts = (@runs.map{|r| r.}.uniq - accounts.map{|acc| acc.name} - ["Transfer"]).map{|acc| Account.new(acc, self, @runner, @runs, true)}
  #if not @report_currency
  external_accounts = external_accounts.map do |acc|
    if acc_inf = ACCOUNT_INFO[acc.name] and currencies = acc_inf[:currencies] and currencies.size > 1
      raise "Only expense accounts can have multiple currencies: #{acc.name} has type #{acc.type}" unless acc.type == :Expense
      new_accounts = currencies.map do |curr|
        Account.new(acc.name, self, @runner, @runs, true, currency: curr)
      end
      new_accounts.delete_if{|a| a.runs.size == 0}
      new_accounts
    else
      acc
    end
  end
  #end
  external_accounts = external_accounts.flatten
  @accounts = accounts + external_accounts
  @expense_accounts = @accounts.find_all{|acc| acc.type == :Expense}
  @accounts_hash = @accounts.map{|acc| [acc.name, acc]}.to_h

  if @report_currency
    @runs.each do |r|
      if (curr = @accounts_hash[r.].currency) != @report_currency
        er = EXCHANGE_RATES[[curr, @report_currency]]
        r.deposit *= er
        r.withdrawal *= er
        begin
          r.balance *= er if r.has_balance?
        rescue => err
          p [r., r.real_id, r.balance, er, r.data_line]
          raise err
        end
      end
    end
    ASSETS.each do |name, details|
      details[:size] *= EXCHANGE_RATES[[details[:currency], @report_currency]] if details[:currency]!=@report_currency
      details[:currency] = @report_currency
    end
    [REGULAR_TRANSFERS, FUTURE_TRANSFERS].each do |transfers|
      transfers.each do |accs, trans|
        #acc = accs.find{|a| p a, @accounts_hash.keys, @accounts.map{|ac| ac.name}; not @accounts_hash[a].external}
        trans.each do |item, details|
          if details[:currency] != @report_currency
            #p item, acc, curr, @report_currency
            details[:size] *= EXCHANGE_RATES[[details[:currency], @report_currency]]
            details[:currency] = @report_currency
          end
        end
      end
    end
    @accounts.each do |acc|
      if acc.info[:opening_balance]
        if acc.currency != @report_currency
          acc.info[:opening_balance] *= EXCHANGE_RATES[[acc.currency, @report_currency]]
        end
      end
      if acc.should_report?
        #p acc.name_c
        acc.
      end
      acc.instance_variable_set(:@original_currency, acc.currency)
      acc.instance_variable_set(:@currency, @report_currency)
      acc.info[:currencies] = [@report_currency]
    end

  end
  get_projected_accounts
  #p ['projected_accounts_info', @projected_accounts_info]
  #exit
  @equities = currency_list.map do |currency|
    equity = Equity.new(self, @runner, @accounts.find_all{|acc| acc.currency == currency}, currency: currency)
    @accounts.unshift (equity)
    [currency, equity]
  end
  @equities = @equities.to_h
end

#get_in_limit_discretionary_account_factor(currency) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/treasurer/report.rb', line 424

def (currency)
  @projected_account_factor = 1.0
  loop do
    ok = true
    date = @today
    while date < @today + @days_ahead
      ok = false if @equities[currency].projected_balance(date) < @equities[currency].red_line(date)
      date += 1
      #ep ['projected_account_factor', date, @equity.projected_balance(date),  @equity.red_line(date), ok]
    end
    @in_limit_discretionary_account_factors[currency] = @projected_account_factor
    ep ['projected_account_factor', @projected_account_factor, @equities[currency].projected_balance(date), currency, ok]
    break if (@projected_account_factor <= 0.0 or ok == true)
    @projected_account_factor -= 0.01
    @projected_account_factor -= 0.04
  end
  @projected_account_factor = nil
  #exit
end

#get_stable_discretionary_account_factor(currency) ⇒ Object

exit



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/treasurer/report.rb', line 443

def (currency)
  @projected_account_factor = 1.0
  loop do
    ok = true
    date = @today
    balances = []
    while date < @today + @days_ahead
      #ok = false if @equity.projected_balance(date) < @equity.red_line(date)
      date += 1
      balances.push @equities[currency].projected_balance(date)
      #ep ['projected_account_factor', date, balances.mean, @projected_account_factor,  @equities[currency].balance(@today), ok]
    end
    ok = false if balances.mean < @equities[currency].balance(@today) - 0.001
    #ok = false if balances.median < @equities[currency].balance(@today) - 0.001
    @stable_discretionary_account_factors[currency] = @projected_account_factor
    break if (@projected_account_factor <= 0.0 or ok == true)
    @projected_account_factor -= 0.01
    #@projected_account_factor -= 0.1
  end
  @projected_account_factor = nil
  #exit
end

#headerObject



727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/treasurer/report.rb', line 727

def header
  <<EOF
\\documentclass[a5paper, 10pt]{article}
%\\usepackage[scale=0.92]{geometry}
\\usepackage[left=0.9cm,top=0.9cm,right=0.9cm,bottom=0.5cm,nohead,includefoot]{geometry}
%\\usepackage[cm]{fullpage}
\\usepackage{tabulary}
\\usepackage{graphicx}
\\usepackage{multicol}
\\usepackage{hyperref}
%\\usepackage{libertine}
%\\usepackage{helvetica}
\\usepackage{fontspec}
\\setmainfont[
  BoldFont={Linux Biolinum O Bold},
  BoldItalicFont={Linux Biolinum O Bold},
  ]{Linux Biolinum O}
\\usepackage{xcolor,listings}
\\usepackage{epstopdf}
\\newcommand\\Tstrut{\\rule{0pt}{2.8ex}}
\\newcommand\\myfigurerot[3]{\\vspace*{1em}\\begin{center}

\\begin{minipage}{\\textwidth}
\\begin{center}
\\includegraphics[clip,height=0.90\\textwidth,angle=#3]{#1}

\\vspace*{0.5em}
\\includegraphics[width=0.5\\textwidth]{#2}
\\end{center}
\\end{minipage}
\\end{center}\\vspace*{0em}

}
\\newcommand\\myfigure[1]{\\vspace*{1em}\\begin{center}

\\includegraphics[clip,width=0.90\\textwidth]{#1}

\\end{center}\\vspace*{0em}

}
\\newcommand{\\transactionsize}{\\scriptsize}
\\lstset{%
basicstyle=\\ttfamily\\color{black},
identifierstyle = \\ttfamily\\color{purple},
keywordstyle=\\ttfamily\\color{blue},
stringstyle=\\color{orange}}
\\usepackage[bf,compact]{titlesec}
%\\titlespacing{\\section}{0pt}{*0}{*0}
%\\titlespacing{\\subsection}{0pt}{*0}{*2}
%\\titlespacing{\\subsubsection}{0pt}{*0}{*0}
%\\setlength{\\parskip}{0pt}
%\\setlength{\\parsep}{0pt}
%\\setlength{\\headsep}{0pt}
%\\setlength{\\topskip}{0pt}
%\\setlength{\\topmargin}{0pt}
%\\setlength{\\topsep}{0pt}
%\\setlength{\\partopsep}{0pt}
\\begin{document}
\\title{Budget Report from #{@start_date.strftime("%A #{@start_date.day.ordinalize} of %B %Y")} to #{@today.strftime("%A #{@today.day.ordinalize} of %B %Y")}}
\\author{With projections to #{@end_date.strftime("%A #{@end_date.day.ordinalize} of %B %Y")}}
\\date{\\today}
\\maketitle
\\tableofcontents
EOF
end

#reportObject



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
# File 'lib/treasurer/report.rb', line 209

def report
  generate_accounts
  #get_actual_accounts
  currency_list.each do |currency|
    (currency)
    (currency)
  end
  report = ""
  report << header
  #report << '\begin{multicols}{2}'
  report << 
  currency_list.each do |currency|
    report << (currency)
  end
  report << available_balances_table
  report << 
  report << 
  report << 
  #report << '\end{multicols}'
  ##report << account_resolutions
  #report << account_breakdown

  report << assumptions
  report << 
  report << footer

  File.open('report.tex', 'w'){|f| f.puts report}
  Process.waitall
  system "lualatex report.tex && lualatex report.tex"
end

#transactions_by_accountObject



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/treasurer/report.rb', line 694

def 
  <<EOF
\\section{Recent Transactions}
  #{@accounts.find_all{|acc| not acc.type == :Equity}.sort_by{|acc| acc.external ? 0 : 1}.map{|acc|
  "\\subsection{#{acc.name_c}}
\\transactionsize
  #{all = acc.runs.find_all{|r|  r.days_ago(@today) < @days_before}
  case acc.type
  when :Expense, :Income
    all = all.sort_by{|r| [r., r.date, r.id]}.reverse
  else
    all = all.sort_by{|r| [r.date, r.id]}.reverse
  end
  #ep ['acc', acc, 'ids', all.map{|r| r.id}, 'size', all.size]
all.pieces((all.size.to_f/60.to_f).ceil).map{|piece|
  "\\setlength{\\parindent}{0cm}\n\n\\begin{tabulary}{0.99\\textwidth}{ #{"c " * 3 + " l " + " r " * 3 + "l"}}
  #{piece.map{|r|
  (CodeRunner::Budget.rcp.component_results - [:sc] + [:]).map{|res|
entry = r.send(res).to_s.latex_escape
#if
entry = entry[0...25] if entry.length > 25
#if entry.length > 40
#entry = entry.split(/.{40}/).join(" \\newline ")
#end
entry
#rcp.component_results.map{|res| r.send(res).to_s.gsub(/(.{20})/, '\1\\\\\\\\').latex_escape
  }.join(" & ")
  }.join("\\\\\n")}
\\end{tabulary}"}.join("\n\n")}"
  }.join("\n\n")}
EOF
end