service and package disable!
[freeside.git] / FS / bin / freeside-overdue
1 #!/usr/bin/perl
2
3 use strict;
4 use vars qw( $days_to_pay $cust_main $cust_pkg 
5              $cust_svc $svc_acct );
6 use Getopt::Std;
7 use FS::cust_main;
8 use FS::cust_pkg;
9 use FS::cust_svc;
10 use FS::svc_acct;
11 use FS::Record qw(qsearch qsearchs);
12 use FS::UID qw(adminsuidsetup);
13
14 &untaint_argv;
15 my %opt;
16 getopts('ed:qplsc', \%opt);
17 my $user = shift or die &usage;
18
19 adminsuidsetup $user;
20
21 my $now = time;
22 my ($sec,$min,$hour,$mday,$mon,$year) =
23   (localtime($now) )[0,1,2,3,4,5];
24 $mon++;
25 $year += 1900;
26
27 foreach $cust_main ( qsearch('cust_main',{} ) ) {
28
29   my ( $eyear, $emon, $eday ) = ( 2037, 12, 31 );
30   if ( $cust_main->paydate =~ /^(\d{4})\-(\d{1,2})\-(\d{1,2})$/
31        && $cust_main->payby eq 'BILL') {
32     ( $eyear, $emon, $eday ) = ( $1, $2, $3 );
33   }
34
35   if ( ( $opt{d}
36            && $cust_main->balance_date(time - $opt{d} * 86400) > 0
37            && qsearchs( 'cust_pkg', { 'custnum' => $cust_main->custnum,
38                                       'susp' => "" } ) )
39        || ( $opt{e}
40             && $cust_main->payby eq 'BILL'
41             && ( $eyear < $year
42                  || ( $eyear == $year && $emon < $mon ) ) )
43   ) { 
44
45     unless ( $opt{q} ) {
46       print $cust_main->custnum, "\t",
47             $cust_main->last, "\t", $cust_main->first, "\t",
48             $cust_main->balance_date(time-$opt{d} * 86400);
49     }
50
51 #    if ( $opt{l} ) {
52 #      print "\n\tCharging late fee of \$$opt{l}" unless $opt{q};
53 #
54 #    }
55
56     foreach $cust_pkg ( qsearch( 'cust_pkg', 
57                                  { 'custnum' => $cust_main->custnum } ) ) {
58
59       if ( $opt{p} && ! grep { $_ eq 'POST' } $cust_main->invoicing_list ) {
60         print "\n\tAdding postal invoicing" unless $opt{q};
61         my @invoicing_list = $cust_main->invoicing_list;
62         push @invoicing_list, 'POST';
63         $cust_main->invoicing_list(\@invoicing_list);
64       }
65
66       if ($opt{s}) {
67         print "\n\tSuspending pkgnum " . $cust_pkg->pkgnum unless $opt{q};
68         $cust_pkg->suspend;
69       }
70
71       if ($opt{c}) {
72         print "\n\tCancelling pkgnum " . $cust_pkg->pkgnum unless $opt{q};
73         $cust_pkg->cancel;
74       }
75
76     }
77
78     print "\n" unless $opt{q};
79
80   }
81
82 }
83
84 sub untaint_argv {
85   foreach $_ ( $[ .. $#ARGV ) { 
86     $ARGV[$_] =~ /^([\w\-\/\.]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
87     $ARGV[$_]=$1;
88   }
89 }
90
91 sub usage {
92   die "Usage:\n\n  freeside-overdue [ -e ] [ -d days ] [ -q ] [ -p ] [ -l amount ] [ -s ] [ -c ] user\n";
93 }
94
95
96 =head1 NAME
97
98 freeside-overdue - Perform actions on overdue and/or expired accounts.
99
100 =head1 SYNOPSIS
101
102   freeside-overdue [ -e ] [ -d days ] [ -q ] [ -p ] [ -l amount ] [ -s ] [ -c ] user
103
104 =head1 DESCRIPTION
105
106 Performs actions on overdue and/or expired accounts.
107
108 Selection options (at least one selection option is required):
109
110   -d:   Customers with a balance due on invoices older than the supplied number
111         of days.  Requires an integer argument.
112
113   -e:   Customers with a billing expiration date in the past.
114
115 Action options: 
116
117   -q:   Be quiet (by default, selected accounts are printed).
118
119   -p:   Add postal invoicing to the relevant customers.
120
121   -l:   Add a charge of the given amount to the relevant customers.
122
123   -s:   Suspend accounts.
124
125   -c:   Cancel accounts.
126
127   user: From the mapsecrets file - see config.html from the base documentation
128
129 =head1 CRONTAB
130
131 Example crontab entries:
132
133 # suspend expired accounts
134 20 4 * * * freeside-overdue -e -s user
135
136 # quietly add postal invoicing to customers over 30 days past due
137 20 4 * * * freeside-overdue -d 30 -p -q user
138
139 # suspend accounts and charge a $10.23 fee for customers over 60 days past due
140 20 4 * * * freeside-overdue -d 60 -s -l 10.23 user
141
142 # cancel accounts over 90 days past due
143 20 4 * * * freeside-overdue -d 90 -c user
144
145 =head1 ORIGINAL AUTHORS
146
147 Original disable-overdue version by mw/kwh: Mark W.? and Kristian Hoffmann ?
148
149 =cut
150
151 1;
152