7 use FS::UID qw(adminsuidsetup driver_name dbh datasrc);
8 use FS::Record qw(qsearch qsearchs dbdef);
12 &untaint_argv; #what it sounds like (eww)
13 use vars qw($opt_d $opt_v $opt_p $opt_a $opt_s $opt_y);
14 getopts("p:a:d:vsy:");
15 my $user = shift or die &usage;
19 $FS::cust_main::DEBUG = 1 if $opt_v;
22 $search{'payby'} = $opt_p if $opt_p;
23 $search{'agentnum'} = $opt_a if $opt_a;
25 #we're at now now (and later).
26 my($time)= $opt_d ? str2time($opt_d) : $^T;
27 $time += $opt_y * 86400 if $opt_y;
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 )
42 my $where_bill_event = <<"END";
43 0 < ( select count(*) from cust_bill
44 where cust_main.custnum = cust_bill.custnum
47 ( select sum(amount) from cust_bill_pay
48 where cust_bill.invnum = cust_bill_pay.invnum )
52 ( select sum(amount) from cust_credit_bill
53 where cust_bill.invnum = cust_credit_bill.invnum )
56 and 0 < ( select count(*) from part_bill_event
57 where payby = cust_main.payby
58 and ( disabled is null or disabled = '' )
59 and seconds <= $time - cust_bill._date
60 and 0 = ( select count(*) from cust_bill_event
61 where cust_bill.invnum = cust_bill_event.invnum
62 and part_bill_event.eventpart = cust_bill_event.eventpart
70 my $extra_sql = ( scalar(%search) ? ' AND ' : ' WHERE ' ). "( $where_pkg OR $where_bill_event )";
74 @cust_main = map { qsearchs('cust_main', { custnum => $_, %search } ) } @ARGV
76 @cust_main = qsearch('cust_main', \%search, '', $extra_sql );
81 foreach $cust_main ( @cust_main ) {
83 # $^T not $time because -d is for pre-printing invoices
84 foreach my $cust_pkg (
85 grep { $_->expire && $_->expire <= $^T } $cust_main->ncancelled_pkgs
87 my $error = $cust_pkg->cancel;
88 warn "Error cancelling expired pkg ". $cust_pkg->pkgnum. " for custnum ".
89 $cust_main->custnum. ": $error"
93 my $error = $cust_main->bill( 'time' => $time,
94 'resetup' => $opt_s, );
95 warn "Error billing, custnum ". $cust_main->custnum. ": $error" if $error;
97 $cust_main->apply_payments;
98 $cust_main->apply_credits;
100 $error = $cust_main->collect( 'invoice_time' => $time );
101 warn "Error collecting, custnum". $cust_main->custnum. ": $error" if $error;
105 if ( driver_name eq 'Pg' ) {
106 dbh->{AutoCommit} = 1; #so we can vacuum
107 foreach my $table ( dbdef->tables ) {
108 my $sth = dbh->prepare("VACUUM ANALYZE $table") or die dbh->errstr;
109 $sth->execute or die $sth->errstr;
113 my $conf = new FS::Conf;
114 my $dest = $conf->config('dump-scpdest');
116 datasrc =~ /dbname=([\w\.]+)$/ or die "unparsable datasrc ". datasrc;
118 eval "use Net::SCP qw(scp);";
119 if ( driver_name eq 'Pg' ) {
120 system("pg_dump $database >/var/tmp/$database.sql")
122 die "database dumps not yet supported for ". driver_name;
124 if ( $conf->config('dump-pgpid') ) {
127 $gpg->encrypt( plaintext => "/var/tmp/$database.sql",
128 output => "/var/tmp/$database.gpg",
129 recipient => $conf->config('dump-pgpid'),
131 chmod 0600, '/var/tmp/$database.gpg';
132 scp("/var/tmp/$database.gpg", $dest);
133 unlink "/var/tmp/$database.gpg" or die $!;
135 chmod 0600, '/var/tmp/$database.sql';
136 scp("/var/tmp/$database.sql", $dest);
138 unlink "/var/tmp/$database.sql" or die $!;
144 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
145 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
147 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
153 die "Usage:\n\n freeside-daily [ -d 'date' ] user [ custnum custnum ... ]\n";
158 freeside-daily - Run daily billing and invoice collection events.
162 freeside-daily [ -d 'date' ] [ -y days ] [ -p 'payby' ] [ -a agentnum ] [ -s ] [ -v ] user [ custnum custnum ... ]
166 Bills customers and runs invoice collection events. Should be run from
169 This script replaces freeside-bill from 1.3.1.
171 Bills customers. Searches for customers who are due for billing and calls
172 the bill and collect methods of a cust_main object. See L<FS::cust_main>.
174 -d: Pretend it's 'date'. Date is in any format Date::Parse is happy with,
177 -y: In addition to -d, which specifies an absolute date, the -y switch
178 specifies an offset, in days. For example, "-y 15" would increment the
179 "pretend date" 15 days from whatever was specified by the -d switch
180 (or now, if no -d switch was given).
182 -p: Only process customers with the specified payby (I<CARD>, I<DCRD>, I<CHEK>, I<DCHK>, I<BILL>, I<COMP>, I<LECB>)
184 -a: Only process customers with the specified agentnum
186 -s: re-charge setup fees
190 user: From the mapsecrets file - see config.html from the base documentation
192 custnum: if one or more customer numbers are specified, only bills those
193 customers. Otherwise, bills all customers.
199 L<FS::cust_main>, config.html from the base documentation