add stack backtrace to fatal problems in virtual field check
[freeside.git] / FS / bin / freeside-bill
1 #!/usr/bin/perl -w
2 # don't take any world-facing input
3 #!/usr/bin/perl -Tw
4
5 use strict;
6 use Fcntl qw(:flock);
7 use Date::Parse;
8 use Getopt::Std;
9 use FS::UID qw(adminsuidsetup);
10 use FS::Record qw(qsearch qsearchs);
11 use FS::cust_main;
12
13 &untaint_argv;  #what it sounds like  (eww)
14 use vars qw($opt_a $opt_c $opt_d $opt_p);
15 getopts("acd:p");
16 my $user = shift or die &usage;
17
18 adminsuidsetup $user;
19
20 my %bill_only = map { $_ => 1 } (
21   @ARGV ? @ARGV : ( map $_->custnum, qsearch('cust_main', {} ) )
22 );
23
24 #we're at now now (and later).
25 my($time)= $opt_d ? str2time($opt_d) : $^T;
26
27 # find packages w/ bill < time && cancel != '', and create corresponding
28 # customer objects
29
30 my($cust_main,%saw);
31 foreach $cust_main (
32   map {
33     unless ( exists $saw{ $_->custnum } && defined $saw{ $_->custnum} ) {
34       $saw{ $_->custnum } = 0; # to avoid 'use of uninitialized value' errors
35     }
36     if (
37       ( $opt_a || ( ( $_->getfield('bill') || 0 ) <= $time ) )
38       && $bill_only{ $_->custnum }
39       && !$saw{ $_->custnum }++
40     ) {
41       qsearchs('cust_main',{'custnum'=> $_->custnum } );
42     } else {
43       ();
44     }
45   } ( qsearch('cust_pkg', { 'cancel' => '' }),
46       qsearch('cust_pkg', { 'cancel' => 0  }),
47     )
48 ) {
49
50   # and bill them
51
52   print "Billing customer #" . $cust_main->getfield('custnum') . "\n";
53
54   my($error);
55
56   $error=$cust_main->bill('time'=>$time);
57   warn "Error billing,  customer #" . $cust_main->getfield('custnum') . 
58     ":" . $error if $error;
59
60   if ($opt_p) {
61     $cust_main->apply_payments;
62     $cust_main->apply_credits;
63   }
64
65   if ($opt_c) {
66     $error=$cust_main->collect( 'invoice_time' => $time);
67     warn "Error collecting from customer #" . $cust_main->custnum.  ":$error"
68       if $error;
69
70     #sleep 1;
71   }
72
73 }
74
75 # subroutines
76
77 sub untaint_argv {
78   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
79     #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
80     # Date::Parse
81     $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
82     $ARGV[$_]=$1;
83   }
84 }
85
86 sub usage {
87   die "Usage:\n\n  freeside-bill [ -c [ -p ] ] [ -d 'date' ] user [ custnum custnum ... ]\n";
88 }
89
90 =head1 NAME
91
92 freeside-bill - Command line (crontab, script) interface to customer billing.
93
94 =head1 SYNOPSIS
95
96   freeside-bill [ -c [ -p ] [ -a ] ] [ -d 'date' ] user [ custnum custnum ... ]
97
98 =head1 DESCRIPTION
99
100 This script is deprecated in 1.4.0.  You should use freeside-daily instead.
101
102 Bills customers.  Searches for customers who are due for billing and calls
103 the bill and collect methods of a cust_main object.  See L<FS::cust_main>.
104
105   -c: Turn on collecting (you probably want this).
106
107   -p: Apply unapplied payments and credits before collecting (you probably want
108       this too)
109
110   -a: Call collect even if there isn't a new invoice (probably a bad idea for
111       daily use)
112
113   -d: Pretend it's 'date'.  Date is in any format Date::Parse is happy with,
114       but be careful.
115
116 user: From the mapsecrets file - see config.html from the base documentation
117
118 custnum: if one or more customer numbers are specified, only bills those
119 customers.  Otherwise, bills all customers.
120
121 =head1 BUGS
122
123 =head1 SEE ALSO
124
125 L<freeside-daily>, L<FS::cust_main>, config.html from the base documentation
126
127 =cut
128