X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FCron%2Fbill.pm;h=5101e0dad124d92498c80c9878bd2689b180de9c;hb=9f9bf2008844e95b0d93c73626ea134b7f97a2f8;hp=7de2ff2f6c6f6a72252c20a4dcdd2851e62c82e0;hpb=5e05724a635a22776f1b973f5d7e77989da4e048;p=freeside.git diff --git a/FS/FS/Cron/bill.pm b/FS/FS/Cron/bill.pm index 7de2ff2f6..5101e0dad 100644 --- a/FS/FS/Cron/bill.pm +++ b/FS/FS/Cron/bill.pm @@ -4,6 +4,7 @@ use strict; use vars qw( @ISA @EXPORT_OK ); use Exporter; use Date::Parse; +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; @@ -28,6 +29,8 @@ sub bill { 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'} @@ -100,51 +103,64 @@ END warn "searching for customers:\n". join("\n", @search). "\n" if $opt{'v'} || $opt{'l'}; - my $sth = dbh->prepare( - "SELECT custnum FROM cust_main". - " WHERE ". join(' AND ', @search) - ) or die dbh->errstr; + my $cursor_dbh = dbh->clone; - $sth->execute or die $sth->errstr; + $cursor_dbh->do( + "DECLARE cron_bill_cursor CURSOR FOR ". + " SELECT custnum FROM cust_main ". + " WHERE ". join(' AND ', @search). + " ORDER BY custnum " #LIMIT 1000 " + ) or die $cursor_dbh->errstr; - my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref }; + while ( 1 ) { - ### - # 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 $sth = $cursor_dbh->prepare('FETCH 100 FROM cron_bill_cursor'); #mysql? + + $sth->execute or die $sth->errstr; - if ( $opt{'m'} ) { + my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref }; + + last unless scalar(@custnums); + + ### + # 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 ), + ); - #add job to queue that calls bill_and_collect with options + 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', + '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, - 'time' => $time, - 'invoice_time' => $invoice_time, - 'check_freq' => $check_freq, - 'resetup' => $opt{'s'} ? $opt{'s'} : 0, - ); + my $error = $queue->insert( 'custnum'=>$custnum, %args ); - } else { + } else { - my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } ); + my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } ); + $cust_main->bill_and_collect( %args, 'debug' => $debug ); - $cust_main->bill_and_collect( - 'time' => $time, - 'invoice_time' => $invoice_time, - 'check_freq' => $check_freq, - 'resetup' => $opt{'s'}, - 'debug' => $debug, - ); + } } } + $cursor_dbh->commit or die $cursor_dbh->errstr; + } + +1;