#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use lib 'lib';
use RTI::State;
use List::Util qw( sum );
#my $custid = 'jscherz@itc-home.com';
my $custid = 'GSG';
my $state = RTI::State->new('rt_invoice.state');
my @payments = reverse @{ $state->{payment}->{$custid} || [] };
my $paid = sum map { $_->{paid} } @payments;
my $invoiced = sum map { $_->{total} }
grep { $_->{custid} eq $custid } values %{ $state->{invoice} };
my $running = 0;
foreach my $invoice ( sort { $a <=> $b } keys %{ $state->{invoice} } ) {
my $i = $state->{invoice}->{$invoice};
next unless $i->{custid};
next unless $i->{custid} eq $custid;
next if $i->{void};
while ( @payments && $payments[0]->{date} < $i->{invdate} ) {
show_payment( shift @payments );
}
next unless $i->{total};
$running += $i->{total};
my $date = $i->{invdate} ? $i->{invdate}->ymd : 'Earlier';
printf "\$%7.02f : %10s invoice %03d \$ %6.02f\n", $running,
$date, $invoice, $i->{total};
}
show_payment($_) for @payments;
printf "invoiced: \$ %6.02f paid: \$ %6.02f balance: \$%7.02f\n",
$invoiced,
$paid, $running;
sub show_payment {
my ($p) = @_;
my $type = $p->{type} || '';
$type .= " $p->{number}" if $p->{number};
my $date = $p->{date} ? $p->{date}->ymd : 'Earlier';
$running -= $p->{paid};
printf "\$%7.02f : %10s payment for \$ %6.02f %s\n", $running, $date,
$p->{paid}, $type;
}