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( qsearch qsearchs );
13 use FS::part_event_condition;
15 @ISA = qw( Exporter );
16 @EXPORT_OK = qw ( bill bill_where );
19 # -s: re-charge setup fees
20 # -v: enable debugging
22 # -m: Experimental multi-process mode uses the job queue for multi-process and/or multi-machine billing.
23 # -r: Multi-process mode dry run option
24 # -g: Don't bill these pkgparts
29 my $check_freq = $opt{'check_freq'} || '1d';
32 $debug = 1 if $opt{'v'};
33 $debug = $opt{'l'} if $opt{'l'};
34 $FS::cust_main::DEBUG = $debug;
35 #$FS::cust_event::DEBUG = $opt{'l'} if $opt{'l'};
37 my $conf = new FS::Conf;
38 if ( $conf->exists('disable_cron_billing') ) {
39 warn "disable_cron_billing set, skipping billing\n" if $debug;
43 #we're at now now (and later).
44 $opt{'time'} = $opt{'d'} ? str2time($opt{'d'}) : $^T;
45 $opt{'time'} += $opt{'y'} * 86400 if $opt{'y'};
47 $opt{'invoice_time'} = $opt{'n'} ? $^T : $opt{'time'};
49 #hashref here doesn't work with -m
50 #my $not_pkgpart = $opt{g} ? { map { $_=>1 } split(/,\s*/, $opt{g}) }
54 # get a list of custnums
57 my $cursor_dbh = dbh->clone;
60 "DECLARE cron_bill_cursor CURSOR FOR ".
61 " SELECT custnum FROM cust_main WHERE ". bill_where( %opt )
62 ) or die $cursor_dbh->errstr;
66 my $sth = $cursor_dbh->prepare('FETCH 100 FROM cron_bill_cursor'); #mysql?
68 $sth->execute or die $sth->errstr;
70 my @custnums = map { $_->[0] } @{ $sth->fetchall_arrayref };
72 last unless scalar(@custnums);
75 # for each custnum, queue or make one customer object and bill
76 # (one at a time, to reduce memory footprint with large #s of customers)
79 foreach my $custnum ( @custnums ) {
82 'time' => $opt{'time'},
83 'invoice_time' => $opt{'invoice_time'},
84 'actual_time' => $^T, #when freeside-bill was started
85 #(not, when using -m, freeside-queued)
86 'check_freq' => $check_freq,
87 'resetup' => ( $opt{'s'} ? $opt{'s'} : 0 ),
88 'not_pkgpart' => $opt{'g'}, #$not_pkgpart,
94 warn "DRY RUN: would add custnum $custnum for queued_bill\n";
97 #avoid queuing another job if there's one still waiting to run
98 next if qsearch( 'queue', { 'job' => 'FS::cust_main::queued_bill',
99 'custnum' => $custnum,
104 #add job to queue that calls bill_and_collect with options
105 my $queue = new FS::queue {
106 'job' => 'FS::cust_main::queued_bill',
108 'priority' => 99, #don't get in the way of provisioning jobs
110 my $error = $queue->insert( 'custnum'=>$custnum, %args );
116 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $custnum } );
117 $cust_main->bill_and_collect( %args, 'debug' => $debug );
125 $cursor_dbh->commit or die $cursor_dbh->errstr;
129 # freeside-daily %opt:
130 # -d: Pretend it's 'date'. Date is in any format Date::Parse is happy with,
133 # -y: In addition to -d, which specifies an absolute date, the -y switch
134 # specifies an offset, in days. For example, "-y 15" would increment the
135 # "pretend date" 15 days from whatever was specified by the -d switch
136 # (or now, if no -d switch was given).
138 # -n: When used with "-d" and/or "-y", specifies that invoices should be dated
139 # with today's date, irregardless of the pretend date used to pre-generate
142 # -p: Only process customers with the specified payby (I<CARD>, I<DCRD>, I<CHEK>, I<DCHK>, I<BILL>, I<COMP>, I<LECB>)
144 # -a: Only process customers with the specified agentnum
146 # -v: enable debugging
148 # -l: debugging level
153 my $time = $opt{'time'};
154 my $invoice_time = $opt{'invoice_time'};
156 my $check_freq = $opt{'check_freq'} || '1d';
160 push @search, "( cust_main.archived != 'Y' OR archived IS NULL )"; #disable?
162 push @search, "cust_main.payby = '". $opt{'p'}. "'"
164 push @search, "cust_main.agentnum IN ( ". $opt{'a'}. " ) "
167 #it would be useful if i recognized $opt{g} / $not_pkgpart...
171 join(' OR ', map "cust_main.custnum = $_", @ARGV ).
176 # generate where_pkg/where_event search clause
179 # select * from cust_main where
180 my $where_pkg = <<"END";
182 SELECT 1 FROM cust_pkg
183 WHERE cust_main.custnum = cust_pkg.custnum
184 AND ( cancel IS NULL OR cancel = 0 )
185 AND ( ( ( setup IS NULL OR setup = 0 )
186 AND ( start_date IS NULL OR start_date = 0
187 OR ( start_date IS NOT NULL AND start_date <= $^T )
190 OR bill IS NULL OR bill <= $time
191 OR ( expire IS NOT NULL AND expire <= $^T )
192 OR ( adjourn IS NOT NULL AND adjourn <= $^T )
197 my $where_event = join(' OR ', map {
200 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
201 my $where = FS::part_event_condition->where_conditions_sql( $eventtable,
204 $where = $where ? "AND $where" : '';
207 "EXISTS ( SELECT 1 FROM part_event $join
208 WHERE check_freq = '$check_freq'
209 AND eventtable = '$eventtable'
210 AND ( disabled = '' OR disabled IS NULL )
215 if ( $eventtable eq 'cust_main' ) {
218 "EXISTS ( SELECT 1 FROM $eventtable
219 WHERE cust_main.custnum = $eventtable.custnum
225 } FS::part_event->eventtables);
227 push @search, "( $where_pkg OR $where_event )";
229 warn "searching for customers:\n". join("\n", @search). "\n"
230 if $opt{'v'} || $opt{'l'};
232 join(' AND ', @search);