[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.46 and 1.53

version 1.46, 2012/01/27 04:13:45 version 1.53, 2015/05/07 06:26:08
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $AFresh1: rt_invoices.pl,v 1.45 2011/12/31 02:14:32 andrew Exp $  # $AFresh1: rt_invoices.pl,v 1.52 2015/05/07 05:23:15 andrew Exp $
 ########################################################################  ########################################################################
 # Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com>  # Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com>
 #  #
Line 19 
Line 19 
 use warnings;  use warnings;
   
 use 5.010;  use 5.010;
   use experimental qw( switch smartmatch );
   
   # Because we don't have a real cert
   $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
   
 use Template;  use Template;
 use RT::Client::REST;  use RT::Client::REST;
 use RT::Client::REST::Ticket;  use RT::Client::REST::Ticket;
Line 28 
Line 32 
 use File::Path;  use File::Path;
 use DateTime;  use DateTime;
   
   use List::Util qw/ sum /;
   
 use lib './lib';    # XXX This is fragile, there are better ways  use lib './lib';    # XXX This is fragile, there are better ways
 use RTI::Config;  use RTI::Config;
 use RTI::State;  use RTI::State;
Line 135 
Line 141 
   
 if ( my $unpaid_invoices = $state->unpaid_invoices() ) {  if ( my $unpaid_invoices = $state->unpaid_invoices() ) {
     foreach my $custid ( keys %{$unpaid_invoices} ) {      foreach my $custid ( keys %{$unpaid_invoices} ) {
         my %project  = ( title => 'Unpaid Invoices', fees => [], );          my %project
               = ( title => 'Unpaid Invoices', fees => [], no_total => 1 );
         my $past_due = 0;          my $past_due = 0;
         my $unpaid   = 0;          my $unpaid   = 0;
   
Line 158 
Line 165 
   
             my $content              my $content
                 = sprintf( "Invoice %06d from %s", $id, $invdate->ymd );                  = sprintf( "Invoice %06d from %s", $id, $invdate->ymd );
             if ( $cust->{duedate}              if ( $cust->{duedate} && $invdate < $cust->{duedate}) {
                 && DateTime->compare( $invdate, $cust->{duedate} ) > 0 )  
             {  
                 $content = "PAST DUE: $content";                  $content = "PAST DUE: $content";
                 $past_due += $unpaid_invoices->{$custid}->{$id};                  $past_due += $unpaid_invoices->{$custid}->{$id};
             }              }
Line 178 
Line 183 
         }          }
   
         if ($past_due) {          if ($past_due) {
             $cust->{invoice} ||= make_invoice();              $cust->{invoice} ||= make_invoice($cust);
   
             $cust->{invoice}->{past_due} = $past_due;              $cust->{invoice}->{past_due} = $past_due;
             $cust->{invoice}->{unpaid}   = $unpaid;              $cust->{invoice}->{unpaid}   = $unpaid;
             $cust->{invoice}->{total_due}  
                 = $cust->{invoice}->{total} + $past_due + $unpaid;  
   
             unshift @{ $cust->{invoice}->{projects} }, \%project;              unshift @{ $cust->{invoice}->{projects} }, \%project;
         }          }
     }      }
 }  }
   
   if ( my $credits = $state->credits ) {
       foreach my $custid ( keys %{$credits} ) {
   
           my $cust;
           foreach ( @{$customers} ) {
               if ( $_->{id} eq $custid ) {
                   $cust = $_;
                   last;
               }
           }
   
           next unless $cust;
           next unless $cust->{invoice};
           next unless $credits->{$custid} < 0;
   
           $cust->{invoice}->{credit} = $credits->{$custid};
   
           unshift @{ $cust->{invoice}->{projects} }, {
               title    => 'Credits',
               no_total => 1,
               fees     => [
                   {   contents => 'Available Credit',
                       count    => 1,
                       rate     => -$credits->{$custid},
                   }
               ],
           };
       }
   }
   
 foreach my $cust ( @{$customers} ) {  foreach my $cust ( @{$customers} ) {
     my $invoice = $cust->{invoice};      my $invoice = $cust->{invoice};
     next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} };      next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} };
Line 211 
Line 244 
             $subtotal += round( $expense->{amount} );              $subtotal += round( $expense->{amount} );
         }          }
         $project->{total} = $subtotal;          $project->{total} = $subtotal;
   
           next if $project->{no_total};
         $invoice->{total} += $subtotal;          $invoice->{total} += $subtotal;
     }      }
     @{ $invoice->{transactions} } = sort { $a <=> $b } keys %transactions;      @{ $invoice->{transactions} } = sort { $a <=> $b } keys %transactions;
Line 229 
Line 264 
         $invoice->{total} -= round( $invoice->{discount}{amount} );          $invoice->{total} -= round( $invoice->{discount}{amount} );
     }      }
   
       if ($invoice->{past_due}) {
           $invoice->{total_due}
               = sum( @{ $invoice }{ qw/ total past_due unpaid / } );
       }
   
     next unless $invoice->{total} > 0 || $invoice->{total_due};      next unless $invoice->{total} > 0 || $invoice->{total_due};
   
     $invoice->{info} = $config->get('info');      $invoice->{info} = $config->get('info');
Line 340 
Line 380 
 sub make_invoice {  sub make_invoice {
     my ($cust) = @_;      my ($cust) = @_;
   
     my %invoice      my %invoice = (
         = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) );          end   => $cust->{billend}->clone->subtract( seconds => 1 ),
           total => 0,
       );
     $invoice{start} = $cust->{startinvoicedate}->clone      $invoice{start} = $cust->{startinvoicedate}->clone
         if $cust->{startinvoicedate};          if $cust->{startinvoicedate};
   
Line 384 
Line 426 
     while ( $date < $billend ) {      while ( $date < $billend ) {
         my $start = $date->clone;          my $start = $date->clone;
   
         $date->add($freq);          $date->add_duration($freq);
   
         my $end = $date > $billend ? $billend->clone : $date->clone;          my $end = $date > $billend ? $billend->clone : $date->clone;
         $end->subtract( seconds => 1 );          $end->subtract( seconds => 1 );
Line 500 
Line 542 
     # XXX Only need $ticket for the alternate subject      # XXX Only need $ticket for the alternate subject
   
     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') || '';
   
     if ( $work_type =~ s/\s*Onsite//i ) {      if ( $work_type =~ s/\s*Onsite//i ) {
   
Line 599 
Line 641 
         }          }
     }      }
   
     return $newest_invoice->clone->subtract($max_duration)      $newest_invoice ||= DateTime->now;
   
       return $newest_invoice->clone->subtract_duration($max_duration)
         ->subtract( days => 1 );          ->subtract( days => 1 );
 }  }
   
Line 612 
Line 656 
     my $end = DateTime->now( time_zone => 'local' )      my $end = DateTime->now( time_zone => 'local' )
         ->set( hour => 0, minute => 0, second => 0 );          ->set( hour => 0, minute => 0, second => 0 );
   
     my $start = $end->clone->subtract($freq);      my $start = $end->clone->subtract_duration($freq);
   
     # XXX This is helpful, but monthly and billday > 28 == !!!      # XXX This is helpful, but monthly and billday > 28 == !!!
     $end->subtract( days => 1 ) while $day && $end->$day_method != $day;      $end->subtract( days => 1 ) while $day && $end->$day_method != $day;
Line 628 
Line 672 
         ? DateTime->now->subtract( days => $cust->{net} )          ? DateTime->now->subtract( days => $cust->{net} )
         : 0;          : 0;
   
     $cust->{no_invoice} = 1 if $start->clone->add($freq) > $end;      $cust->{no_invoice} = 1 if $start->clone->add_duration($freq) > $end;
     $cust->{billend}    = $end;      $cust->{billend}    = $end;
     $cust->{billstart}  = $start;      $cust->{billstart}  = $start;
 }  }

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.53

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