adminsuidsetup needs user, pod, cleanup
[freeside.git] / bin / bill
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use Fcntl qw(:flock);
5 use Date::Parse;
6 use Getopt::Std;
7 use FS::UID qw(adminsuidsetup swapuid);
8 use FS::Record qw(qsearch qsearchs);
9 use FS::cust_main;
10
11 &untaint_argv;  #what it sounds like  (eww)
12 use vars qw($opt_b $opt_c $opt_i $opt_d);
13 getopts("bcid:");       #switches
14 my $user = shift or die &usage;
15
16 adminsuidsetup $user;
17
18 #we're at now now (and later).
19 my($time)= $main::opt_d ? str2time($main::opt_d) : $^T;
20
21 # find packages w/ bill < time && cancel != '', and create corresponding
22 # customer objects
23
24 my($cust_main,%saw);
25 foreach $cust_main (
26   map {
27     if ( ( $_->getfield('bill') || 0 ) <= $time &&
28          !$saw{ $_->getfield('custnum') }++ ) {
29       qsearchs('cust_main',{'custnum'=> $_->getfield('custnum') } );
30     } else {
31       ();
32     }
33   } qsearch('cust_pkg',{'cancel'=>''})
34 ) {
35
36   # and bill them
37
38   print "Billing customer #" . $cust_main->getfield('custnum') . "\n";
39
40   my($error);
41
42   $error=$cust_main->bill('time'=>$time);
43   warn "Error billing,  customer #" . $cust_main->getfield('custnum') . 
44     ":" . $error if $error;
45
46   if ($main::opt_c) {
47     $error=$cust_main->collect('invoice_time'=>$time,
48                                'batch_card' => $main::opt_i ? 'no' : 'yes',
49                               );
50     warn "Error collecting customer #" . $cust_main->getfield('custnum') .
51       ":" . $error if $error;
52
53   #sleep 1;
54
55   }
56
57 }
58
59 # subroutines
60
61 sub untaint_argv {
62   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
63     $ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
64     $ARGV[$_]=$1;
65   }
66 }
67
68 sub usage {
69   die "Usage:\n\n  bill [ -c [ i ] ] [ -d 'date' ] [ -b ] user\n";
70 }
71
72 =head1 NAME
73
74 bill - Command line (crontab, script) interface to customer billing.
75
76 =head1 SYNOPSIS
77
78   bill [ -c [ i ] ] [ -d 'date' ] user
79
80 =head1 DESCRIPTION
81
82 Bills all customers.  Searches for customers who are due for billing and calls
83 the bill and collect methods of a cust_main object.  See L<FS::cust_main>.
84
85 -c: Turn on collecting (you probably want this).
86
87 -i: real-time billing (as opposed to batch billing).  only relevant
88     for credit cards.
89
90 -d: Pretent it's 'date'.  Date is in any format Date::Parse is happy with,
91     but be careful.
92
93 user: From the mapsecrets file - see config.html from the base documentation
94
95 =head1 BUGS
96
97 =head1 SEE ALSO
98
99 L<FS::cust_main>, config.html from the base documentation
100
101 =head1 HISTORY
102
103 ivan@voicenet.com sep/oct 96
104
105 separated billing and collections, cleaned up code.
106 ivan@voicenet.com 96-nov-11
107
108 added -d option
109 ivan@voicenet.com 96-nov-13
110
111 added -v option and started to implement it, added 'd:' to getopts call
112   (oops!)
113 ivan@voicenet.com 97-jan-2
114
115 added more debug messages, moved some searches to fssearch.pl library (for 
116 speed)
117 rewrote "all customer" finder to know about bill dates, for speed.
118 ivan@voicenet.com 97-jan-8
119
120 thought about it a while, and removed passing of the -d option to collect...?
121 ivan@voicenet.com 97-jan-14
122
123 make all -v stuff STDERR 
124 ivan@voicenet.com 97-feb-4
125
126 added pkgnum as argument to program from /db/part_pkg, with kludge for the
127 "/bin/echo XX" 's already there.
128 ivan@voicenet.com 97-feb-23
129
130 - general cleanup
131 - customers who are suspended can still be billed for the setup fee
132 - cust_pkg record is re-read after the package setup fee program is run.
133   this way,
134   that program can modify the record (for example, to start accounts off
135   suspended)
136   (best to think four or five times before modifying anything else!)
137 ivan@voicenet.com 97-feb-26
138
139 don't bill recurring fee if its not time! (was removed)
140 ivan@voicenet.com 97-mar-6
141
142 added -b option, send batch when done billing.
143 ivan@voicenet.com 97-apr-4
144
145 insecure dependency on line 179ish below needs to be fixed before bill is
146 used setuid
147 ivan@voicenet.com 97-jun-2
148
149 removed running of setup program (depriciated)
150 ivan@voicenet.com 97-jul-21
151
152 rewrote for new API, removed option to specify custnums (use FS::Bill 
153 instead), removed -v option (?)
154 ivan@voicenet.com 97-jul-22 - 23 - 25 -28
155 (need to add back in email stuff, look in /home/ivan/old/dbin/collect)
156
157 s/suidsetup/adminsuidsetup/, s/FS::Search/FS::Record/, added some batch
158 exporting stuff (which still needs to be generalized) and removed &idiot
159 ivan@sisd.com 98-may-27
160
161 $Log: bill,v $
162 Revision 1.5  1998-11-15 02:51:21  ivan
163 adminsuidsetup needs user, pod, cleanup
164
165 Revision 1.4  1998/11/07 08:21:26  ivan
166 missing use
167
168 =cut
169
170