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.
8 use FS::UID qw( dbh driver_name );
9 use FS::Record qw( qsearch qsearchs );
10 use FS::Misc::DateTime qw( day_end );
14 use FS::part_event_condition;
18 @ISA = qw( Exporter );
19 @EXPORT_OK = qw ( bill bill_where );
22 # -s: re-charge setup fees
23 # -v: enable debugging
25 # -m: Experimental multi-process mode uses the job queue for multi-process and/or multi-machine billing.
26 # -r: Multi-process mode dry run option
27 # -g: Don't bill these pkgparts
32 my $log = FS::Log->new('Cron::bill');
35 my $check_freq = $opt{'check_freq'} || '1d';
38 $debug = 1 if $opt{'v'};
39 $debug = $opt{'l'} if $opt{'l'};
40 $FS::cust_main::DEBUG = $debug;
41 #$FS::cust_event::DEBUG = $opt{'l'} if $opt{'l'};
43 my $conf = new FS::Conf;
45 if ( $conf->exists('disable_cron_billing') ) {
46 warn "disable_cron_billing set, skipping billing\n" if $debug;
50 #we're at now now (and later).
51 $opt{'time'} = $opt{'d'} ? str2time($opt{'d'}) : $^T;
52 $opt{'time'} += $opt{'y'} * 86400 if $opt{'y'};
54 $opt{'invoice_time'} = $opt{'n'} ? $^T : $opt{'time'};
56 #hashref here doesn't work with -m
57 #my $not_pkgpart = $opt{g} ? { map { $_=>1 } split(/,\s*/, $opt{g}) }
61 # get a list of custnums
64 my $cursor_dbh = dbh->clone;
66 my $select = 'SELECT custnum FROM cust_main WHERE '. bill_where( %opt );
68 unless ( driver_name =~ /^mysql/ ) {
69 $cursor_dbh->do( "DECLARE cron_bill_cursor CURSOR FOR $select" )
70 or die $cursor_dbh->errstr;
75 my $sql = (driver_name =~ /^mysql/)
77 : 'FETCH 100 FROM cron_bill_cursor';
79 my $sth = $cursor_dbh->prepare($sql);
81 $sth->execute or die $sth->errstr;
83 my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref };
85 last unless scalar(@custnums);
88 # for each custnum, queue or make one customer object and bill
89 # (one at a time, to reduce memory footprint with large #s of customers)
92 foreach my $custnum ( @custnums ) {
95 'time' => $opt{'time'},
96 'invoice_time' => $opt{'invoice_time'},
97 'actual_time' => $^T, #when freeside-bill was started
98 #(not, when using -m, freeside-queued)
99 'check_freq' => $check_freq,
100 'resetup' => ( $opt{'s'} ? $opt{'s'} : 0 ),
101 'not_pkgpart' => $opt{'g'}, #$not_pkgpart,
102 'one_recur' => $opt{'o'},
108 warn "DRY RUN: would add custnum $custnum for queued_bill\n";
111 #avoid queuing another job if there's one still waiting to run
112 next if qsearch( 'queue', { 'job' => 'FS::cust_main::queued_bill',
113 'custnum' => $custnum,
118 #add job to queue that calls bill_and_collect with options
119 my $queue = new FS::queue {
120 'job' => 'FS::cust_main::queued_bill',
122 'priority' => 99, #don't get in the way of provisioning jobs
124 my $error = $queue->insert( 'custnum'=>$custnum, %args );
130 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } );
131 if ( $disable_bill ) {
132 $cust_main->collect( %args, 'debug' => $debug );
134 $cust_main->bill_and_collect( %args, 'debug' => $debug );
141 last if driver_name =~ /^mysql/;
145 $cursor_dbh->commit or die $cursor_dbh->errstr;
147 $log->info('finish');
150 # freeside-daily %opt:
151 # -d: Pretend it's 'date'. Date is in any format Date::Parse is happy with,
154 # -y: In addition to -d, which specifies an absolute date, the -y switch
155 # specifies an offset, in days. For example, "-y 15" would increment the
156 # "pretend date" 15 days from whatever was specified by the -d switch
157 # (or now, if no -d switch was given).
159 # -n: When used with "-d" and/or "-y", specifies that invoices should be dated
160 # with today's date, regardless of the pretend date used to pre-generate
163 # -p: Only process customers with the specified payby (I<CARD>, I<DCRD>, I<CHEK>, I<DCHK>, I<BILL>, I<COMP>, I<LECB>)
165 # -a: Only process customers with the specified agentnum
167 # -v: enable debugging
169 # -l: debugging level
174 my $time = $opt{'time'};
175 my $invoice_time = $opt{'invoice_time'};
177 my $check_freq = $opt{'check_freq'} || '1d';
181 push @search, "( cust_main.archived != 'Y' OR archived IS NULL )"; #disable?
183 push @search, "cust_main.payby = '". $opt{'p'}. "'"
185 push @search, "cust_main.agentnum IN ( ". $opt{'a'}. " ) "
188 #it would be useful if i recognized $opt{g} / $not_pkgpart...
192 join(' OR ', map "cust_main.custnum = $_", @ARGV ).
197 # generate where_pkg/where_event search clause
200 my $conf = new FS::Conf;
201 my $billtime = $conf->exists('next-bill-ignore-time') ? day_end($time) : $time;
203 # select * from cust_main where
204 my $where_pkg = <<"END";
206 SELECT 1 FROM cust_pkg LEFT JOIN part_pkg USING ( pkgpart )
207 WHERE cust_main.custnum = cust_pkg.custnum
208 AND ( cancel IS NULL OR cancel = 0 )
209 AND ( ( ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 )
210 AND ( start_date IS NULL OR start_date = 0
211 OR ( start_date IS NOT NULL AND start_date <= $^T )
214 OR ( freq != '0' AND ( bill IS NULL OR bill <= $billtime ) )
215 OR ( expire IS NOT NULL AND expire <= $^T )
216 OR ( adjourn IS NOT NULL AND adjourn <= $^T )
217 OR ( resume IS NOT NULL AND resume <= $^T )
222 #some false laziness w/cust_main::Billing due_cust_event
223 my $where_event = join(' OR ', map {
226 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
227 my $where = FS::part_event_condition->where_conditions_sql( $eventtable,
230 $where = $where ? "AND $where" : '';
233 "EXISTS ( SELECT 1 FROM part_event $join
234 WHERE check_freq = '$check_freq'
235 AND eventtable = '$eventtable'
236 AND ( disabled = '' OR disabled IS NULL )
241 if ( $eventtable eq 'cust_main' ) {
244 "EXISTS ( SELECT 1 FROM $eventtable
245 WHERE cust_main.custnum = $eventtable.custnum
251 } FS::part_event->eventtables);
253 push @search, "( $where_pkg OR $where_event )";
255 warn "searching for customers:\n". join("\n", @search). "\n"
256 if $opt{'v'} || $opt{'l'};
258 join(' AND ', @search);