selectable choice for arbitrary fields, RT#25623
[freeside.git] / FS / FS / Cron / upload.pm
index c266797..d00037a 100644 (file)
@@ -9,6 +9,7 @@ use FS::Record qw( qsearch qsearchs );
 use FS::Conf;
 use FS::queue;
 use FS::agent;
+use FS::Log;
 use LWP::UserAgent;
 use HTTP::Request;
 use HTTP::Request::Common;
@@ -30,6 +31,8 @@ $me = '[FS::Cron::upload]';
 
 sub upload {
   my %opt = @_;
+  my $log = FS::Log->new('Cron::upload');
+  $log->info('start');
 
   my $debug = 0;
   $debug = 1 if $opt{'v'};
@@ -112,6 +115,32 @@ sub upload {
 
   } #!if cust_bill-ftp_spool
 
+  # if there's nothing to do, don't hold up the rest of the process
+  if (!@tasks) {
+    $log->info('finish (nothing to upload)');
+    return '';
+  }
+
+  # wait for any ongoing billing jobs to complete
+  if ($opt{m}) {
+    my $dbh = dbh;
+    my $sql = "SELECT count(*) FROM queue LEFT JOIN cust_main USING(custnum) ".
+    "WHERE queue.job='FS::cust_main::queued_bill' AND status != 'failed'";
+    if (@agents) {
+      $sql .= ' AND cust_main.agentnum IN('.
+        join(',', map {$_->agentnum} @agents).
+        ')';
+    }
+    my $sth = $dbh->prepare($sql) or die $dbh->errstr;
+    while (1) {
+      $sth->execute()
+        or die "Unexpected error executing statement $sql: ". $sth->errstr;
+      last if $sth->fetchrow_arrayref->[0] == 0;
+      warn "Waiting 5min for billing to complete...\n" if $DEBUG;
+      sleep 300;
+    }
+  }
+
   foreach (@tasks) {
 
     my $agentnum = $_->{agentnum};
@@ -136,17 +165,21 @@ sub upload {
     }
 
   }
+  $log->info('finish');
 
 }
 
 sub spool_upload {
   my %opt = @_;
+  my $log = FS::Log->new('spool_upload');
 
   warn "$me spool_upload called\n" if $DEBUG;
   my $conf = new FS::Conf;
   my $dir = '%%%FREESIDE_EXPORT%%%/export.'. $FS::UID::datasrc. '/cust_bill';
 
-  my $agentnum = $opt{agentnum} or die "no agentnum provided\n";
+  my $agentnum = $opt{agentnum} || '';
+  $log->debug('start', agentnum => $agentnum);
+
   my $url      = $opt{url} or die "no url for agent $agentnum\n";
   $url =~ s/^\s+//; $url =~ s/\s+$//;
 
@@ -166,12 +199,16 @@ sub spool_upload {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
-  my $agent = qsearchs( 'agent', { agentnum => $agentnum } )
-    or die "no such agent: $agentnum";
-  $agent->select_for_update; #mutex 
+  if ( $agentnum ) {
+    my $agent = qsearchs( 'agent', { agentnum => $agentnum } )
+      or die "no such agent: $agentnum";
+    $agent->select_for_update; #mutex 
+  }
 
   if ( $opt{'format'} eq 'billco' ) {
 
+    die "no agentnum provided\n" unless $agentnum;
+
     my $zipfile  = "$dir/agentnum$agentnum-$opt{date}.zip";
 
     unless ( -f "$dir/agentnum$agentnum-header.csv" ||
@@ -179,23 +216,12 @@ sub spool_upload {
     {
       warn "$me neither $dir/agentnum$agentnum-header.csv nor ".
            "$dir/agentnum$agentnum-detail.csv found\n" if $DEBUG;
+      $log->debug("finish (neither agentnum$agentnum-header.csv nor ".
+                  "agentnum$agentnum-detail.csv found)");
       $dbh->commit or die $dbh->errstr if $oldAutoCommit;
       return;
     }
 
-    # a better way?
-    if ($opt{m}) {
-      my $sql = "SELECT count(*) FROM queue LEFT JOIN cust_main USING(custnum) ".
-        "WHERE queue.job='FS::cust_main::queued_bill' AND cust_main.agentnum = ?";
-      my $sth = $dbh->prepare($sql) or die $dbh->errstr;
-      while (1) {
-        $sth->execute( $agentnum )
-          or die "Unexpected error executing statement $sql: ". $sth->errstr;
-        last if $sth->fetchrow_arrayref->[0];
-        sleep 300;
-      }
-    }
-
     foreach ( qw ( header detail ) ) {
       rename "$dir/agentnum$agentnum-$_.csv",
              "$dir/agentnum$agentnum-$opt{date}-$_.csv";
@@ -257,6 +283,7 @@ sub spool_upload {
     my $file = $opt{agentnum} ? "agentnum$opt{agentnum}" : 'spool'; #.csv
     unless ( -f "$dir/$file.csv" ) {
       warn "$me $dir/$file.csv not found\n" if $DEBUG;
+      $log->debug("finish ($dir/$file.csv not found)");
       $dbh->commit or die $dbh->errstr if $oldAutoCommit;
       return;
     }
@@ -283,6 +310,8 @@ sub spool_upload {
       die "malformed FTP URL $url\n";
     }
   } #opt{format}
+  
+  $log->debug('finish', agentnum => $agentnum);
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
   '';