=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.43 retrieving revision 1.48 diff -u -r1.43 -r1.48 --- RT/Invoicing/rt_invoices.pl 2011/12/30 03:30:51 1.43 +++ RT/Invoicing/rt_invoices.pl 2012/11/28 02:23:34 1.48 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.42 2011/12/30 03:20:45 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.47 2012/01/31 04:36:28 andrew Exp $ ######################################################################## # Copyright (c) 2011 Andrew Fresh # @@ -28,9 +28,12 @@ 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; +use RTI::Util qw/ round ymd_to_DateTime /; my $config = RTI::Config->new(); my $state = RTI::State->new( $config->get('state') ); @@ -85,12 +88,10 @@ next if $cust->{no_invoice}; $cust->{invoice} ||= make_invoice($cust); - if ( !$cust->{invoice} ) { - $cust->{no_invoice} = 1; - say "$cust->{id} has no open invoices [" . $ticket->id . ']'; - next; - } + die "$cust->{id} has no open invoices [" . $ticket->id . ']' + unless $cust->{invoice}; + say 'Ticket ' . $ticket->id . " belongs to $cust->{id}"; my $project = make_project( $ticket, $cust ); @@ -133,6 +134,90 @@ push @{ $cust->{invoice}->{projects} }, $project; } + +if ( my $unpaid_invoices = $state->unpaid_invoices() ) { + foreach my $custid ( keys %{$unpaid_invoices} ) { + my %project + = ( title => 'Unpaid Invoices', fees => [], no_total => 1 ); + my $past_due = 0; + my $unpaid = 0; + + my $cust; + foreach ( @{$customers} ) { + if ( $_->{id} eq $custid ) { + $cust = $_; + last; + } + } + $cust ||= fake_customer($custid); + + foreach my $id ( + sort { $a <=> $b } + keys %{ $unpaid_invoices->{$custid} } + ) + { + my $unpaid = $state->get_invoice($id); + my $invdate = ymd_to_DateTime( $unpaid->{invdate} ); + + my $content + = sprintf( "Invoice %06d from %s", $id, $invdate->ymd ); + if ( $cust->{duedate} && $invdate < $cust->{duedate}) { + $content = "PAST DUE: $content"; + $past_due += $unpaid_invoices->{$custid}->{$id}; + } + else { + $unpaid += $unpaid_invoices->{$custid}->{$id}; + } + + push @{ $project{fees} }, + { + id => $id, + contents => $content, + count => 1, + rate => $unpaid_invoices->{$custid}->{$id}, + }; + } + + if ($past_due) { + $cust->{invoice} ||= make_invoice($cust); + + $cust->{invoice}->{past_due} = $past_due; + $cust->{invoice}->{unpaid} = $unpaid; + + unshift @{ $cust->{invoice}->{projects} }, \%project; + } + } +} + +if ( my $credits = $state->credits ) { + foreach my $custid ( keys %{$credits} ) { + + my $cust; + foreach ( @{$customers} ) { + if ( $_->{id} eq $custid ) { + $cust = $_; + last; + } + } + + next unless $cust; + next unless $cust->{invoice}; + + $cust->{invoice}->{credit} = $credits->{$custid}; + + unshift @{ $cust->{invoice}->{projects} }, { + title => 'Credits', + no_total => 1, + fees => [ + { contents => 'Available Credit', + count => 1, + rate => -$credits->{$custid}, + } + ], + }; + } +} + foreach my $cust ( @{$customers} ) { my $invoice = $cust->{invoice}; next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} }; @@ -154,6 +239,8 @@ $subtotal += round( $expense->{amount} ); } $project->{total} = $subtotal; + + next if $project->{no_total}; $invoice->{total} += $subtotal; } @{ $invoice->{transactions} } = sort { $a <=> $b } keys %transactions; @@ -172,35 +259,9 @@ $invoice->{total} -= round( $invoice->{discount}{amount} ); } - if ( my $unpaid_invoices = $state->unpaid_invoices( $cust->{id} ) ) { - my %project = ( title => 'Unpaid Invoices', fees => [], ); - my $past_due = 0; - - foreach my $id ( sort { $a <=> $b } keys %{$unpaid_invoices} ) { - my $unpaid = $state->get_invoice($id); - my $invdate = ymd_to_DateTime( $unpaid->{invdate} ); - - next - if $cust->{duedate} - && DateTime->compare( $invdate, $cust->{duedate} ) > 0; - - $past_due += $unpaid_invoices->{$id}; - push @{ $project{fees} }, - { - id => $id, - contents => - sprintf( "Invoice %06d from %s", $id, $invdate->ymd ), - count => 1, - rate => $unpaid_invoices->{$id}, - }; - } - - if ($past_due) { - $invoice->{past_due} = $past_due; - $invoice->{total_due} = $invoice->{total} + $invoice->{past_due}; - - unshift @{ $invoice->{projects} }, \%project; - } + if ($invoice->{past_due}) { + $invoice->{total_due} + = sum( @{ $invoice }{ qw/ total past_due unpaid / } ); } next unless $invoice->{total} > 0 || $invoice->{total_due}; @@ -219,12 +280,6 @@ $state->add_invoice($invoice); - foreach my $key (qw/ start end /) { - if ( exists $invoice->{$key} ) { - $invoice->{$key} = $invoice->{$key}->strftime('%B %d, %Y'); - } - } - my $invoice_dir = $config->get('invoice_dir'); File::Path::make_path($invoice_dir); my $file = join '/', $invoice_dir, $invoice->{file}; @@ -242,14 +297,6 @@ $state->save; -sub round { - my ($amount) = @_; - - #$amount =~ s/\.\d\d\K.*$//; - #return $amount; - return sprintf "%.02f", $amount; -} - sub find_customer_for_ticket { my ( $ticket, $customers ) = @_; @@ -271,13 +318,17 @@ } } - # Fake customer if we didn't find one - my $cust = $config->new_customer; + return fake_customer($ticket->requestors); +} - ( $cust->{id} ) = $ticket->requestors; - return unless $cust->{id}; +sub fake_customer { + my ($custid) = @_; + return unless $custid; + + my $cust = $config->new_customer; push @{$customers}, $cust; + $cust->{id} = $custid; $cust->{match} = [ { type => 'requestors', regex => $cust->{id}, @@ -324,16 +375,13 @@ sub make_invoice { my ($cust) = @_; - return if $cust->{no_invoice}; - return unless $cust->{billstart}; - - 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}; - return if $invoice{start} && $invoice{start} > $invoice{end}; - if ( $cust->{base_rate} ) { my ( $project, $hours ) = make_base_project($cust); @@ -550,23 +598,6 @@ } } return $hours; -} - -sub ymd_to_DateTime { - my ($ymd) = @_; - my ( $date, $time ) = split /[\sT]/, $ymd; - my ( $year, $month, $day ) = split '-', $date; - my ( $hour, $minute, $second ) = split ':', $time if $time; - - return DateTime->new( - year => $year, - month => $month, - day => $day, - hour => $hour || 0, - minute => $minute || 0, - second => $second || 0, - time_zone => 'local', - ); } sub get_billing_frequency {