aboutsummaryrefslogtreecommitdiffstats
path: root/lib
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
parent6ed672e4439cf6be54781dd87abcc815d09cd5d1 (diff)
downloadimusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.tar.gz
imusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.tar.bz2
imusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.zip
Report sales by country.
Diffstat (limited to 'lib')
-rw-r--r--lib/reports/sales_by_country.rb36
-rw-r--r--lib/sale.rb5
2 files changed, 39 insertions, 2 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
diff --git a/lib/sale.rb b/lib/sale.rb
index f02a471..057927c 100644
--- a/lib/sale.rb
+++ b/lib/sale.rb
@@ -1,12 +1,13 @@
module SalesReporter
class Sale
- attr_reader :date, :amount, :quantity
+ attr_reader :date, :amount, :quantity, :country
- def initialize(date_, amount, quantity)
+ def initialize(date_, amount, quantity, country)
@date = sanitize_date(date_)
raise TypeError('sanitize failed') unless @date.is_a? Date
@amount = amount
@quantity = quantity
+ @country = country
end
private