7 use FS::UID qw(adminsuidsetup driver_name dbh datasrc);
8 use FS::Record qw(qsearch qsearchs);
12 &untaint_argv; #what it sounds like (eww)
13 use vars qw($opt_d $opt_v $opt_p $opt_s);
15 my $user = shift or die &usage;
19 $FS::cust_main::Debug = 1 if $opt_v;
22 $search{'payby'} = $opt_p if $opt_p;
25 ? map { qsearchs('cust_main', { custnum => $_, %search } ) } @ARGV
26 : qsearch('cust_main', \%search )
29 #we're at now now (and later).
30 my($time)= $opt_d ? str2time($opt_d) : $^T;
33 foreach $cust_main ( @cust_main ) {
35 # $^T not $time because -d is for pre-printing invoices
36 foreach my $cust_pkg (
37 grep { $_->expire && $_->expire <= $^T } $cust_main->ncancelled_pkgs
39 my $error = $cust_pkg->cancel;
40 warn "Error cancelling expired pkg ". $cust_pkg->pkgnum. " for custnum ".
41 $cust_main->custnum. ": $error"
45 my $error = $cust_main->bill( 'time' => $time,
46 'resetup' => $opt_s, );
47 warn "Error billing, custnum ". $cust_main->custnum. ": $error" if $error;
49 $cust_main->apply_payments;
50 $cust_main->apply_credits;
52 $error = $cust_main->collect( 'invoice_time' => $time );
53 warn "Error collecting, custnum". $cust_main->custnum. ": $error" if $error;
57 if ( driver_name eq 'Pg' ) {
58 dbh->{AutoCommit} = 1; #so we can vacuum
59 foreach my $statement ( 'vacuum', 'vacuum analyze' ) {
60 my $sth = dbh->prepare($statement) or die dbh->errstr;
61 $sth->execute or die $sth->errstr;
66 my $conf = new FS::Conf;
67 my $dest = $conf->config('dump-scpdest');
69 datasrc =~ /dbname=([\w\.]+)$/ or die "unparsable datasrc ". datasrc;
71 eval "use Net::SCP qw(scp);";
72 if ( driver_name eq 'Pg' ) {
73 system("pg_dump $database >/var/tmp/$database.sql")
75 die "database dumps not yet supported for ". driver_name;
77 scp("/var/tmp/$database.sql", $dest);
78 unlink "/var/tmp/$database.sql" or die $!;
84 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
85 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
87 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
93 die "Usage:\n\n freeside-daily [ -d 'date' ] user [ custnum custnum ... ]\n";
98 freeside-daily - Run daily billing and invoice collection events.
102 freeside-daily [ -d 'date' ] [ -p 'payby' ] [ -s ] [ -v ] user [ custnum custnum ... ]
106 Bills customers and runs invoice collection events. Should be run from
109 This script replaces freeside-bill from 1.3.1.
111 Bills customers. Searches for customers who are due for billing and calls
112 the bill and collect methods of a cust_main object. See L<FS::cust_main>.
114 -d: Pretend it's 'date'. Date is in any format Date::Parse is happy with,
117 -p: Only process customers with the specified payby (I<CARD>, I<CHEK>, I<BILL>, I<COMP>, I<LECB>)
119 -s: re-charge setup fees
123 user: From the mapsecrets file - see config.html from the base documentation
125 custnum: if one or more customer numbers are specified, only bills those
126 customers. Otherwise, bills all customers.
132 L<FS::cust_main>, config.html from the base documentation