1 package FS::Cron::bill;
4 use vars qw( @ISA @EXPORT_OK );
7 use DBI 1.33; #The "clone" method was added in DBI 1.33.
9 use FS::Record qw(qsearchs);
12 use FS::part_event_condition;
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw ( bill );
21 my $check_freq = $opt{'check_freq'} || '1d';
24 $debug = 1 if $opt{'v'};
25 $debug = $opt{'l'} if $opt{'l'};
27 $FS::cust_main::DEBUG = $debug;
28 #$FS::cust_event::DEBUG = $opt{'l'} if $opt{'l'};
32 push @search, "( cust_main.archived != 'Y' OR archived IS NULL )"; #disable?
34 push @search, "cust_main.payby = '". $opt{'p'}. "'"
36 push @search, "cust_main.agentnum = ". $opt{'a'}
41 join(' OR ', map "cust_main.custnum = $_", @ARGV ).
46 # generate where_pkg/where_event search clause
49 #we're at now now (and later).
50 my($time)= $opt{'d'} ? str2time($opt{'d'}) : $^T;
51 $time += $opt{'y'} * 86400 if $opt{'y'};
53 my $invoice_time = $opt{'n'} ? $^T : $time;
55 # select * from cust_main where
56 my $where_pkg = <<"END";
57 0 < ( select count(*) from cust_pkg
58 where cust_main.custnum = cust_pkg.custnum
59 and ( cancel is null or cancel = 0 )
60 and ( setup is null or setup = 0
61 or bill is null or bill <= $time
62 or ( expire is not null and expire <= $^T )
63 or ( adjourn is not null and adjourn <= $^T )
68 my $where_event = join(' OR ', map {
71 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
72 my $where = FS::part_event_condition->where_conditions_sql( $eventtable,
77 "0 < ( SELECT COUNT(*) FROM part_event $join
78 WHERE check_freq = '$check_freq'
79 AND eventtable = '$eventtable'
80 AND ( disabled = '' OR disabled IS NULL )
85 if ( $eventtable eq 'cust_main' ) {
88 "0 < ( SELECT COUNT(*) FROM $eventtable
89 WHERE cust_main.custnum = $eventtable.custnum
95 } FS::part_event->eventtables);
97 push @search, "( $where_pkg OR $where_event )";
100 # get a list of custnums
103 warn "searching for customers:\n". join("\n", @search). "\n"
104 if $opt{'v'} || $opt{'l'};
106 my $cursor_dbh = dbh->clone;
109 "DECLARE cron_bill_cursor CURSOR FOR ".
110 " SELECT custnum FROM cust_main WHERE ". join(' AND ', @search)
111 ) or die $cursor_dbh->errstr;
115 my $sth = $cursor_dbh->prepare('FETCH 100 FROM cron_bill_cursor'); #mysql?
117 $sth->execute or die $sth->errstr;
119 my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref };
121 last unless scalar(@custnums);
124 # for each custnum, queue or make one customer object and bill
125 # (one at a time, to reduce memory footprint with large #s of customers)
128 foreach my $custnum ( @custnums ) {
132 'invoice_time' => $invoice_time,
133 'actual_time' => $^T, #when freeside-bill was started
134 #(not, when using -m, freeside-queued)
135 'check_freq' => $check_freq,
136 'resetup' => ( $opt{'s'} ? $opt{'s'} : 0 ),
142 warn "DRY RUN: would add custnum $custnum for queued_bill\n";
144 #add job to queue that calls bill_and_collect with options
145 my $queue = new FS::queue {
146 'job' => 'FS::cust_main::queued_bill',
148 'priority' => 99, #don't get in the way of provisioning jobs
150 my $error = $queue->insert( 'custnum'=>$custnum, %args );
155 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } );
156 $cust_main->bill_and_collect( %args, 'debug' => $debug );
164 $cursor_dbh->commit or die $cursor_dbh->errstr;