[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.1 and 1.5

version 1.1, 2011/03/21 00:55:41 version 1.5, 2011/03/21 18:36:59
Line 12 
Line 12 
   
 use DateTime;  use DateTime;
   
 my %rates;  
 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 61 
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 || $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 );  
         }          }
   
         push @{ $invoice{projects} }, \%project;          if ( @{ $project{fees} } ) {
               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 126 
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 141 
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,
         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 => [],
     );      );
   
     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 209 
Line 191 
     while ( my $txn = $txn_i->() ) {      while ( my $txn = $txn_i->() ) {
         next unless $txn->time_taken;          next unless $txn->time_taken;
   
         my $work_time = sprintf "%.03f", $txn->time_taken / 60;          my ( $date, $time ) = split ' ', $txn->created;
         my $work_type = $txn->cf('WorkType');          my ( $year, $month,  $day )    = split '-', $date;
         my $work_rate = $rates{$work_type} || $rates{default} || 0;          my ( $hour, $minute, $second ) = split ':', $time;
   
         my $ih_type          my $txn_date = DateTime->new(
             = exists $included_hours{$work_type}              year   => $year,
             ? $work_type              month  => $month,
             : 'default';              day    => $day,
               hour   => $hour,
         my %fee = (              minute => $minute,
             id       => $txn->id,              second => $second,
             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 if $invoice->{start} && $invoice->{start} > $txn_date;
           next if $invoice->{end} < $txn_date;
   
         next          my $fee = make_fee( $ticket, $txn, $invoice->{rates} );
             unless $included_hours{$ih_type} && $included_hours{$ih_type} > 0;          push @{ $project{fees} }, $fee;
   
           my $hours = hours_for_date( $invoice, $txn_date );
   
           my $h_type
               = exists $hours->{ $fee->{type} }
               ? $fee->{type}
               : 'default';
   
           next unless exists $hours->{$h_type} && $hours->{$h_type} > 0;
   
         my $discount_time = 0;          my $discount_time = 0;
         if ( $included_hours{$ih_type} > $work_time ) {          if ( $hours->{$h_type} > $fee->{count} ) {
             $included_hours{$ih_type} -= $work_time;              $hours->{$h_type} -= $fee->{count};
             $discount_time = $work_time;              $discount_time = $fee->{count};
         }          }
         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) {
             $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 295 
Line 281 
         $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;
     $invoice->{file} = 'invoice_' . $invoiceid . '.pdf';      $invoice->{file} = 'invoice_' . $invoiceid . '.pdf';
   
Line 320 
Line 312 
     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;
 use strict;  use strict;
 use warnings;  use warnings;
   
 use 5.010;  use 5.010;
   
 use YAML::Any qw/ LoadFile /;  use YAML::Any qw/ LoadFile Dump Load /;
   
 sub new {  sub new {
     my ( $class, $args ) = @_;      my ( $class, $args ) = @_;
   
     my $self = { file => '', };      my $self = { file => '', };
     bless $self, $class;      bless $self, $class;
   
     my $file = $args->{file} || $self->_find_config;      my $file = $args->{file} || $self->_find_config;
     $self->read_config($file);      $self->read_config($file);
   
Line 364 
Line 422 
     if ( $c->{default} ) {      if ( $c->{default} ) {
         foreach my $cust ( @{ $c->{customers} } ) {          foreach my $cust ( @{ $c->{customers} } ) {
             foreach my $k ( keys %{ $c->{default} } ) {              foreach my $k ( keys %{ $c->{default} } ) {
                 $cust->{$k} //= $c->{default}->{$k};                  $cust->{$k} //= Load( Dump( $c->{default}->{$k} ) );
             }              }
         }          }
     }      }
Line 375 
Line 433 
   
 sub get {  sub get {
     my ( $self, $key ) = @_;      my ( $self, $key ) = @_;
       return Load( Dump( $self->{_config}->{$key} ) );
     # XXX This should deep copy? not a reference would be good  
     return $self->{_config}->{$key};  
 }  }
   
 package RTI::State;  package RTI::State;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

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