aboutsummaryrefslogtreecommitdiffstats
path: root/lib/reports
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2014-11-14 09:45:23 +0100
committerHarald Eilertsen <haraldei@anduin.net>2014-11-14 09:55:38 +0100
commit20e52cca19d06d84ca6f3e4294549022dc7518a7 (patch)
tree94f8fc38d177ca0ae0e5523fd591f07b43814221 /lib/reports
parent6ed672e4439cf6be54781dd87abcc815d09cd5d1 (diff)
downloadimusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.tar.gz
imusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.tar.bz2
imusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.zip
Report sales by country.
Diffstat (limited to 'lib/reports')
-rw-r--r--lib/reports/sales_by_country.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/reports/sales_by_country.rb b/lib/reports/sales_by_country.rb
new file mode 100644
index 0000000..58f1b15
--- /dev/null
+++ b/lib/reports/sales_by_country.rb
@@ -0,0 +1,36 @@
+module SalesReporter
+ module Reports
+ class SalesByCountry
+ def self.render(sales)
+ sales_by_country = {}
+
+ sales.each do |s|
+ c = s.country
+ if sales_by_country[c]
+ sales_by_country[c][:count] += s.quantity
+ sales_by_country[c][:total] += s.amount
+ else
+ sales_by_country[c] = {:count => s.quantity, :total => s.amount}
+ end
+ end
+
+ sorted = sales_by_country.sort { |a,b| b[1][:count] <=> a[1][:count] }
+
+ puts "Country: Streams: Revenue:"
+ puts "------------------------------------------"
+
+ total_rev = 0
+ total_streams = 0
+
+ sorted.each do |key, row|
+ puts "#{key}: #{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