1 package FS::Cron::bill;
4 use vars qw( @ISA @EXPORT_OK );
7 use FS::Record qw(qsearch qsearchs);
10 @ISA = qw( Exporter );
11 @EXPORT_OK = qw ( bill );
17 $FS::cust_main::DEBUG = 1 if $opt{'v'};
20 $search{'payby'} = $opt{'p'} if $opt{'p'};
21 $search{'agentnum'} = $opt{'a'} if $opt{'a'};
23 #we're at now now (and later).
24 my($time)= $opt{'d'} ? str2time($opt{'d'}) : $^T;
25 $time += $opt{'y'} * 86400 if $opt{'y'};
27 my $invoice_time = $opt{'n'} ? $^T : $time;
29 # select * from cust_main where
30 my $where_pkg = <<"END";
31 0 < ( select count(*) from cust_pkg
32 where cust_main.custnum = cust_pkg.custnum
33 and ( cancel is null or cancel = 0 )
34 and ( setup is null or setup = 0
35 or bill is null or bill <= $time
36 or ( expire is not null and expire <= $^T )
37 or ( adjourn is not null and adjourn <= $^T )
43 my $where_bill_event = <<"END";
44 0 < ( select count(*) from cust_bill
45 where cust_main.custnum = cust_bill.custnum
48 ( select sum(amount) from cust_bill_pay
49 where cust_bill.invnum = cust_bill_pay.invnum )
53 ( select sum(amount) from cust_credit_bill
54 where cust_bill.invnum = cust_credit_bill.invnum )
57 and 0 < ( select count(*) from part_bill_event
58 where payby = cust_main.payby
59 and ( disabled is null or disabled = '' )
60 and seconds <= $time - cust_bill._date
61 and 0 = ( select count(*) from cust_bill_event
62 where cust_bill.invnum = cust_bill_event.invnum
63 and part_bill_event.eventpart = cust_bill_event.eventpart
71 my $extra_sql = ( scalar(%search) ? ' AND ' : ' WHERE ' ). "( $where_pkg OR $where_bill_event )";
75 @cust_main = map { qsearchs('cust_main', { custnum => $_, %search } ) } @ARGV
77 @cust_main = qsearch('cust_main', \%search, '', $extra_sql );
82 foreach $cust_main ( @cust_main ) {
84 my $custnum = $cust_main->custnum;
86 # $^T not $time because -d is for pre-printing invoices
87 foreach my $cust_pkg (
88 grep { $_->expire && $_->expire <= $^T } $cust_main->ncancelled_pkgs
90 my $error = $cust_pkg->cancel;
91 warn "Error cancelling expired pkg ". $cust_pkg->pkgnum.
92 " for custnum $custnum: $error"
95 # $^T not $time because -d is for pre-printing invoices
96 foreach my $cust_pkg (
97 grep { ( $_->part_pkg->is_prepaid && $_->bill && $_->bill < $^T
98 || $_->adjourn && $_->adjourn <= $^T
102 $cust_main->ncancelled_pkgs
104 my $error = $cust_pkg->suspend;
105 warn "Error suspending package ". $cust_pkg->pkgnum.
106 " for custnum $custnum: $error"
110 my $error = $cust_main->bill( 'time' => $time,
111 'invoice_time' => $invoice_time,
112 'resetup' => $opt{'s'},
114 warn "Error billing, custnum $custnum: $error" if $error;
116 $error = $cust_main->apply_payments_and_credits;
117 warn "Error applying payments and credits, custnum $custnum: $error"
120 $error = $cust_main->collect( 'invoice_time' => $time,
121 'freq' => $opt{'freq'},
123 warn "Error collecting, custnum $custnum: $error" if $error;