[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.43 and 1.44

version 1.43, 2011/12/30 03:30:51 version 1.44, 2011/12/30 05:01:41
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $AFresh1: rt_invoices.pl,v 1.42 2011/12/30 03:20:45 andrew Exp $  # $AFresh1: rt_invoices.pl,v 1.43 2011/12/30 03:30:51 andrew Exp $
 ########################################################################  ########################################################################
 # Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com>  # Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com>
 #  #
Line 85 
Line 85 
   
     next if $cust->{no_invoice};      next if $cust->{no_invoice};
     $cust->{invoice} ||= make_invoice($cust);      $cust->{invoice} ||= make_invoice($cust);
     if ( !$cust->{invoice} ) {  
         $cust->{no_invoice} = 1;  
         say "$cust->{id} has no open invoices [" . $ticket->id . ']';  
         next;  
     }  
   
       die "$cust->{id} has no open invoices [" . $ticket->id . ']'
           unless $cust->{invoice};
   
     say 'Ticket ' . $ticket->id . " belongs to $cust->{id}";      say 'Ticket ' . $ticket->id . " belongs to $cust->{id}";
   
     my $project = make_project( $ticket, $cust );      my $project = make_project( $ticket, $cust );
Line 133 
Line 131 
     push @{ $cust->{invoice}->{projects} }, $project;      push @{ $cust->{invoice}->{projects} }, $project;
 }  }
   
   
   if ( my $unpaid_invoices = $state->unpaid_invoices() ) {
       foreach my $custid ( keys %{$unpaid_invoices} ) {
           my %project = ( title => 'Unpaid Invoices', fees => [], );
           my $past_due = 0;
   
           my $cust;
           foreach ( @{$customers} ) {
               if ( $_->{id} eq $custid ) {
                   $cust = $_;
                   last;
               }
           }
           $cust ||= fake_customer($custid);
   
           foreach my $id ( sort { $a <=> $b }
               keys %{ $unpaid_invoices->{$custid} } )
           {
               my $unpaid  = $state->get_invoice($id);
               my $invdate = ymd_to_DateTime( $unpaid->{invdate} );
   
               next
                   if $cust->{duedate}
                       && DateTime->compare( $invdate, $cust->{duedate} ) > 0;
   
               $past_due += $unpaid_invoices->{$custid}->{$id};
               push @{ $project{fees} },
                   {
                   id => $id,
                   contents =>
                       sprintf( "Invoice %06d from %s", $id, $invdate->ymd ),
                   count => 1,
                   rate  => $unpaid_invoices->{$custid}->{$id},
                   };
           }
   
           if ($past_due) {
               $cust->{invoice} ||= make_invoice();
   
               $cust->{invoice}->{past_due} = $past_due;
               $cust->{invoice}->{total_due}
                   = $cust->{invoice}->{total} + $past_due;
   
               unshift @{ $cust->{invoice}->{projects} }, \%project;
           }
       }
   }
   
 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 172 
Line 218 
         $invoice->{total} -= round( $invoice->{discount}{amount} );          $invoice->{total} -= round( $invoice->{discount}{amount} );
     }      }
   
     if ( my $unpaid_invoices = $state->unpaid_invoices( $cust->{id} ) ) {  
         my %project = ( title => 'Unpaid Invoices', fees => [], );  
         my $past_due = 0;  
   
         foreach my $id ( sort { $a <=> $b } keys %{$unpaid_invoices} ) {  
             my $unpaid  = $state->get_invoice($id);  
             my $invdate = ymd_to_DateTime( $unpaid->{invdate} );  
   
             next  
                 if $cust->{duedate}  
                     && DateTime->compare( $invdate, $cust->{duedate} ) > 0;  
   
             $past_due += $unpaid_invoices->{$id};  
             push @{ $project{fees} },  
                 {  
                 id => $id,  
                 contents =>  
                     sprintf( "Invoice %06d from %s", $id, $invdate->ymd ),  
                 count => 1,  
                 rate  => $unpaid_invoices->{$id},  
                 };  
         }  
   
         if ($past_due) {  
             $invoice->{past_due}  = $past_due;  
             $invoice->{total_due} = $invoice->{total} + $invoice->{past_due};  
   
             unshift @{ $invoice->{projects} }, \%project;  
         }  
     }  
   
     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 271 
Line 286 
         }          }
     }      }
   
     # Fake customer if we didn't find one      return fake_customer($ticket->requestors);
     my $cust = $config->new_customer;  }
   
     ( $cust->{id} ) = $ticket->requestors;  sub fake_customer {
     return unless $cust->{id};      my ($custid) = @_;
       return unless $custid;
   
       my $cust = $config->new_customer;
     push @{$customers}, $cust;      push @{$customers}, $cust;
   
       $cust->{id} = $custid;
     $cust->{match} = [      $cust->{match} = [
         {   type  => 'requestors',          {   type  => 'requestors',
             regex => $cust->{id},              regex => $cust->{id},
Line 324 
Line 343 
 sub make_invoice {  sub make_invoice {
     my ($cust) = @_;      my ($cust) = @_;
   
     return if $cust->{no_invoice};  
     return unless $cust->{billstart};  
   
     my %invoice      my %invoice
         = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) );          = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) );
     $invoice{start} = $cust->{startinvoicedate}->clone      $invoice{start} = $cust->{startinvoicedate}->clone
         if $cust->{startinvoicedate};          if $cust->{startinvoicedate};
   
     return if $invoice{start} && $invoice{start} > $invoice{end};  
   
     if ( $cust->{base_rate} ) {      if ( $cust->{base_rate} ) {
         my ( $project, $hours ) = make_base_project($cust);          my ( $project, $hours ) = make_base_project($cust);

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.44

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