[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.2 and 1.3

version 1.2, 2011/03/21 01:05:38 version 1.3, 2011/03/21 04:19:25
Line 12 
Line 12 
   
 use DateTime;  use DateTime;
   
 my %included_hours;  
   
 my $config = RTI::Config->new();  my $config = RTI::Config->new();
   
 #print Dump $config; exit;  #print Dump $config; exit;
   
 # my $state = RTI::State->new($cust);  
 # $invoice{state} = $state;  
 my $lastinvdate;    # = $state->{lastinvoicedte}; XXX Needs to be a DateTime  
   
 #$lastinvdate = DateTime->now->subtract( months => 2 );  
 my $invoiceid = 1;    # $state->{lastinvoice} + 1;  my $invoiceid = 1;    # $state->{lastinvoice} + 1;
   
 my $startdate;  my $startdate;
 my @invoices;  my @invoices;
 foreach my $cust ( @{ $config->get('customers') } ) {  foreach my $cust ( @{ $config->get('customers') } ) {
   
       # my $state = RTI::State->new($cust);
       # $invoice{state} = $state;
       my $lastinvdate;  # = $state->{lastinvoicedte}; XXX Needs to be a DateTime
                         #$lastinvdate = DateTime->now->subtract( months => 2 )
                         #    ->set( hour => 0, minute => 0, second => 0 );
   
     my %invoice = (      my %invoice = (
         from  => $config->get('from'),          from  => $config->get('from'),
           info  => $config->get('info'),
         to    => $cust->{address},          to    => $cust->{address},
         rates => $cust->{rates},          rates => $cust->{rates},
         match => $cust->{match},          match => $cust->{match},
     );      );
   
     if ( $cust->{base_rate} ) {      if ( $cust->{base_rate} ) {
         my $date = DateTime->now;          my $day  = $cust->{day}       || 1;
         my $day  = $cust->{day} || 1;  
         my $freq = $cust->{frequency} || 1;          my $freq = $cust->{frequency} || 1;
   
         my $diff;          my $day_method;
         my $per;          my $per;
         given ( $cust->{per} ) {          given ( $cust->{per} ) {
             when ('week')  { $per = 'weeks';  $diff = $date->dow - $day; }              when ('week')  { $per = 'weeks';  $day_method = 'dow' }
             when ('month') { $per = 'months'; $diff = $date->day - $day; }              when ('month') { $per = 'months'; $day_method = 'day' }
             default { die "Unknown per [$cust->{per}]\n" }              default { die "Unknown per [$cust->{per}]\n" }
         }          }
   
         # $day is start day, end should be one day further back          my $lastbill
         $diff = abs($diff) + 1;              = DateTime->now->set( hour => 0, minute => 0, second => 0 );
         $date->subtract( days => $diff );          while ( $lastbill->$day_method != $day ) {
               $lastbill->subtract( days => 1 );
           }
   
           my $date
               = $lastinvdate
               ? $lastinvdate->clone->add( days => 1 )
               : $lastbill->clone->subtract( $per => $freq );
   
         my $title          my $title
             = $freq == 1              = $freq == 1
             ? ucfirst( $cust->{per} . 'ly' )              ? ucfirst( $cust->{per} . 'ly' )
Line 60 
Line 67 
   
         my %project = ( title => $title, fees => [], );          my %project = ( title => $title, fees => [], );
   
         # XXX need to add them until we get to where we billed already          while ( $date < $lastbill ) {
         # if we don't know the last invoice date, assume the day before              my $start = $date->clone;
         $lastinvdate ||= $date->clone->subtract( days => 1 );  
         while ( $date > $lastinvdate ) {              $date->add( $per => $freq );
             $invoice{enddate} = $date->clone;              $date = $lastbill->clone if $date > $lastbill;
               if ( my $diff = $date->$day_method - $day ) {
                   $date->subtract( days => $diff );
               }
   
               my $end = $date->clone;
   
               $end->subtract( seconds => 1 );
   
               $startdate = $start->clone if $startdate > $start;
               $invoice{start} ||= $start->clone;
               $invoice{end} = $end->clone;
             my %hours = (              my %hours = (
                 end   => $date->clone,                  start => $start->clone,
                   end   => $end->clone,
                 hours => { %{ $cust->{hours} } },                  hours => { %{ $cust->{hours} } },
             );              );
   
             my $contents = ' to ' . $date->ymd;              push @{ $invoice{hours} }, \%hours;
             $date->subtract( $per => $freq )->add( days => 1 );              push @{ $project{fees} },
             $contents = $date->ymd . $contents;  
   
             $invoice{startdate} ||= $date->clone;  
             $hours{start} = $date->clone;  
             $startdate = $date->clone;  
   
             unshift @{ $invoice{hours} }, \%hours;  
             unshift @{ $project{fees} },  
                 {                  {
                 count    => 1,                  count    => 1,
                 rate     => $cust->{base_rate},                  rate     => $cust->{base_rate},
                 contents => $contents,                  contents => $start->ymd . ' to ' . $end->ymd,
                 };                  };
   
             # Next time, one day less  
             $date->subtract( days => 1 );  
         }          }
   
         if (@{ $project{fees} }) {          if ( @{ $project{fees} } ) {
             push @{ $invoice{projects} }, \%project;              push @{ $invoice{projects} }, \%project;
         }          }
     }      }
     else {      else {
         $invoice{enddate} = DateTime->now->ymd;          $invoice{end} = DateTime->now;
         push @{ $invoice{hours} },          push @{ $invoice{hours} },
             { end => DateTime->now, hours => $cust->{hours} };              { end => DateTime->now, hours => $cust->{hours} };
     }      }
   
       next unless $invoice{end};
     push @invoices, \%invoice;      push @invoices, \%invoice;
 }  }
   
Line 161 
Line 171 
         id     => $ticket->id,          id     => $ticket->id,
         queue  => $ticket->queue,          queue  => $ticket->queue,
         owner  => $ticket->owner,          owner  => $ticket->owner,
         title  => $ticket->id . ': ' . $ticket->subject,          title  => $ticket->subject,
         detail => 'Requestors: '          detail => 'Ticket: '
             . join( ', ', $ticket->requestors )              . $ticket->id
             . ' Queue: '              . ' Queue: '
             . $ticket->queue,              . $ticket->queue
               . ' Requestors: '
               . join( ', ', $ticket->requestors ),
         fees     => [],          fees     => [],
         expenses => [],          expenses => [],
     );      );
Line 210 
Line 222 
     while ( my $txn = $txn_i->() ) {      while ( my $txn = $txn_i->() ) {
         next unless $txn->time_taken;          next unless $txn->time_taken;
   
           my ( $date, $time ) = split ' ', $txn->created;
           my ( $year, $month,  $day )    = split '-', $date;
           my ( $hour, $minute, $second ) = split ':', $time;
   
           my $txn_date = DateTime->new(
               year   => $year,
               month  => $month,
               day    => $day,
               hour   => $hour,
               minute => $minute,
               second => $second,
           );
   
           next if $invoice->{start} && $invoice->{start} > $txn_date;
           next if $invoice->{end} < $txn_date;
   
           my $hours = {};
           if ( $invoice->{hours} ) {
               foreach my $h ( @{ $invoice->{hours} } ) {
                   next if $h->{start} && $h->{start} > $txn_date;
                   next if $h->{end} < $txn_date;
   
                   $hours = $h->{hours};
                   last;
               }
           }
   
         my $work_time = sprintf "%.03f", $txn->time_taken / 60;          my $work_time = sprintf "%.03f", $txn->time_taken / 60;
         my $work_type = $txn->cf('WorkType');          my $work_type = $txn->cf('WorkType');
         my $work_rate          my $work_rate
Line 217 
Line 256 
             || $invoice->{rates}{default}              || $invoice->{rates}{default}
             || 0;              || 0;
   
         my $ih_type          my $h_type
             = exists $included_hours{$work_type}              = exists $hours->{$work_type}
             ? $work_type              ? $work_type
             : 'default';              : 'default';
   
Line 236 
Line 275 
   
         push @{ $project{fees} }, \%fee;          push @{ $project{fees} }, \%fee;
   
         next          next unless $hours->{$h_type} && $hours->{$h_type} > 0;
             unless $included_hours{$ih_type} && $included_hours{$ih_type} > 0;  
   
         my $discount_time = 0;          my $discount_time = 0;
         if ( $included_hours{$ih_type} > $work_time ) {          if ( $hours->{$h_type} > $work_time ) {
             $included_hours{$ih_type} -= $work_time;              $hours->{$h_type} -= $work_time;
             $discount_time = $work_time;              $discount_time = $work_time;
         }          }
         else {          else {
             $discount_time = $included_hours{$ih_type};              $discount_time = $hours->{$h_type};
             $included_hours{$ih_type} = 0;              $hours->{$h_type} = 0;
         }          }
   
         if ($discount_time) {          if ($discount_time) {
Line 297 
Line 335 
   
     if ( $invoice->{past_due} ) {      if ( $invoice->{past_due} ) {
         $invoice->{total_due} = $invoice->{total} + $invoice->{past_due};          $invoice->{total_due} = $invoice->{total} + $invoice->{past_due};
       }
   
       foreach my $key (qw/ start end /) {
           if ( exists $invoice->{$key} ) {
               $invoice->{$key} = $invoice->{$key}->strftime('%B %d, %Y');
           }
     }      }
   
     $invoice->{id}   = $invoiceid;      $invoice->{id}   = $invoiceid;

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

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