#!/usr/bin/perl use strict; use Date::Manip; use IO::Handle; use Data::Dumper; $| = 1; use vars qw( $infile $outfile $errfile $rulesfile ); use vars qw( $in $out $err $rules ); $infile = 'data/all-epfarms-paypal-20060810.csv'; $outfile = 'data/paypal-ok.csv'; $errfile = 'data/paypal-bad.csv'; $rulesfile = 'data/paypal-rules.csv'; open($in, "$infile"); open($out, ">$outfile"); open($err, ">$errfile"); open($rules, "$rulesfile"); sub getline { my ($file) = @_; my @content; if(my $line = <$file>) { @content = ($line =~ /\s*"?(.*?)"?(?:,|$)/g); } return @content; } my @cols = getline($in); foreach my $col (@cols) { print "Col: '$col'\n"; } my @data; # Now we suck in the whole file print "Sucking in file..."; while(my @row = getline($in)) { my $rowhash; foreach my $col (@cols) { $rowhash->{$col} = shift @row; } push @data, $rowhash; } print "done.\n"; print "Reversing data (to get a better date order)..."; @data = reverse(@data); print "done.\n"; print "Combining related rows..."; my %ref; my @combined_data; for(my $i = 0; $i < $#data; $i++) { next if $data[$i]->{Type} =~ /^Update to .*$/; next if defined $ref{$data[$i]->{'Transaction ID'}}; if($data[$i]->{'Reference Txn ID'} =~ /\w/) { my $ref_id = $data[$i]->{'Reference Txn ID'}; my $row = $data[$ref{$ref_id}]; if(!$row) { print "Hrm... didn't find existing row...\n"; } $row->{Currency} = $data[$i]->{Currency}; $row->{Gross} += $data[$i]->{Gross}; $row->{Fee} += $data[$i]->{Fee}; $row->{Net} += $data[$i]->{Net}; print "Adding secondary txn to $ref_id - $ref{$ref_id} (".$data[$i]->{"From Email Address"}.")\n"; } else { $ref{$data[$i]->{'Transaction ID'}} = $i; } } foreach my $id (keys %ref) { push @combined_data, $data[$ref{$id}]; } @data = @combined_data; print "done.\n"; # Now we go through the rules and assign usernames to each row print "Applying rules..."; while(my @row = getline($rules)) { my ($col, $regex, $user) = @row; #print "\n Trying rule: '$col', '$regex', '$user'..."; foreach my $rowhash (@data) { if($rowhash->{$col} =~ /$regex/i) { #print " applied rule to column " . $rowhash->{'Transaction ID'} . "..."; $rowhash->{username} = $user; } } } print "done.\n"; my @export_cols = ('From Email Address', 'Time', 'Date','Transaction ID', 'Currency','Gross','Fee','Net' ); # Now we dump the results print "Dumping results..."; foreach my $rowhash (@data) { my %rowhash = %$rowhash; if($rowhash->{username}) { my @outrow = @rowhash{'username', @export_cols}; @outrow = map {'"' . $_ . '"'} @outrow; print $out (join(',', @outrow)) . "\n"; my ($username, $date, $amount) = @rowhash{'username','Date','Net'}; $date = UnixDate($date,"%Y-%m-%d"); open my $userfile, '>>', "data/userdata/$username.txt" or die "Err..."; print $userfile "$date\t$amount\tPayment through paypal\n"; close $userfile; } else { #my @outrow = @rowhash{@export_cols}; my @outrow = values %rowhash; @outrow = map {'"' . $_ . '"'} @outrow; print $err (join(',', @outrow)) . "\n"; } } print "done.\n";