Skip to content

Instantly share code, notes, and snippets.

@pkrumins
Created February 6, 2015 01:37
Show Gist options
  • Save pkrumins/f048ceb9e7e5520110da to your computer and use it in GitHub Desktop.
Save pkrumins/f048ceb9e7e5520110da to your computer and use it in GitHub Desktop.
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