package expiration
[freeside.git] / FS / bin / freeside-daily
index 8d839cb..22bf2c9 100755 (executable)
@@ -4,17 +4,19 @@ use strict;
 use Fcntl qw(:flock);
 use Date::Parse;
 use Getopt::Std;
-use FS::UID qw(adminsuidsetup);
+use FS::UID qw(adminsuidsetup driver_name dbh);
 use FS::Record qw(qsearch qsearchs);
 use FS::cust_main;
 
 &untaint_argv; #what it sounds like  (eww)
-use vars qw($opt_d);
-getopts("d:");
+use vars qw($opt_d $opt_v);
+getopts("d:v");
 my $user = shift or die &usage;
 
 adminsuidsetup $user;
 
+$FS::cust_main::Debug = 1 if $opt_v;
+
 my @cust_main = @ARGV
   ? map { qsearchs('cust_main', { custnum => $_ } ) } @ARGV
   : qsearch('cust_main', {} )
@@ -26,19 +28,34 @@ my($time)= $opt_d ? str2time($opt_d) : $^T;
 my($cust_main,%saw);
 foreach $cust_main ( @cust_main ) {
 
-  my $error;
+  # $^T not $time because -d is for pre-printing invoices
+  foreach my $cust_pkg (
+    grep { $_->expire && $_->expire >= $^T } $cust_main->ncancelled_pkgs
+  ) {
+    my $error = $cust_pkg->cancel;
+    warn "Error cancelling expired pkg ". $cust_pkg->pkgnum. " for custnum ".
+         $cust_main->custnum. ": $error"
+      if $error;
+  }
 
-  $error = $cust_main->bill( 'time' => $time );
+  my $error = $cust_main->bill( 'time' => $time );
   warn "Error billing, custnum ". $cust_main->custnum. ": $error" if $error;
 
   $cust_main->apply_payments;
   $cust_main->apply_credits;
 
-  $error=$cust_main->collect( 'invoice_time' => $time );
+  $error = $cust_main->collect( 'invoice_time' => $time );
   warn "Error collecting, custnum". $cust_main->custnum. ": $error" if $error;
 
 }
 
+if ( driver_name eq 'Pg' ) {
+  foreach my $statement ( 'vacuum', 'vacuum analyze' ) {
+    my $sth = dbh->prepare($statement) or die dbh->errstr;
+    $sth->execute or die $sth->errstr;
+  }
+}
+
 # subroutines
 
 sub untaint_argv {