*** empty log message ***
[freeside.git] / FS / bin / freeside-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_a $opt_c $opt_i $opt_d);
13 getopts("acid:");
14 my $user = shift or die &usage;
15
16 adminsuidsetup $user;
17
18 my %bill_only = map { $_ => 1 } (
19   @ARGV ? @ARGV : ( map $_->custnum, qsearch('cust_main', {} ) )
20 );
21
22 #we're at now now (and later).
23 my($time)= $main::opt_d ? str2time($main::opt_d) : $^T;
24
25 # find packages w/ bill < time && cancel != '', and create corresponding
26 # customer objects
27
28 my($cust_main,%saw);
29 foreach $cust_main (
30   map {
31     if (
32       ( $main::opt_a || ( ( $_->getfield('bill') || 0 ) <= $time ) )
33       && $bill_only{ $_->custnum }
34       && !$saw{ $_->custnum }++
35     ) {
36       qsearchs('cust_main',{'custnum'=> $_->custnum } );
37     } else {
38       ();
39     }
40   } ( qsearch('cust_pkg', { 'cancel' => '' }),
41       qsearch('cust_pkg', { 'cancel' => 0  }),
42     )
43 ) {
44
45   # and bill them
46
47   print "Billing customer #" . $cust_main->getfield('custnum') . "\n";
48
49   my($error);
50
51   $error=$cust_main->bill('time'=>$time);
52   warn "Error billing,  customer #" . $cust_main->getfield('custnum') . 
53     ":" . $error if $error;
54
55   if ($main::opt_c) {
56     $error=$cust_main->collect('invoice_time'=>$time,
57                                'batch_card' => $main::opt_i ? 'no' : 'yes',
58                               );
59     warn "Error collecting customer #" . $cust_main->getfield('custnum') .
60       ":" . $error if $error;
61
62   #sleep 1;
63
64   }
65
66 }
67
68 # subroutines
69
70 sub untaint_argv {
71   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
72     $ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
73     $ARGV[$_]=$1;
74   }
75 }
76
77 sub usage {
78   die "Usage:\n\n  bill [ -c [ i ] ] [ -d 'date' ] [ -b ] user\n";
79 }
80
81 =head1 NAME
82
83 freeside-bill - Command line (crontab, script) interface to customer billing.
84
85 =head1 SYNOPSIS
86
87   freeside-bill [ -c [ -a ] [ -i ] ] [ -d 'date' ] user [ custnum custnum ... ]
88
89 =head1 DESCRIPTION
90
91 Bills customers.  Searches for customers who are due for billing and calls
92 the bill and collect methods of a cust_main object.  See L<FS::cust_main>.
93
94   -c: Turn on collecting (you probably want this).
95
96   -a: Call collect even if there isn't a new invoice (probably a bad idea for
97       daily use)
98
99   -i: real-time billing (as opposed to batch billing).  only relevant
100       for credit cards.
101
102   -d: Pretent it's 'date'.  Date is in any format Date::Parse is happy with,
103       but be careful.
104
105 user: From the mapsecrets file - see config.html from the base documentation
106
107 custnum: if one or more customer numbers are specified, only bills those
108 customers.  Otherwise, bills all customers.
109
110 =head1 VERSION
111
112 $Id: freeside-bill,v 1.2 1999-08-11 20:51:54 ivan Exp $
113
114 =head1 BUGS
115
116 =head1 SEE ALSO
117
118 L<FS::cust_main>, config.html from the base documentation
119
120 =cut
121