Created
December 23, 2011 17:03
-
-
Save waaa/1514766 to your computer and use it in GitHub Desktop.
Script for refunds and charges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def script file_name | |
array = [] | |
trans_hash = {} | |
File.open(file_name){ |f| f.each { |s|array << s }} | |
hash = array.group_by{ |a| a.split[6]} | |
hash.each do |order, operations| | |
trans_hash[order] = [] | |
operations.each do |operation| | |
if operation.present? | |
fields = operation.split | |
price = fields[5] | |
if price.include? "." | |
price = price.to_f.abs | |
else | |
price = (price.to_f/100).abs | |
end | |
trans_hash[order] << [fields[7].to_sym, price] if (fields[7] == 'Charged' || fields[7] == 'Refunded') | |
end | |
end | |
end | |
trans_hash.each{|k,v| trans_hash.delete k if v.blank?} | |
trans_hash.each do |k, order| | |
previous_sum = order[0][1] | |
n = 2 | |
if order.count % 2 == 0 | |
order.insert(0, [:Charged, order[0][1]]) | |
end | |
while order[n].present? | |
order[n-1][1] = previous_sum - order[n][1] | |
previous_sum = order[n][1] | |
puts "order '#{k}' went under zero" if previous_sum < 0 | |
n = n + 2 | |
end | |
order.each_with_index{|arr, i| order.delete(arr) if (arr[0] == :Charged && i != 0)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment