diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2014-11-14 10:13:46 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2014-11-14 10:13:46 +0100 |
commit | c78c80e41609e4aa138b4c34047eccaa92d8196a (patch) | |
tree | a87e6db9d10bfc0ac693670ab709ad45301172cd | |
parent | 20e52cca19d06d84ca6f3e4294549022dc7518a7 (diff) | |
download | imusician-reports-c78c80e41609e4aa138b4c34047eccaa92d8196a.tar.gz imusician-reports-c78c80e41609e4aa138b4c34047eccaa92d8196a.tar.bz2 imusician-reports-c78c80e41609e4aa138b4c34047eccaa92d8196a.zip |
Report sales by shop.
-rw-r--r-- | lib/reports/sales_by_shop.rb | 37 | ||||
-rw-r--r-- | lib/sale.rb | 5 | ||||
-rw-r--r-- | parse_report.rb | 4 |
3 files changed, 43 insertions, 3 deletions
diff --git a/lib/reports/sales_by_shop.rb b/lib/reports/sales_by_shop.rb new file mode 100644 index 0000000..7308b1d --- /dev/null +++ b/lib/reports/sales_by_shop.rb @@ -0,0 +1,37 @@ +module SalesReporter + module Reports + class SalesByShop + def self.render(sales) + sales_by_shop = {} + + sales.each do |s| + shop = s.shop + if sales_by_shop[shop] + sales_by_shop[shop][:count] += s.quantity + sales_by_shop[shop][:total] += s.amount + else + sales_by_shop[shop] = {:count => s.quantity, :total => s.amount} + end + end + + sorted = sales_by_shop.sort { |a,b| b[1][:count] <=> a[1][:count] } + + puts "Shop: Streams: Revenue:" + puts "------------------------------------------" + + total_rev = 0 + total_streams = 0 + + sorted.each do |key, row| + puts "#{key.slice(0, 19).ljust(20)}: #{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/lib/sale.rb b/lib/sale.rb index 057927c..17c6f18 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,13 +1,14 @@ module SalesReporter class Sale - attr_reader :date, :amount, :quantity, :country + attr_reader :date, :amount, :quantity, :country, :shop - def initialize(date_, amount, quantity, country) + def initialize(date_, amount, quantity, country, shop) @date = sanitize_date(date_) raise TypeError('sanitize failed') unless @date.is_a? Date @amount = amount @quantity = quantity @country = country + @shop = shop end private diff --git a/parse_report.rb b/parse_report.rb index 5410e05..ad000bc 100644 --- a/parse_report.rb +++ b/parse_report.rb @@ -1,6 +1,7 @@ require 'sale' require 'reports/sales_by_date' require 'reports/sales_by_country' +require 'reports/sales_by_shop' require 'csv' csv_opts = { @@ -14,9 +15,10 @@ sales = [] CSV.foreach(ARGV[0], csv_opts) do |row| if row.count > 0 - sales << SalesReporter::Sale.new(row["Date"], row["revenue EUR"], row["quantity"], row["Country"]) + sales << SalesReporter::Sale.new(row["Date"], row["revenue EUR"], row["quantity"], row["Country"], row["Shop"]) end end SalesReporter::Reports::SalesByDate.render(sales) SalesReporter::Reports::SalesByCountry.render(sales) +SalesReporter::Reports::SalesByShop.render(sales) |