aboutsummaryrefslogtreecommitdiffstats
path: root/lib/reports
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2014-11-14 10:13:46 +0100
committerHarald Eilertsen <haraldei@anduin.net>2014-11-14 10:13:46 +0100
commitc78c80e41609e4aa138b4c34047eccaa92d8196a (patch)
treea87e6db9d10bfc0ac693670ab709ad45301172cd /lib/reports
parent20e52cca19d06d84ca6f3e4294549022dc7518a7 (diff)
downloadimusician-reports-c78c80e41609e4aa138b4c34047eccaa92d8196a.tar.gz
imusician-reports-c78c80e41609e4aa138b4c34047eccaa92d8196a.tar.bz2
imusician-reports-c78c80e41609e4aa138b4c34047eccaa92d8196a.zip
Report sales by shop.
Diffstat (limited to 'lib/reports')
-rw-r--r--lib/reports/sales_by_shop.rb37
1 files changed, 37 insertions, 0 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