- documentation updates
[freeside.git] / FS / bin / freeside-overdue
index 541b8be..116245f 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/perl -w
 
 use strict;
 use vars qw( $days_to_pay $cust_main $cust_pkg 
@@ -13,12 +13,12 @@ use FS::UID qw(adminsuidsetup);
 
 &untaint_argv;
 my %opt;
-getopts('ed:qpsc', \%opt);
+getopts('ed:qpl:scbyoi', \%opt);
 my $user = shift or die &usage;
 
 adminsuidsetup $user;
 
-my $now = time;
+my $now = time; #eventually take a time option like freeside-bill
 my ($sec,$min,$hour,$mday,$mon,$year) =
   (localtime($now) )[0,1,2,3,4,5];
 $mon++;
@@ -48,16 +48,23 @@ foreach $cust_main ( qsearch('cust_main',{} ) ) {
             $cust_main->balance_date(time-$opt{d} * 86400);
     }
 
+    if ( $opt{p} && ! grep { $_ eq 'POST' } $cust_main->invoicing_list ) {
+      print "\n\tAdding postal invoicing" unless $opt{q};
+      my @invoicing_list = $cust_main->invoicing_list;
+      push @invoicing_list, 'POST';
+      $cust_main->invoicing_list(\@invoicing_list);
+    }
+
+    if ( $opt{l} ) {
+      print "\n\tCharging late fee of \$$opt{l}" unless $opt{q};
+      my $error = $cust_main->charge($opt{l}, 'Late fee');
+      # comment or plandata with info so we don't redo the same late fee every
+      # day
+    }
+
     foreach $cust_pkg ( qsearch( 'cust_pkg', 
                                  { 'custnum' => $cust_main->custnum } ) ) {
 
-      if ($opt{p} && ! grep { $_ eq 'POST' } $cust_main->invoicing_list ) {
-        print "\n\tAdding postal invoicing" unless $opt{q};
-        my @invoicing_list = $cust_main->invoicing_list;
-        push @invoicing_list, 'POST';
-        $cust_main->invoicing_list(\@invoicing_list);
-      }
-
       if ($opt{s}) {
         print "\n\tSuspending pkgnum " . $cust_pkg->pkgnum unless $opt{q};
         $cust_pkg->suspend;
@@ -67,7 +74,31 @@ foreach $cust_main ( qsearch('cust_main',{} ) ) {
         print "\n\tCancelling pkgnum " . $cust_pkg->pkgnum unless $opt{q};
         $cust_pkg->cancel;
       }
+      
+    }
+
+    if ( $opt{b} ) {
+      print "\n\tBilling" unless $opt{q};
+      my $error = $cust_main->bill('time'=>$now);
+      warn "Error billing,  customer #" . $cust_main->custnum . 
+        ":" . $error if $error;
+    }
 
+    if ( $opt{y} ) {
+      print "\n\tApplying outstanding payments and credits" unless $opt{q};
+      $cust_main->apply_payments;
+      $cust_main->apply_credits;
+    }
+
+    if ( $opt{o} ) {
+      print "\n\tCollecting" unless $opt{q};
+      my $error = $cust_main->collect(
+        'invoice_time' => $now,
+        'batch_card'   => $opt{i} ? 'no' : 'yes',
+        'force_print'  => 'yes',
+      );
+      warn "Error collecting from customer #" . $cust_main->custnum.  ":$error"
+        if $error;
     }
 
     print "\n" unless $opt{q};
@@ -78,13 +109,13 @@ foreach $cust_main ( qsearch('cust_main',{} ) ) {
 
 sub untaint_argv {
   foreach $_ ( $[ .. $#ARGV ) { 
-    $ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
+    $ARGV[$_] =~ /^([\w\-\/\.]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
     $ARGV[$_]=$1;
   }
 }
 
 sub usage {
-  die "Usage:\n\n  freeside-overdue [ -e ] [ -d days ] [ -q ] [ -p ] [ -s ] [ -c ] user\n";
+  die "Usage:\n\n    freeside-overdue [ -e ] [ -d days ] [ -q ] [ -p ] [ -l amount ] [ -s ] [ -c ] [ -b ] [ -y ] [ -o [ -i ] ] user\n";
 }
 
 
@@ -94,28 +125,42 @@ freeside-overdue - Perform actions on overdue and/or expired accounts.
 
 =head1 SYNOPSIS
 
-  freeside-overdue [ -e ] [ -d days ] [ -q ] [ -p ] [ -s ] [ -c ] user
+  freeside-overdue [ -e ] [ -d days ] [ -q ] [ -p ] [ -l amount ] [ -s ] [ -c ] [ -b ] [ -y ] [ -o [ -i ] ] user
 
 =head1 DESCRIPTION
 
+This script is deprecated in 1.4.0.  You should use freeside-daily and invoice
+events instead.
+
 Performs actions on overdue and/or expired accounts.
 
 Selection options (at least one selection option is required):
 
-  -d:  Customers with a balance due on invoices older than the supplied number
-        of days.  Requires an integer argument.
+  -d:  Customers with a balance due on invoices older than the supplied number
+       of days.  Requires an integer argument.
 
-  -e:   Customers with a billing expiration date in the past.
+  -e:  Customers with a billing expiration date in the past.
 
 Action options: 
 
-  -q:   Be quiet (by default, suspended accounts are printed).
+  -q:  Be quiet (by default, selected accounts are printed).
 
-  -p:   Add postal invoicing to the relevant customers.
+  -p:  Add postal invoicing to the relevant customers.
 
-  -s:  Suspend accounts.
+  -l:  Add a charge of the given amount to the relevant customers.
 
-  -c:   Cancel accounts.
+  -s:  Suspend accounts.
+
+  -c:  Cancel accounts.
+
+  -b:  Bill customers (create invoices)
+
+  -y:  Apply unapplied payments and credits
+
+  -o:  Collect from customers (charge cards, print invoices)
+
+    -i:  real-time billing (as opposed to batch billing).  only relevant
+         for credit cards.
 
   user: From the mapsecrets file - see config.html from the base documentation
 
@@ -123,16 +168,28 @@ Action options:
 
 Example crontab entries:
 
-20 4,16 * * * freeside-overdue -e -s user
-20 4,16 * * * freeside-overdue -d 30 -p -q user
-20 4,16 * * * freeside-overdue -d 60 user
-20 4,16 * * * freeside-overdue -d 90 -s user
-20 4,16 * * * freeside-overdue -d 120 -c user
+# suspend expired accounts
+20 4 * * * freeside-overdue -e -s user
+
+# quietly add postal invoicing to customers over 30 days past due
+20 4 * * * freeside-overdue -d 30 -p -q user
+
+# suspend accounts and charge a $10.23 fee for customers over 60 days past due
+20 4 * * * freeside-overdue -d 60 -s -l 10.23 user
+
+# cancel accounts over 90 days past due
+20 4 * * * freeside-overdue -d 90 -c user
 
 =head1 ORIGINAL AUTHORS
 
 Original disable-overdue version by mw/kwh: Mark W.? and Kristian Hoffmann ?
 
+Ivan seems to be turning it into the "do-everything" CLI.
+
+=head1 BUGS
+
+Hell now that this is the do-everything CLI it should have --longoptions
+
 =cut
 
 1;