diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2014-11-13 18:03:41 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2014-11-13 18:03:41 +0100 |
commit | 6ed672e4439cf6be54781dd87abcc815d09cd5d1 (patch) | |
tree | b220628e1f636520db5c58f372ff9ce438979b57 | |
parent | 33b2c7b846962bc92b7ea32049ab2e0502cd4d80 (diff) | |
download | imusician-reports-6ed672e4439cf6be54781dd87abcc815d09cd5d1.tar.gz imusician-reports-6ed672e4439cf6be54781dd87abcc815d09cd5d1.tar.bz2 imusician-reports-6ed672e4439cf6be54781dd87abcc815d09cd5d1.zip |
Move reporting to Reports::SalesByDate class.
-rw-r--r-- | lib/reports/sales_by_date.rb | 34 | ||||
-rw-r--r-- | parse_report.rb | 27 |
2 files changed, 36 insertions, 25 deletions
diff --git a/lib/reports/sales_by_date.rb b/lib/reports/sales_by_date.rb new file mode 100644 index 0000000..9e623f1 --- /dev/null +++ b/lib/reports/sales_by_date.rb @@ -0,0 +1,34 @@ +module SalesReporter + module Reports + class SalesByDate + def self.render(sales) + sales_by_date = {} + + sales.each do |s| + d = s.date + if sales_by_date[d] + sales_by_date[d][:count] += s.quantity + sales_by_date[d][:total] += s.amount + else + sales_by_date[d] = {:count => s.quantity, :total => s.amount} + end + end + + puts "Date: Streams: Revenue:" + puts "------------------------------------------" + + total_rev = 0 + total_streams = 0 + + sales_by_date.each do |key, row| + puts "#{key.iso8601}: #{row[:count].to_s.rjust(7)} #{row[:total].round(5).to_s.rjust(10)} EUR" + total_rev += row[:total] + total_streams += row[:count] + end + + puts "------------------------------------------" + puts "Total: #{total_streams.to_s.rjust(7)} #{total_rev.round(5).to_s.rjust(10)} EUR" + end + end + end +end diff --git a/parse_report.rb b/parse_report.rb index efaa231..40df679 100644 --- a/parse_report.rb +++ b/parse_report.rb @@ -1,4 +1,5 @@ require 'sale' +require 'reports/sales_by_date' require 'csv' csv_opts = { @@ -9,7 +10,6 @@ csv_opts = { } sales = [] -sales_by_date = {} CSV.foreach(ARGV[0], csv_opts) do |row| if row.count > 0 @@ -17,27 +17,4 @@ CSV.foreach(ARGV[0], csv_opts) do |row| end end -sales.each do |s| - d = s.date - if sales_by_date[d] - sales_by_date[d][:count] += s.quantity - sales_by_date[d][:total] += s.amount - else - sales_by_date[d] = {:count => s.quantity, :total => s.amount} - end -end - -puts "Date: Streams: Revenue:" -puts "------------------------------------------" - -total_rev = 0 -total_streams = 0 - -sales_by_date.each do |key, row| - puts "#{key.iso8601}: #{row[:count].to_s.rjust(7)} #{row[:total].round(5).to_s.rjust(10)} EUR" - total_rev += row[:total] - total_streams += row[:count] -end - -puts "------------------------------------------" -puts "Total: #{total_streams.to_s.rjust(7)} #{total_rev.round(5).to_s.rjust(10)} EUR" +SalesReporter::Reports::SalesByDate.render(sales) |