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);
15 my $user = shift or die &usage;
19 $FS::cust_main::Debug = 1 if $opt_v;
22 ? map { qsearchs('cust_main', { custnum => $_ } ) } @ARGV
23 : qsearch('cust_main', {} )
26 #we're at now now (and later).
27 my($time)= $opt_d ? str2time($opt_d) : $^T;
30 foreach $cust_main ( @cust_main ) {
32 # $^T not $time because -d is for pre-printing invoices
33 foreach my $cust_pkg (
34 grep { $_->expire && $_->expire <= $^T } $cust_main->ncancelled_pkgs
36 my $error = $cust_pkg->cancel;
37 warn "Error cancelling expired pkg ". $cust_pkg->pkgnum. " for custnum ".
38 $cust_main->custnum. ": $error"
42 my $error = $cust_main->bill( 'time' => $time );
43 warn "Error billing, custnum ". $cust_main->custnum. ": $error" if $error;
45 $cust_main->apply_payments;
46 $cust_main->apply_credits;
48 $error = $cust_main->collect( 'invoice_time' => $time );
49 warn "Error collecting, custnum". $cust_main->custnum. ": $error" if $error;
53 if ( driver_name eq 'Pg' ) {
54 dbh->{AutoCommit} = 1; #so we can vacuum
55 foreach my $statement ( 'vacuum', 'vacuum analyze' ) {
56 my $sth = dbh->prepare($statement) or die dbh->errstr;
57 $sth->execute or die $sth->errstr;
62 my $conf = new FS::Conf;
63 my $dest = $conf->config('dump-scpdest');
65 datasrc =~ /dbname=([\w\.]+)$/ or die "unparsable datasrc ". datasrc;
67 eval "use Net::SCP qw(scp);";
68 if ( driver_name eq 'Pg' ) {
69 system("pg_dump $database >/var/tmp/$database.sql")
71 die "database dumps not yet supported for ". driver_name;
73 scp("/var/tmp/$database.sql", $dest);
74 unlink "/var/tmp/$database.sql" or die $!;
80 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
81 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
83 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
89 die "Usage:\n\n freeside-daily [ -d 'date' ] user [ custnum custnum ... ]\n";
94 freeside-daily - Run daily billing and invoice collection events.
98 freeside-daily [ -d 'date' ] user [ custnum custnum ... ]
102 Bills customers and runs invoice collection events. Should be run from
105 This script replaces freeside-bill from 1.3.1.
107 Bills customers. Searches for customers who are due for billing and calls
108 the bill and collect methods of a cust_main object. See L<FS::cust_main>.
110 -d: Pretend it's 'date'. Date is in any format Date::Parse is happy with,
113 user: From the mapsecrets file - see config.html from the base documentation
115 custnum: if one or more customer numbers are specified, only bills those
116 customers. Otherwise, bills all customers.
122 L<FS::cust_main>, config.html from the base documentation