1 #!/usr/local/bin/perl -Tw
3 # bill: Bill customer(s)
5 # Usage: bill [ -c [ i ] ] [ -d 'date' ] [ -b ]
9 # Adds record to /dbin/cust_bill and /dbin/cust_pay (if payment made -
10 # CARD & COMP), prints invoice / charges card etc.
12 # -c: Turn on collecting (you probably want this).
14 # -i: real-time billing (as opposed to batch billing). only relevant
17 # -d: Pretent it's 'date'. Date is in any format Date::Parse is happy with,
20 # ## n/a ## -b: send batch when done billing
22 # ivan@voicenet.com sep/oct 96
24 # separated billing and collections, cleaned up code.
25 # ivan@voicenet.com 96-nov-11
28 # ivan@voicenet.com 96-nov-13
30 # added -v option and started to implement it, added 'd:' to getopts call
32 # ivan@voicenet.com 97-jan-2
34 # added more debug messages, moved some searches to fssearch.pl library (for
36 # rewrote "all customer" finder to know about bill dates, for speed.
37 # ivan@voicenet.com 97-jan-8
39 # thought about it a while, and removed passing of the -d option to collect...?
40 # ivan@voicenet.com 97-jan-14
42 # make all -v stuff STDERR
43 # ivan@voicenet.com 97-feb-4
45 # added pkgnum as argument to program from /db/part_pkg, with kludge for the
46 # "/bin/echo XX" 's already there.
47 # ivan@voicenet.com 97-feb-23
50 # - customers who are suspended can still be billed for the setup fee
51 # - cust_pkg record is re-read after the package setup fee program is run.
53 # that program can modify the record (for example, to start accounts off
55 # (best to think four or five times before modifying anything else!)
56 # ivan@voicenet.com 97-feb-26
58 # don't bill recurring fee if its not time! (was removed)
59 # ivan@voicenet.com 97-mar-6
61 # added -b option, send batch when done billing.
62 # ivan@voicenet.com 97-apr-4
64 #insecure dependency on line 179ish below needs to be fixed before bill is
66 # ivan@voicenet.com 97-jun-2
68 # removed running of setup program (depriciated)
69 # ivan@voicenet.com 97-jul-21
71 # rewrote for new API, removed option to specify custnums (use FS::Bill
72 # instead), removed -v option (?)
73 # ivan@voicenet.com 97-jul-22 - 23 - 25 -28
74 # (need to add back in email stuff, look in /home/ivan/old/dbin/collect)
76 # s/suidsetup/adminsuidsetup/, s/FS::Search/FS::Record/, added some batch
77 # exporting stuff (which still needs to be generalized) and removed &idiot
78 # ivan@sisd.com 98-may-27
81 # Revision 1.4 1998-11-07 08:21:26 ivan
91 use FS::UID qw(adminsuidsetup swapuid);
92 use FS::Record qw(qsearch qsearchs);
95 my($batchfile)="/var/spool/freeside/batch";
96 my($batchlock)="/var/spool/freeside/batch.lock";
100 &untaint_argv; #what it sounds like (eww)
101 use vars qw($opt_b $opt_c $opt_i $opt_d);
102 getopts("bcid:"); #switches
104 #we're at now now (and later).
105 my($time)= $main::opt_d ? str2time($main::opt_d) : $^T;
107 # find packages w/ bill < time && cancel != '', and create corresponding
113 if ( ( $_->getfield('bill') || 0 ) <= $time &&
114 !$saw{ $_->getfield('custnum') }++ ) {
115 qsearchs('cust_main',{'custnum'=> $_->getfield('custnum') } );
119 } qsearch('cust_pkg',{'cancel'=>''})
124 print "Billing customer #" . $cust_main->getfield('custnum') . "\n";
126 bless($cust_main,"FS::cust_main");
130 $error=$cust_main->bill('time'=>$time);
131 warn "Error billing, customer #" . $cust_main->getfield('custnum') .
132 ":" . $error if $error;
135 $error=$cust_main->collect('invoice_time'=>$time,
136 'batch_card' => $main::opt_i ? 'no' : 'yes',
138 warn "Error collecting customer #" . $cust_main->getfield('custnum') .
139 ":" . $error if $error;
149 # die "Batch still waiting for reply? ($batchlock exists)\n" if -e $batchlock;
150 # open(BATCHLOCK,"+>>$batchlock") or die "Can't open $batchlock: $!";
151 # select(BATCHLOCK); $|=1; select(STDOUT);
152 # unless ( flock(BATCHLOCK,,LOCK_EX|LOCK_NB) ) {
153 # seek(BATCHLOCK,0,0);
154 # my($pid)=<BATCHLOCK>;
156 # die "Is a batch running? (pid $pid)\n";
158 # seek(BATCHLOCK,0,0);
159 # print BATCHLOCK $$,"\n";
161 # ( open(BATCH,">$batchfile")
162 # and flock(BATCH,LOCK_EX|LOCK_NB)
163 # ) or die "Can't open $batchfile: $!";
165 # my($cust_pay_batch);
166 # foreach $cust_pay_batch (qsearch('cust_pay_batch',{})) {
167 # print BATCH join(':',
168 # $_->getfield('cardnum'),
169 # $_->getfield('exp'),
170 # $_->getfield('amount'),
171 # $_->getfield('payname')
172 # || $_->getfield('first'). ' '. $_->getfield('last'),
174 # $_->getfield('zip'),
178 # flock(BATCH,LOCK_UN);
181 # flock(BATCHLOCK,LOCK_UN);
188 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
189 $ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";