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 )";
102 # get a list of custnums
105 warn "searching for customers:\n".
107 "custnum > $prev_custnum\n"
108 if $opt{'v'} || $opt{'l'};
110 my $sth = dbh->prepare(
111 "SELECT custnum FROM cust_main".
112 " WHERE ". join(' AND ', @search).
113 " AND custnum > $prev_custnum ".
114 " ORDER BY custnum LIMIT 1000 "
115 ) or die dbh->errstr;
117 $sth->execute or die $sth->errstr;
119 my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref };
121 last unless scalar(@custnums);
123 $prev_custnum = $custnums[-1];
126 # for each custnum, queue or make one customer object and bill
127 # (one at a time, to reduce memory footprint with large #s of customers)
130 foreach my $custnum ( @custnums ) {
134 'invoice_time' => $invoice_time,
135 'actual_time' => $^T, #when freeside-bill was started
136 #(not, when using -m, freeside-queued)
137 'check_freq' => $check_freq,
138 'resetup' => ( $opt{'s'} ? $opt{'s'} : 0 ),
143 #add job to queue that calls bill_and_collect with options
144 my $queue = new FS::queue {
145 'job' => 'FS::cust_main::queued_bill',
147 'priority' => 99, #don't get in the way of provisioning jobs
149 my $error = $queue->insert( 'custnum'=>$custnum, %args );
153 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } );
154 $cust_main->bill_and_collect( %args, 'debug' => $debug );