Created
February 6, 2015 01:37
-
-
Save pkrumins/f048ceb9e7e5520110da to your computer and use it in GitHub Desktop.
This file contains 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
use warnings; | |
use strict; | |
use Text::CSV; | |
use constant { | |
ID => 0, | |
TYPE => 1, | |
SOURCE => 2, | |
AMOUNT => 3, | |
FEE => 4, | |
NET => 5, | |
CURRENCY => 6, | |
DATE_CREATED => 7, | |
DATE_AVAILABLE => 8, | |
DESCRIPTION => 9 | |
}; | |
my $csvFile = shift or die "usage: $0 <balance_history.csv>"; | |
my $csv = Text::CSV->new; | |
open my $file, '<', $csvFile or die "unable to open $csvFile: $!"; | |
my ($amount, $fee, $net, $fee_refund, $fee_adj, $refund, $adjustment, $transfer); | |
while (my $row = $csv->getline($file)) { | |
next if $row->[ID] eq "id"; | |
my $created = $row->[DATE_CREATED]; | |
my ($date, $time) = split ' ', $created; | |
my $month = (split '-', $date)[1]; | |
$month =~ s/^0//; | |
if ($row->[TYPE] eq "charge") { | |
$amount->{$month} += $row->[AMOUNT]; | |
$fee->{$month} += $row->[FEE]; | |
$net->{$month} += $row->[NET]; | |
} | |
elsif ($row->[TYPE] eq "refund") { | |
$refund->{$month} += $row->[AMOUNT]; | |
$fee_refund->{$month} += $row->[FEE]; | |
$net->{$month} += $row->[NET]; | |
} | |
elsif ($row->[TYPE] eq "adjustment") { | |
$adjustment->{$month} += $row->[AMOUNT]; | |
$fee_adj->{$month} += $row->[FEE]; | |
$net->{$month} += $row->[NET]; | |
} | |
elsif ($row->[TYPE] eq "transfer") { | |
$transfer->{$month} += $row->[AMOUNT]; | |
} | |
} | |
my $total; | |
printf "MONTH AMOUNT FEE REFUND REFUND_FEE ADJUSTMENT ADJUSTMENT_FEE NET TRANSFER\n"; | |
for (1..12) { | |
printf "%-5d %-8.02f %-8.02f %-8.02f %-10.02f %-10.02f %-14.02f %-8.02f %-8.02f\n", $_, $amount->{$_}, $fee->{$_}, $refund->{$_} || 0, $fee_refund->{$_} || 0, $adjustment->{$_} || 0, $fee_adj->{$_} || 0, $net->{$_}, $transfer->{$_}; | |
$total += $amount->{$_}; | |
} | |
printf " %-8.02f\n", $total; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment