=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.31 retrieving revision 1.36 diff -u -r1.31 -r1.36 --- RT/Invoicing/rt_invoices.pl 2011/05/07 02:03:55 1.31 +++ RT/Invoicing/rt_invoices.pl 2011/05/18 20:03:17 1.36 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.30 2011/05/03 04:41:11 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.35 2011/05/18 19:02:36 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; @@ -101,28 +96,32 @@ foreach my $fee ( @{ $project->{fees} } ) { my $hours = hours_for_date( $cust->{invoice}, $fee->{date} ); - my $type = exists $hours->{ $fee->{type} } - && $hours->{ $fee->{type} } > 0 ? $type : 'default'; + my $type = 'unknown'; + while ( $type && $type ne 'default' ) { + $type = exists $hours->{ $fee->{type} } + && $hours->{ $fee->{type} } > 0 ? $fee->{type} : 'default'; - next unless exists $hours->{$type} && $hours->{$type} > 0; + next unless exists $hours->{$type} && $hours->{$type} > 0; - my $discount_time = 0; - if ( $hours->{$type} > $fee->{count} ) { - $hours->{$type} -= $fee->{count}; - $discount_time = $fee->{count}; - } - else { - $discount_time = $hours->{$type}; - $hours->{$type} = 0; - } + my $discount_time = 0; + if ( $hours->{$type} > $fee->{count} ) { + $hours->{$type} -= $fee->{count}; + $discount_time = $fee->{count}; + } + else { + $discount_time = $hours->{$type}; + $hours->{$type} = 0; + } - if ($discount_time) { - $cust->{invoice}->{discount}->{amount} - += round( $discount_time * $fee->{rate} ); - $cust->{invoice}->{discount}->{hours}{$type} += $discount_time; + if ($discount_time) { + $cust->{invoice}->{discount}->{amount} + += round( $discount_time * $fee->{rate} ); + $cust->{invoice}->{discount}->{hours}{$type} + += $discount_time; - $type = '' if $type eq 'default'; - $fee->{detail} = "$discount_time $type Hours Discounted"; + $type = '' if $type eq 'default'; + $fee->{detail} .= " $discount_time $type Hours Discounted"; + } } } @@ -134,13 +133,15 @@ next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} }; my %li = ( - custid => $cust->{id}, - invdate => DateTime->now( time_zone => 'local' )->ymd, + 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,12 +154,14 @@ $project->{total} = $subtotal; $invoice->{total} += $subtotal; } + @{ $li{transactions} } = sort { $a <=> $b } keys %transactions; if ( $invoice->{discount} ) { my $c = "Included Hours\n"; if ( $invoice->{discount}{hours} ) { foreach my $t ( keys %{ $invoice->{discount}{hours} } ) { - $c .= "\n$invoice->{discount}{hours}{$t} $t hour"; + my $type = $t eq 'default' ? '' : $t; + $c .= "\n$invoice->{discount}{hours}{$t} $type hour"; $c .= 's' if $invoice->{discount}{hours}{$t} != 1; $c .= "\n"; } @@ -469,9 +472,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} ) ) { @@ -489,8 +498,8 @@ } next if $invoice->{end} < $fee->{date}; - push @{ $project{fees} }, $fee; - push @{ $project{transactions} }, $txn->id; + push @{ $project{fees} }, $fee; + $project{transactions}{ $txn->id } = 1; } return \%project; @@ -504,6 +513,14 @@ my $work_time = sprintf "%.03f", $txn->time_taken / 60; my $work_type = $txn->cf('WorkType'); + if ( $work_type =~ s/\s*Onsite//i ) { + + # XXX Do something special for onsite activities + } + + $work_type =~ s/^\s+|\s+$//g; + $work_type ||= 'Normal'; + my %fee = ( id => $txn->id, contents => $txn->created . ' (' @@ -520,6 +537,25 @@ } 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 {