=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- RT/Invoicing/rt_invoices.pl 2011/05/07 02:09:43 1.32 +++ RT/Invoicing/rt_invoices.pl 2011/05/07 03:27:34 1.33 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.31 2011/05/07 01:03:55 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.32 2011/05/07 01:09:43 andrew Exp $ ######################################################################## # Copyright (c) 2011 Andrew Fresh # @@ -76,11 +76,6 @@ my $iterator = $results->get_iterator; while ( my $ticket = &$iterator ) { - if ( !$ticket->time_worked ) { - say "Ticket " . $ticket->id . " has no time worked"; - next; - } - my $cust = find_customer_for_ticket($ticket); if ( !$cust ) { warn "No customer found for ticket " . $ticket->id; @@ -136,11 +131,13 @@ my %li = ( custid => $cust->{id}, invdate => DateTime->now( time_zone => 'local' )->ymd, + transactions => [], ); + my %transactions; foreach my $project ( @{ $invoice->{projects} } ) { if ( $project->{transactions} ) { - push @{ $li{transactions} }, @{ $project->{transactions} }; + %transactions = ( %transactions, %{ $project->{transactions} } ); } my $subtotal = 0; foreach my $fee ( @{ $project->{fees} } ) { @@ -153,6 +150,7 @@ $project->{total} = $subtotal; $invoice->{total} += $subtotal; } + @{ $li{transactions} } = sort { $a <=> $b } keys %transactions; if ( $invoice->{discount} ) { my $c = "Included Hours\n"; @@ -469,9 +467,15 @@ my $txns = $ticket->transactions( type => [qw(Comment Correspond)] ); my $txn_i = $txns->get_iterator; while ( my $txn = $txn_i->() ) { - next unless $txn->time_taken; next if $state->txn_is_invoiced( $txn->id ); + if (my $expense = make_expense( $txn, $ticket ) ) { + push @{ $project{expenses} }, $expense; + $project{transactions}{ $txn->id } = 1; + } + + next unless $txn->time_taken; + my $fee = make_fee( $txn, $cust->{rates}, $ticket ); if ( !( $fee->{rate} && $fee->{count} ) ) { @@ -490,7 +494,7 @@ next if $invoice->{end} < $fee->{date}; push @{ $project{fees} }, $fee; - push @{ $project{transactions} }, $txn->id; + $project{transactions}{ $txn->id } = 1; } return \%project; @@ -520,6 +524,24 @@ } return \%fee; +} + +sub make_expense { + my ( $txn, $ticket ) = @_; + + my $amount = $txn->cf('ExpenseAmount') or return; + + my %expense = ( + id => $txn->id, + contents => $txn->created . ' (' + . $txn->id . ')' . "\n\n" + . ( $txn->data || $ticket->subject ), + amount => $amount, + date => ymd_to_DateTime( $txn->created ), + # detail => ???, + ); + + return \%expense; } sub hours_for_date {