[BACK]Return to rt_invoices.pl CVS log [TXT][DIR] Up to [local] / RT / Invoicing

Diff for /RT/Invoicing/rt_invoices.pl between version 1.3 and 1.4

version 1.3, 2011/03/21 04:19:25 version 1.4, 2011/03/21 05:01:33
Line 80 
Line 80 
   
             $end->subtract( seconds => 1 );              $end->subtract( seconds => 1 );
   
             $startdate = $start->clone if $startdate > $start;              $startdate = $start->clone if !$startdate || $startdate > $start;
             $invoice{start} ||= $start->clone;              $invoice{start} ||= $start->clone;
             $invoice{end} = $end->clone;              $invoice{end} = $end->clone;
             my %hours = (              my %hours = (
Line 137 
Line 137 
     # XXX This should be a config option      # XXX This should be a config option
     qw/ open stalled resolved /;      qw/ open stalled resolved /;
   
 #push @limits,  
 #    {  
 #    attribute => 'resolved',  
 #    operator  => '>=',  
 #    value     => '2011-03-01'  
 #    };  
 if ($startdate) {  if ($startdate) {
     push @limits,      push @limits,
         {          {
Line 152 
Line 146 
         };          };
 }  }
   
 #push @limits, { attribute => 'id', operator => '=', value => '51' };  
 #push @limits, { attribute => '', operator => '', value => '' };  
   
 my $results = $tickets->search(  my $results = $tickets->search(
     limits  => \@limits,      limits  => \@limits,
     orderby => 'id',      orderby => 'id',
 );  );
   
 #print Dump $ticket, $results;  
   
 my $count = $results->count;  my $count = $results->count;
 print "There are $count results that matched your query\n";  print "There are $count results that matched your query\n";
   
 my $iterator = $results->get_iterator;  my $iterator = $results->get_iterator;
 while ( my $ticket = &$iterator ) {  while ( my $ticket = &$iterator ) {
       my $invoice = find_invoice_for_ticket( \@invoices, $ticket );
   
       if ( !$invoice ) {
           say "No invoice found for ticket " . $ticket->id;
   
           # XXX should construct a "new" invoice to pop onto the list
           next;
       }
   
     my %project = (      my %project = (
         id     => $ticket->id,          id     => $ticket->id,
         queue  => $ticket->queue,          queue  => $ticket->queue,
Line 182 
Line 180 
         expenses => [],          expenses => [],
     );      );
   
     my $invoice;  
 INVOICE: foreach my $i (@invoices) {  
         next INVOICE unless $i->{match};  
         foreach my $m ( @{ $i->{match} } ) {  
             my $type = $m->{type};  
             my $thing = join ' ', $ticket->$type;  
   
             if ( $m->{$type} ) {  
                 my $match = lc $m->{$type};  
                 next INVOICE unless lc($thing) ~~ $match;  
             }  
             elsif ( $m->{regex} ) {  
                 next INVOICE unless $thing ~~ /\Q$m->{regex}\E/;  
             }  
             else {  
                 warn "Invalid match!";  
                 next INVOICE;  
             }  
         }  
         $invoice = $i;  
     }  
   
     if ( !$invoice ) {  
         say "No invoice found for ticket " . $ticket->id;  
   
         # XXX should construct a "new" invoice to pop onto the list  
         next;  
     }  
   
     #foreach my $r ($ticket->requestors) {      #foreach my $r ($ticket->requestors) {
     #    $users->id( $r );      #    $users->id( $r );
     #    $users->retrieve;      #    $users->retrieve;
Line 238 
Line 207 
         next if $invoice->{start} && $invoice->{start} > $txn_date;          next if $invoice->{start} && $invoice->{start} > $txn_date;
         next if $invoice->{end} < $txn_date;          next if $invoice->{end} < $txn_date;
   
         my $hours = {};          my $fee = make_fee( $ticket, $txn, $invoice->{rates} );
         if ( $invoice->{hours} ) {          push @{ $project{fees} }, $fee;
             foreach my $h ( @{ $invoice->{hours} } ) {  
                 next if $h->{start} && $h->{start} > $txn_date;  
                 next if $h->{end} < $txn_date;  
   
                 $hours = $h->{hours};          my $hours = hours_for_date( $invoice, $txn_date );
                 last;  
             }  
         }  
   
         my $work_time = sprintf "%.03f", $txn->time_taken / 60;  
         my $work_type = $txn->cf('WorkType');  
         my $work_rate  
             = $invoice->{rates}{$work_type}  
             || $invoice->{rates}{default}  
             || 0;  
   
         my $h_type          my $h_type
             = exists $hours->{$work_type}              = exists $hours->{ $fee->{type} }
             ? $work_type              ? $fee->{type}
             : 'default';              : 'default';
   
         my %fee = (          next unless exists $hours->{$h_type} && $hours->{$h_type} > 0;
             id       => $txn->id,  
             contents => $txn->created . ' ('  
                 . $txn->id . ')' . "\n\n"  
                 . ( $txn->data || $ticket->subject ),  
             count => $work_time,  
             rate  => $work_rate,  
         );  
         if ( $work_type && $work_type ne 'Normal' ) {  
             $fee{detail} = $work_type . ' rate';  
         }  
   
         push @{ $project{fees} }, \%fee;  
   
         next unless $hours->{$h_type} && $hours->{$h_type} > 0;  
   
         my $discount_time = 0;          my $discount_time = 0;
         if ( $hours->{$h_type} > $work_time ) {          if ( $hours->{$h_type} > $fee->{count} ) {
             $hours->{$h_type} -= $work_time;              $hours->{$h_type} -= $fee->{count};
             $discount_time = $work_time;              $discount_time = $fee->{count};
         }          }
         else {          else {
             $discount_time = $hours->{$h_type};              $discount_time = $hours->{$h_type};
Line 288 
Line 230 
         }          }
   
         if ($discount_time) {          if ($discount_time) {
             $fee{detail} = "$discount_time $work_type Hours Discounted";              $invoice->{discount}{amount} += $discount_time * $fee->{rate};
             $invoice->{discount}{amount} += $discount_time * $fee{rate};              $invoice->{discount}{hours}{$h_type} += $discount_time;
             $invoice->{discount}{hours}{$work_type} += $discount_time;  
               $h_type = '' if $h_type eq 'default';
               $fee->{detail} = "$discount_time $h_type Hours Discounted";
         }          }
   
         #print Dump $txn; exit;          #print Dump $txn; exit;
Line 366 
Line 310 
     #$amount =~ s/\.\d\d\K.*$//;      #$amount =~ s/\.\d\d\K.*$//;
     #return $amount;      #return $amount;
     return sprintf "%.02f", $amount;      return sprintf "%.02f", $amount;
   }
   
   sub find_invoice_for_ticket {
       my ( $invoices, $ticket ) = @_;
   
   INVOICE: foreach my $i ( @{$invoices} ) {
           next INVOICE unless $i->{match};
           foreach my $m ( @{ $i->{match} } ) {
               my $type = $m->{type};
               my $thing = join ' ', $ticket->$type;
   
               if ( $m->{$type} ) {
                   my $match = lc $m->{$type};
                   next INVOICE unless lc($thing) ~~ $match;
               }
               elsif ( $m->{regex} ) {
                   next INVOICE unless $thing ~~ /\Q$m->{regex}\E/;
               }
               else {
                   warn "Invalid match!";
                   next INVOICE;
               }
           }
           return $i;
       }
   }
   
   sub make_fee {
       my ( $ticket, $txn, $rates ) = @_;
   
       my $work_time = sprintf "%.03f", $txn->time_taken / 60;
       my $work_type = $txn->cf('WorkType');
       my $work_rate = $rates->{$work_type} || $rates->{default} || 0;
   
       my %fee = (
           id       => $txn->id,
           contents => $txn->created . ' ('
               . $txn->id . ')' . "\n\n"
               . ( $txn->data || $ticket->subject ),
           count => $work_time,
           rate  => $work_rate,
           type  => $work_type,
       );
   
       if ( $work_type && $work_type ne 'Normal' ) {
           $fee{detail} = $work_type . ' rate';
       }
   
       return \%fee;
   }
   
   sub hours_for_date {
       my ( $invoice, $date ) = @_;
   
       my $hours = {};
       if ( $invoice->{hours} ) {
           foreach my $h ( @{ $invoice->{hours} } ) {
               next if $h->{start} && $h->{start} > $date;
               next if $h->{end} < $date;
   
               $hours = $h->{hours};
               last;
           }
       }
       return $hours;
 }  }
   
 package RTI::Config;  package RTI::Config;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>