-
-
Save codesnik/1588000 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'active_support/core_ext' | |
require 'rspec' | |
require 'bigdecimal' | |
class BigDecimal | |
def inspect; to_s end | |
end | |
def script file_name | |
trans_hash = {} | |
File.open(file_name).each_line do |line| | |
next if line.blank? | |
row = parse_line line | |
if %W[Charged Refunded].include?(row[1]) | |
trans_hash[row[0]] ||= [] | |
trans_hash[row[0]] << row | |
end | |
end | |
trans_hash.each do |k, rows| | |
rows.replace transform(rows) | |
raise "order '#{k}' went under zero" if rows.map {|x| x[2] }.sum < 0 | |
end | |
trans_hash | |
end | |
def parse_line line | |
fields = line.split("\t") | |
oid = fields[4] | |
opcode = fields[5] | |
updated = DateTime.parse(fields[1] + " +0400") | |
pan = fields[6] | |
price = fields[3] | |
price = | |
if price.include? "." | |
BigDecimal(price).abs | |
else | |
BigDecimal(price).abs / 100 | |
end | |
[oid, opcode, price, pan, updated] | |
end | |
def transform rows | |
rows = rows.dup | |
previous_sum = rows.first[2] | |
rows.first[1] = 'Charged' | |
rows.delete_if { |r| r[1] != 'Charged' } | |
previous_sum = rows.first[2] | |
rows[1..-1].each do |row| | |
row[1] = 'Refunded' | |
previous_sum, row[2] = row[2], -(previous_sum - row[2]) | |
end | |
rows.delete_if {|r| r[2] == 0 } | |
rows | |
end | |
if $0 == __FILE__ | |
require 'pp' | |
pp script(ARGV.first) | |
end | |
describe "#transform" do | |
subject { transform(rows) } | |
context '["Charged"]' do | |
let :rows do | |
[["o781", "Charged", 10884.96, "513691xxxxxx8182", DateTime.parse('Wed, 01 Jun 2011 13:45:17 +0400')]] | |
end | |
it { should == | |
[["o781", "Charged", 10884.96, "513691xxxxxx8182", DateTime.parse('Wed, 01 Jun 2011 13:45:17 +0400')]] | |
} | |
end | |
context '["Charged", "Refunded", "Charged"]' do | |
let :rows do | |
[["o793", "Charged", 5688.89, "427684xxxxxx4936", DateTime.parse('Wed, 01 Jun 2011 14:35:51 +0400')], | |
["o793", "Refunded", 5688.89, "427684xxxxxx4936", DateTime.parse('Mon, 06 Jun 2011 20:28:57 +0400')], | |
["o793", "Charged", 1124.89, "427684xxxxxx4936", DateTime.parse('Mon, 06 Jun 2011 20:28:57 +0400')]] | |
end | |
it { should == | |
[["o793", "Charged", 5688.89, "427684xxxxxx4936", DateTime.parse('Wed, 01 Jun 2011 14:35:51 +0400')], | |
["o793", "Refunded", -(5688.89-1124.89), "427684xxxxxx4936", DateTime.parse('Mon, 06 Jun 2011 20:28:57 +0400')]] | |
} | |
end | |
context '["Refunded", "Charged"]' do | |
let :rows do | |
[["o375", "Refunded", 33810.97, "539726xxxxxx6004", DateTime.parse('Sun, 05 Jun 2011 17:30:44 +0400')], | |
["o375", "Charged", 27315.97, "539726xxxxxx6004", DateTime.parse('Sun, 05 Jun 2011 17:30:44 +0400')]] | |
end | |
it { should == | |
[["o375", "Charged", 33810.97, "539726xxxxxx6004", DateTime.parse('Sun, 05 Jun 2011 17:30:44 +0400')], | |
["o375", "Refunded", -(33810.97-27315.97), "539726xxxxxx6004", DateTime.parse('Sun, 05 Jun 2011 17:30:44 +0400')]] | |
} | |
end | |
context '["Charged", "Refunded", "Charged", "Refunded", "Charged"]' do | |
let :rows do | |
[["o1498", "Charged", 10366.93, "427938xxxxxx0624", DateTime.parse('Thu, 14 Jul 2011 20:58:37 +0400')], | |
["o1498", "Refunded", 10366.93, "427938xxxxxx0624", DateTime.parse('Wed, 03 Aug 2011 10:47:31 +0400')], | |
["o1498", "Charged", 5351.93, "427938xxxxxx0624", DateTime.parse('Wed, 03 Aug 2011 10:47:31 +0400')], | |
["o1498", "Refunded", 5351.93, "427938xxxxxx0624", DateTime.parse('Wed, 17 Aug 2011 15:01:13 +0400')], | |
["o1498", "Charged", 336.93, "427938xxxxxx0624", DateTime.parse('Wed, 17 Aug 2011 15:01:13 +0400')]] | |
end | |
it { should == | |
[["o1498", "Charged", 10366.93, "427938xxxxxx0624", DateTime.parse('Thu, 14 Jul 2011 20:58:37 +0400')], | |
["o1498", "Refunded", -(10366.93-5351.93), "427938xxxxxx0624", DateTime.parse('Wed, 03 Aug 2011 10:47:31 +0400')], | |
["o1498", "Refunded", -(5351.93-336.93), "427938xxxxxx0624", DateTime.parse('Wed, 17 Aug 2011 15:01:13 +0400')]] | |
} | |
end | |
context '["Charged", "Charged"]' do | |
let :rows do | |
[["o3765", "Charged", 20715.25, "510126xxxxxx0650", DateTime.parse('Tue, 06 Sep 2011 20:53:12 +0400')], | |
["o3765", "Charged", 20715.25, "510126xxxxxx0650", DateTime.parse('Mon, 26 Sep 2011 17:38:40 +0400')]] | |
end | |
it { should == | |
[["o3765", "Charged", 20715.25, "510126xxxxxx0650", DateTime.parse('Tue, 06 Sep 2011 20:53:12 +0400')]] | |
} | |
end | |
context '["Charged", "Refunded", "Refunded", "Charged"]' do | |
let :rows do | |
[["o8243", "Charged", 65399.38, "427229xxxxxx1136", DateTime.parse('Wed, 14 Dec 2011 20:43:45 +0400')], | |
["o8243", "Refunded", 65399.38, "427229xxxxxx1136", DateTime.parse('Mon, 26 Dec 2011 10:22:14 +0400')], | |
["o8243", "Refunded", 49192.38, "427229xxxxxx1136", DateTime.parse('Mon, 26 Dec 2011 10:59:00 +0400')], | |
["o8243", "Charged", 7991.88, "427229xxxxxx1136", DateTime.parse('Mon, 26 Dec 2011 10:59:00 +0400')]] | |
end | |
it { should == | |
[["o8243", "Charged", 65399.38, "427229xxxxxx1136", DateTime.parse('Wed, 14 Dec 2011 20:43:45 +0400')], | |
["o8243", "Refunded", -(65399.38-7991.88), "427229xxxxxx1136", DateTime.parse('Mon, 26 Dec 2011 10:59:00 +0400')]] | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment