=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.46 retrieving revision 1.47 diff -u -r1.46 -r1.47 --- RT/Invoicing/rt_invoices.pl 2012/01/27 04:13:45 1.46 +++ RT/Invoicing/rt_invoices.pl 2012/01/31 04:36:28 1.47 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.45 2011/12/31 02:14:32 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.46 2012/01/27 04:13:45 andrew Exp $ ######################################################################## # Copyright (c) 2011 Andrew Fresh # @@ -28,6 +28,8 @@ use File::Path; use DateTime; +use List::Util qw/ sum /; + use lib './lib'; # XXX This is fragile, there are better ways use RTI::Config; use RTI::State; @@ -135,7 +137,8 @@ if ( my $unpaid_invoices = $state->unpaid_invoices() ) { foreach my $custid ( keys %{$unpaid_invoices} ) { - my %project = ( title => 'Unpaid Invoices', fees => [], ); + my %project + = ( title => 'Unpaid Invoices', fees => [], no_total => 1 ); my $past_due = 0; my $unpaid = 0; @@ -158,9 +161,7 @@ my $content = sprintf( "Invoice %06d from %s", $id, $invdate->ymd ); - if ( $cust->{duedate} - && DateTime->compare( $invdate, $cust->{duedate} ) > 0 ) - { + if ( $cust->{duedate} && $invdate < $cust->{duedate}) { $content = "PAST DUE: $content"; $past_due += $unpaid_invoices->{$custid}->{$id}; } @@ -178,12 +179,10 @@ } if ($past_due) { - $cust->{invoice} ||= make_invoice(); + $cust->{invoice} ||= make_invoice($cust); $cust->{invoice}->{past_due} = $past_due; $cust->{invoice}->{unpaid} = $unpaid; - $cust->{invoice}->{total_due} - = $cust->{invoice}->{total} + $past_due + $unpaid; unshift @{ $cust->{invoice}->{projects} }, \%project; } @@ -211,6 +210,8 @@ $subtotal += round( $expense->{amount} ); } $project->{total} = $subtotal; + + next if $project->{no_total}; $invoice->{total} += $subtotal; } @{ $invoice->{transactions} } = sort { $a <=> $b } keys %transactions; @@ -229,6 +230,11 @@ $invoice->{total} -= round( $invoice->{discount}{amount} ); } + if ($invoice->{past_due}) { + $invoice->{total_due} + = sum( @{ $invoice }{ qw/ total past_due unpaid / } ); + } + next unless $invoice->{total} > 0 || $invoice->{total_due}; $invoice->{info} = $config->get('info'); @@ -340,8 +346,10 @@ sub make_invoice { my ($cust) = @_; - my %invoice - = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) ); + my %invoice = ( + end => $cust->{billend}->clone->subtract( seconds => 1 ), + total => 0, + ); $invoice{start} = $cust->{startinvoicedate}->clone if $cust->{startinvoicedate};