we don't actually need the results ordered, and i'm sure it doesn't help the planner...
[freeside.git] / FS / FS / Cron / bill.pm
index 1a6c4a7..225b372 100644 (file)
@@ -4,7 +4,9 @@ use strict;
 use vars qw( @ISA @EXPORT_OK );
 use Exporter;
 use Date::Parse;
-use FS::Record qw(qsearch qsearchs);
+use DBI 1.33; #The "clone" method was added in DBI 1.33. 
+use FS::UID qw(dbh);
+use FS::Record qw(qsearchs);
 use FS::cust_main;
 use FS::part_event;
 use FS::part_event_condition;
@@ -18,14 +20,32 @@ sub bill {
 
   my $check_freq = $opt{'check_freq'} || '1d';
 
-  $FS::cust_main::DEBUG = 1 if $opt{'v'};
-  $FS::cust_main::DEBUG = $opt{'l'} if $opt{'l'};
+  my $debug = 0;
+  $debug = 1 if $opt{'v'};
+  $debug = $opt{'l'} if $opt{'l'};
+  $FS::cust_main::DEBUG = $debug;
   #$FS::cust_event::DEBUG = $opt{'l'} if $opt{'l'};
-  
-  my %search = ();
-  $search{'payby'}    = $opt{'p'} if $opt{'p'};
-  $search{'agentnum'} = $opt{'a'} if $opt{'a'};
-  
+
+  my @search = ();
+
+  push @search, "( cust_main.archived != 'Y' OR archived IS NULL )"; #disable?
+
+  push @search, "cust_main.payby    = '". $opt{'p'}. "'"
+    if $opt{'p'};
+  push @search, "cust_main.agentnum =  ". $opt{'a'}
+    if $opt{'a'};
+
+  if ( @ARGV ) {
+    push @search, "( ".
+      join(' OR ', map "cust_main.custnum = $_", @ARGV ).
+    " )";
+  }
+
+  ###
+  # generate where_pkg/where_event search clause
+  ###
+
   #we're at now now (and later).
   my($time)= $opt{'d'} ? str2time($opt{'d'}) : $^T;
   $time += $opt{'y'} * 86400 if $opt{'y'};
@@ -74,45 +94,71 @@ END
 
   } FS::part_event->eventtables);
 
-  my $extra_sql = ( scalar(%search) ? ' AND ' : ' WHERE ' ).
-                  "( $where_pkg OR $where_event )";
+  push @search, "( $where_pkg OR $where_event )";
 
-  my @cust_main;
-  if ( @ARGV ) {
-    @cust_main = map { qsearchs('cust_main', { custnum => $_, %search } ) } @ARGV
-  } else {
+  ###
+  # get a list of custnums
+  ###
 
-    warn "searching for customers:\n".
-         join("\n", map "  $_ => ".$search{$_}, keys %search). "\n".
-         "  $extra_sql\n"
-      if $opt{'v'} || $opt{'l'};
+  warn "searching for customers:\n". join("\n", @search). "\n"
+    if $opt{'v'} || $opt{'l'};
 
-    @cust_main = qsearch({
-      'table'     => 'cust_main',
-      'hashref'   => \%search,
-      'extra_sql' => $extra_sql,
-    });
+  my $cursor_dbh = dbh->clone;
 
-  }
-  
-  foreach my $cust_main ( @cust_main ) {
+  $cursor_dbh->do(
+    "DECLARE cron_bill_cursor CURSOR FOR ".
+    "  SELECT custnum FROM cust_main WHERE ". join(' AND ', @search)
+  ) or die $cursor_dbh->errstr;
 
-    if ( $opt{'m'} ) {
+  while ( 1 ) {
 
-      die "XXX multi-process mode not yet completed";
-      #add job to queue that calls bill_and_collect with options
+    my $sth = $cursor_dbh->prepare('FETCH 100 FROM cron_bill_cursor'); #mysql?
 
-    } else {
+    $sth->execute or die $sth->errstr;
+
+    my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref };
+
+    last unless scalar(@custnums);
 
-      $cust_main->bill_and_collect(
-        'time'         => $time,
-        'invoice_time' => $invoice_time,
-        'check_freq'   => $check_freq,
-        'resetup'      => $opt{'s'},
+    ###
+    # for each custnum, queue or make one customer object and bill
+    # (one at a time, to reduce memory footprint with large #s of customers)
+    ###
+    
+    foreach my $custnum ( @custnums ) {
+    
+      my %args = (
+          'time'         => $time,
+          'invoice_time' => $invoice_time,
+          'actual_time'  => $^T, #when freeside-bill was started
+                                 #(not, when using -m, freeside-queued)
+          'check_freq'   => $check_freq,
+          'resetup'      => ( $opt{'s'} ? $opt{'s'} : 0 ),
       );
 
+      if ( $opt{'m'} ) {
+
+        #add job to queue that calls bill_and_collect with options
+        my $queue = new FS::queue {
+          'job'      => 'FS::cust_main::queued_bill',
+          'secure'   => 'Y',
+          'priority' => 99, #don't get in the way of provisioning jobs
+        };
+        my $error = $queue->insert( 'custnum'=>$custnum, %args );
+
+      } else {
+
+        my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } );
+        $cust_main->bill_and_collect( %args, 'debug' => $debug );
+
+      }
+
     }
 
   }
 
+  $cursor_dbh->commit or die $cursor_dbh->errstr;
+
 }
+
+1;