-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastercsv_version.rb
62 lines (47 loc) · 1.47 KB
/
fastercsv_version.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'csv'
# ===================== import product ine
products = {}
CSV.foreach("./import_files/product_file1.csv", {:headers => :first_row}) do |row|
if row.nil? || row.empty? || row[0][0] == "#"
next
end
hash = row.to_hash
products[hash['product_id']] = hash
end
# ====================== process transactions
transactions = {}
CSV.foreach("./import_files/transactions.csv", {:headers => :first_row}) do |row|
if row.nil? || row.empty? || row[0][0] == "#"
next
end
hash = row.to_hash
item_trans = transactions[hash['product_id']]
item_trans ||= []
item_trans << hash
transactions[hash['product_id']] = item_trans
end
total_revenue = 0
# ======================== reporting results
puts "DETAIL REPORT"
products.each do |key, value|
puts " Product: #{value['name']}"
high = low = avg = quantity = total_qty = count = revenue = 0
transactions[value['product_id']].each do |transaction|
count += 1
quant = transaction['quantity'].to_i
price = transaction['price'].to_f
avg += price
high = price if price > high
low = price if (price < low) || (low == 0)
total_qty += quant
revenue += quant * price
total_revenue += quant * price
end
avg = avg / count if count > 0
puts " High:#{high}"
puts " Low: #{low}"
puts " Avg: #{avg}"
puts " Total QTY: #{total_qty}"
puts " Total Revenue: #{revenue}"
end
puts "\n\n >>>>>>>> TOTOAL REVENUE: #{total_revenue}"