-
Notifications
You must be signed in to change notification settings - Fork 0
/
rakefile
52 lines (43 loc) · 1.17 KB
/
rakefile
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
require 'csv'
task :default => ["output/totals.csv"]
directory "output"
directory "data"
CLEAN_FILES = FileList['tmp/*','output/*']
CLEAN_FILES.clear_exclude
task :clean do
rm CLEAN_FILES
end
file "output/totals.csv" do
files = Dir.glob('data/*.csv')
order_no = nil
paid = nil?
items = Hash.new
files.sort.each do |f|
CSV.foreach(f, {:headers => true}) do |row|
if row["Name"] != order_no
order_no = row["Name"]
paid = row["Financial Status"]
end
if paid == "paid" or paid == "authorized" or paid == "pending"
qty = row["Lineitem quantity"].to_i
#show all variants
item = row["Lineitem name"].split("-")[0].strip
#group by product parent
#item = row["Lineitem name"].split("-")[0].strip
puts item
price = row["Lineitem price"].to_f
if items[item].nil?
items[item] = Hash[ :qty=>0, :amount=>0.0 ]
end
items[item][:qty] += qty
items[item][:amount] += (price * qty).round(2)
end
end
end
CSV.open("output/totals.csv", "wb") do |csv|
csv << [ "Item", "Quantity", "Value" ]
items.sort_by { |key, value| value[:qty] }.reverse.each do |key,value|
csv << [ key, value[:qty], value[:amount] ]
end
end
end