1 package FS::Cron::bill;
4 use vars qw( @ISA @EXPORT_OK );
8 use FS::Record qw(qsearchs);
11 use FS::part_event_condition;
13 @ISA = qw( Exporter );
14 @EXPORT_OK = qw ( bill );
20 my $check_freq = $opt{'check_freq'} || '1d';
23 $debug = 1 if $opt{'v'};
24 $debug = $opt{'l'} if $opt{'l'};
26 $FS::cust_main::DEBUG = $debug;
27 #$FS::cust_event::DEBUG = $opt{'l'} if $opt{'l'};
31 push @search, "( cust_main.archived != 'Y' OR archived IS NULL )"; #disable?
33 push @search, "cust_main.payby = '". $opt{'p'}. "'"
35 push @search, "cust_main.agentnum = ". $opt{'a'}
40 join(' OR ', map "cust_main.custnum = $_", @ARGV ).
45 # generate where_pkg/where_event search clause
48 #we're at now now (and later).
49 my($time)= $opt{'d'} ? str2time($opt{'d'}) : $^T;
50 $time += $opt{'y'} * 86400 if $opt{'y'};
52 my $invoice_time = $opt{'n'} ? $^T : $time;
54 # select * from cust_main where
55 my $where_pkg = <<"END";
56 0 < ( select count(*) from cust_pkg
57 where cust_main.custnum = cust_pkg.custnum
58 and ( cancel is null or cancel = 0 )
59 and ( setup is null or setup = 0
60 or bill is null or bill <= $time
61 or ( expire is not null and expire <= $^T )
62 or ( adjourn is not null and adjourn <= $^T )
67 my $where_event = join(' OR ', map {
70 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
71 my $where = FS::part_event_condition->where_conditions_sql( $eventtable,
76 "0 < ( SELECT COUNT(*) FROM part_event $join
77 WHERE check_freq = '$check_freq'
78 AND eventtable = '$eventtable'
79 AND ( disabled = '' OR disabled IS NULL )
84 if ( $eventtable eq 'cust_main' ) {
87 "0 < ( SELECT COUNT(*) FROM $eventtable
88 WHERE cust_main.custnum = $eventtable.custnum
94 } FS::part_event->eventtables);
96 push @search, "( $where_pkg OR $where_event )";
99 # get a list of custnums
102 warn "searching for customers:\n". join("\n", @search). "\n"
103 if $opt{'v'} || $opt{'l'};
106 "DECLARE cron_bill_cursor CURSOR WITH HOLD FOR ". #no WITH HOLD for mysql?
107 " SELECT custnum FROM cust_main ".
108 " WHERE ". join(' AND ', @search).
109 " ORDER BY custnum " #LIMIT 1000 "
110 ) or die dbh->errstr;
114 my $sth = dbh->prepare('FETCH 1000 FROM cron_bill_cursor'); #mysql?
116 $sth->execute or die $sth->errstr;
118 my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref };
120 last unless scalar(@custnums);
123 # for each custnum, queue or make one customer object and bill
124 # (one at a time, to reduce memory footprint with large #s of customers)
127 foreach my $custnum ( @custnums ) {
131 'invoice_time' => $invoice_time,
132 'actual_time' => $^T, #when freeside-bill was started
133 #(not, when using -m, freeside-queued)
134 'check_freq' => $check_freq,
135 'resetup' => ( $opt{'s'} ? $opt{'s'} : 0 ),
140 #add job to queue that calls bill_and_collect with options
141 my $queue = new FS::queue {
142 'job' => 'FS::cust_main::queued_bill',
144 'priority' => 99, #don't get in the way of provisioning jobs
146 my $error = $queue->insert( 'custnum'=>$custnum, %args );
150 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } );
151 $cust_main->bill_and_collect( %args, 'debug' => $debug );