1 package FS::Cron::bill;
4 use vars qw( @ISA @EXPORT_OK );
7 use FS::Record qw(qsearch qsearchs);
10 use FS::part_event_condition;
12 @ISA = qw( Exporter );
13 @EXPORT_OK = qw ( bill );
19 my $check_freq = $opt{'check_freq'} || '1d';
21 $FS::cust_main::DEBUG = 1 if $opt{'v'};
22 $FS::cust_main::DEBUG = $opt{'l'} if $opt{'l'};
23 #$FS::cust_event::DEBUG = $opt{'l'} if $opt{'l'};
26 $search{'payby'} = $opt{'p'} if $opt{'p'};
27 $search{'agentnum'} = $opt{'a'} if $opt{'a'};
29 #we're at now now (and later).
30 my($time)= $opt{'d'} ? str2time($opt{'d'}) : $^T;
31 $time += $opt{'y'} * 86400 if $opt{'y'};
33 my $invoice_time = $opt{'n'} ? $^T : $time;
35 # select * from cust_main where
36 my $where_pkg = <<"END";
37 0 < ( select count(*) from cust_pkg
38 where cust_main.custnum = cust_pkg.custnum
39 and ( cancel is null or cancel = 0 )
40 and ( setup is null or setup = 0
41 or bill is null or bill <= $time
42 or ( expire is not null and expire <= $^T )
43 or ( adjourn is not null and adjourn <= $^T )
48 my $where_event = join(' OR ', map {
51 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
52 my $where = FS::part_event_condition->where_conditions_sql( $eventtable,
57 "0 < ( SELECT COUNT(*) FROM part_event $join
58 WHERE check_freq = '$check_freq'
59 AND eventtable = '$eventtable'
60 AND ( disabled = '' OR disabled IS NULL )
65 if ( $eventtable eq 'cust_main' ) {
68 "0 < ( SELECT COUNT(*) FROM $eventtable
69 WHERE cust_main.custnum = $eventtable.custnum
75 } FS::part_event->eventtables);
77 my $extra_sql = ( scalar(%search) ? ' AND ' : ' WHERE ' ).
78 "( $where_pkg OR $where_event )";
82 @cust_main = map { qsearchs('cust_main', { custnum => $_, %search } ) } @ARGV
85 warn "searching for customers:\n".
86 join("\n", map " $_ => ".$search{$_}, keys %search). "\n".
88 if $opt{'v'} || $opt{'l'};
90 @cust_main = qsearch({
91 'table' => 'cust_main',
92 'hashref' => \%search,
93 'extra_sql' => $extra_sql,
98 foreach my $cust_main ( @cust_main ) {
102 die "XXX multi-process mode not yet completed";
103 #add job to queue that calls bill_and_collect with options
107 $cust_main->bill_and_collect(
109 'invoice_time' => $invoice_time,
110 'check_freq' => $check_freq,
111 'resetup' => $opt{'s'},