Skip to content

Instantly share code, notes, and snippets.

@timanglade
Created May 7, 2010 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timanglade/393041 to your computer and use it in GitHub Desktop.
Save timanglade/393041 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'activesupport'
def days_in_month(year, month)
(Date.new(year, 12, 31) << (12-month)).day
end
f = File.new("items.csv", "r")
puts "Enter the current account position:"
current_position = gets.to_f
@positions = [[Time.now, current_position, "Position Actuelle", ""]]
date = Time.now
while (date <= (Time.now + 9.months))
date += 1.day
position = [date, @positions.last.second, "", ""]
ae_total = 0
f.each_line do |l|
next if l == "\n"
a = l.split(", ")
#puts a.join(" | ")
if a.first == "every" and (date.day == a.second.to_i)
#puts "got an every"
position[1] = position.second + a.third.to_f
elsif a.first == "only" and (date.to_date == Time.parse(a.second).to_date)
position[1] += a.third.to_f
position[2] += (a.third.to_f > 0 ? " Income " : a[3])
position[3] += (a.third.to_f > 0 ? a[3] : "#{a.third}€")
elsif a.first == "accrued"
#puts "got an accrued"
position[1] = position.second + (a.second.to_f/days_in_month(date.year, date.month))
end
if (a.last == "ae\n")
t = Time.parse(a.second)
d = Time.parse([t.year, t.month+2, 5].join("/"))
if date.to_date == d.to_date
ae_total += (a.third.to_i * -0.205)
position[1] += ae_total
position[2] = "Ursaff"
position[3] = "#{ae_total.to_i}€ for #{t.strftime("%B")}"
end
else
#puts "no ae found"
end
end
@positions << position
f.rewind
end
g = File.new("account.html", "w")
g.write <<EOF
<html>
<head>
<meta charset="utf-8">
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages':['annotatedtimeline'], 'language' : 'fr'});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Position');
data.addColumn('string', 'title1');
data.addColumn('string', 'text1');
data.addRows([
EOF
g.puts @positions.collect {|a| "[new Date(#{a.first.year},#{a.first.month-1},#{a.first.day}), \
#{a.second.to_i}, \
#{a[2].empty? ? "undefined" : "'"+a[2].gsub("\n", " ")+"'" }, \
#{a[3].empty? ? "undefined" : "'"+a[3].gsub("\n", " ")+"'" }]"}.join(",\n")
g.write <<EOF
]);
var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
chart.draw(data, {displayAnnotations: true, displayAnnotationsFilter: true, allValuesSuffix: "€"});
}
</script>
</head>
<body>
<div id='chart_div' style='width: 1200px; height: 600px;'></div>
</body>
</html>
EOF
g.close
system "open account.html"
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 4 columns, instead of 3. in line 2.
every, 4, -800, Loyer
every, 4, -500, Carte Bleue
accrued, -800, Autres dépenses
only, 2010/02/03, 3100, Hypermonth, ae
only, 2010/04/01, 17350, Acompte Posc, ae
only, 2010/05/01, 5387, Posc, ae
only, 2010/06/01, 5387, Posc, ae
only, 2010/07/01, 5387, Posc, ae
only, 2010/08/01, 5387, Posc, ae
only, 2010/05/10, 12000, Davero, ae
only, 2010/04/03, -1084, Remboursement Avocat
only, 2010/04/12, -800, Remboursement
only, 2010/06/03, -900, Remboursement
only, 2010/04/02, -447, Iacif
only, 2010/05/16, -3373, Taxes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment