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 @ISA = qw( Exporter );
13 @EXPORT_OK = qw ( bill );
20 $debug = 1 if $opt{'v'};
21 $debug = $opt{'l'} if $opt{'l'};
23 $FS::cust_main::DEBUG = $debug;
24 #$FS::cust_event::DEBUG = $opt{'l'} if $opt{'l'};
28 push @search, "( cust_main.archived != 'Y' OR archived IS NULL )"; #disable?
30 push @search, "cust_main.payby = '". $opt{'p'}. "'"
32 push @search, "cust_main.agentnum = ". $opt{'a'}
37 join(' OR ', map "cust_main.custnum = $_", @ARGV ).
42 # generate where_pkg / where_bill_event search clause (1.7-style)
45 #we're at now now (and later).
46 my($time)= $opt{'d'} ? str2time($opt{'d'}) : $^T;
47 $time += $opt{'y'} * 86400 if $opt{'y'};
49 my $invoice_time = $opt{'n'} ? $^T : $time;
51 # select * from cust_main where
52 my $where_pkg = <<"END";
53 0 < ( select count(*) from cust_pkg
54 where cust_main.custnum = cust_pkg.custnum
55 and ( cancel is null or cancel = 0 )
56 and ( setup is null or setup = 0
57 or bill is null or bill <= $time
58 or ( expire is not null and expire <= $^T )
59 or ( adjourn is not null and adjourn <= $^T )
65 my $where_bill_event = <<"END";
66 0 < ( select count(*) from cust_bill
67 where cust_main.custnum = cust_bill.custnum
70 ( select sum(amount) from cust_bill_pay
71 where cust_bill.invnum = cust_bill_pay.invnum )
75 ( select sum(amount) from cust_credit_bill
76 where cust_bill.invnum = cust_credit_bill.invnum )
79 and 0 < ( select count(*) from part_bill_event
80 where payby = cust_main.payby
81 and ( disabled is null or disabled = '' )
82 and seconds <= $time - cust_bill._date
83 and 0 = ( select count(*) from cust_bill_event
84 where cust_bill.invnum = cust_bill_event.invnum
85 and part_bill_event.eventpart = cust_bill_event.eventpart
93 push @search, "( $where_pkg OR $where_bill_event )";
96 # get a list of custnums
99 warn "searching for customers:\n". join("\n", @search). "\n"
100 if $opt{'v'} || $opt{'l'};
102 my $cursor_dbh = dbh->clone;
105 "DECLARE cron_bill_cursor CURSOR FOR ".
106 " SELECT custnum FROM cust_main WHERE ". join(' AND ', @search)
107 ) or die $cursor_dbh->errstr;
111 my $sth = $cursor_dbh->prepare('FETCH 1000 FROM cron_bill_cursor'); #mysql?
113 $sth->execute or die $sth->errstr;
115 my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref };
117 last unless scalar(@custnums);
120 # for each custnum, queue or make one customer object and bill
121 # (one at a time, to reduce memory footprint with large #s of customers)
124 foreach my $custnum ( @custnums ) {
128 'invoice_time' => $invoice_time,
129 'actual_time' => $^T, #when freeside-bill was started
130 #(not, when using -m, freeside-queued)
131 'resetup' => ( $opt{'s'} ? $opt{'s'} : 0 ),
136 #add job to queue that calls bill_and_collect with options
137 my $queue = new FS::queue {
138 'job' => 'FS::cust_main::queued_bill',
139 'priority' => 99, #don't get in the way of provisioning jobs
141 my $error = $queue->insert( 'custnum'=>$custnum, %args );
145 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } );
146 $cust_main->bill_and_collect( %args, 'debug' => $debug );
154 $cursor_dbh->commit or die $cursor_dbh->errstr;