=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.34 retrieving revision 1.38 diff -u -r1.34 -r1.38 --- RT/Invoicing/rt_invoices.pl 2011/05/07 03:34:17 1.34 +++ RT/Invoicing/rt_invoices.pl 2011/06/21 01:20:28 1.38 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.33 2011/05/07 02:27:34 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.37 2011/05/26 01:09:36 andrew Exp $ ######################################################################## # Copyright (c) 2011 Andrew Fresh # @@ -96,28 +96,34 @@ foreach my $fee ( @{ $project->{fees} } ) { my $hours = hours_for_date( $cust->{invoice}, $fee->{date} ); - my $type = exists $hours->{ $fee->{type} } - && $hours->{ $fee->{type} } > 0 ? $fee->{type} : 'default'; + my $type = 'unknown'; + my $count = $fee->{count}; + while ( $type && $count > 0 && $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} > $count ) { + $hours->{$type} -= $count; + $discount_time = $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'; + $count -= $discount_time; + $fee->{detail} .= " $discount_time $type Hours Discounted"; + } } } @@ -129,8 +135,8 @@ 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 => [], ); @@ -156,7 +162,8 @@ 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,7 +476,7 @@ while ( my $txn = $txn_i->() ) { next if $state->txn_is_invoiced( $txn->id ); - if (my $expense = make_expense( $txn, $ticket ) ) { + if ( my $expense = make_expense( $txn, $ticket ) ) { push @{ $project{expenses} }, $expense; $project{transactions}{ $txn->id } = 1; } @@ -493,7 +500,7 @@ } next if $invoice->{end} < $fee->{date}; - push @{ $project{fees} }, $fee; + push @{ $project{fees} }, $fee; $project{transactions}{ $txn->id } = 1; } @@ -509,6 +516,7 @@ my $work_type = $txn->cf('WorkType'); if ( $work_type =~ s/\s*Onsite//i ) { + # XXX Do something special for onsite activities } @@ -545,6 +553,7 @@ . ( $txn->data || $ticket->subject ), amount => $amount, date => ymd_to_DateTime( $txn->created ), + # detail => ???, );