1dce9a19bf8f127a6633d286c3b2e9709e624797
[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);
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     unless ( exists $saw{ $_->custnum } && defined $saw{ $_->custnum} ) {
32       $saw{ $_->custnum } = 0; # to avoid 'use of uninitialized value' errors
33     }
34     if (
35       ( $main::opt_a || ( ( $_->getfield('bill') || 0 ) <= $time ) )
36       && $bill_only{ $_->custnum }
37       && !$saw{ $_->custnum }++
38     ) {
39       qsearchs('cust_main',{'custnum'=> $_->custnum } );
40     } else {
41       ();
42     }
43   } ( qsearch('cust_pkg', { 'cancel' => '' }),
44       qsearch('cust_pkg', { 'cancel' => 0  }),
45     )
46 ) {
47
48   # and bill them
49
50   print "Billing customer #" . $cust_main->getfield('custnum') . "\n";
51
52   my($error);
53
54   $error=$cust_main->bill('time'=>$time);
55   warn "Error billing,  customer #" . $cust_main->getfield('custnum') . 
56     ":" . $error if $error;
57
58   if ($main::opt_p) {
59     $cust_main->apply_payments;
60     $error=$cust_main->apply_credits;
61   }
62
63   if ($main::opt_c) {
64     $error=$cust_main->collect('invoice_time'=>$time,
65                                'batch_card' => $main::opt_i ? 'no' : 'yes',
66                               );
67     warn "Error collecting from customer #" . $cust_main->gcustnum.  ":$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 [ i ] ] [ -d 'date' ] [ -b ] user\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 ] [ -i ] ] [ -d 'date' ] user [ custnum custnum ... ]
97
98 =head1 DESCRIPTION
99
100 Bills customers.  Searches for customers who are due for billing and calls
101 the bill and collect methods of a cust_main object.  See L<FS::cust_main>.
102
103   -c: Turn on collecting (you probably want this).
104
105   -p: Apply unapplied payments and credits before collecting (you probably want
106       this too)
107
108   -a: Call collect even if there isn't a new invoice (probably a bad idea for
109       daily use)
110
111   -i: real-time billing (as opposed to batch billing).  only relevant
112       for credit cards.
113
114   -d: Pretend it's 'date'.  Date is in any format Date::Parse is happy with,
115       but be careful.
116
117 user: From the mapsecrets file - see config.html from the base documentation
118
119 custnum: if one or more customer numbers are specified, only bills those
120 customers.  Otherwise, bills all customers.
121
122 =head1 VERSION
123
124 $Id: freeside-bill,v 1.8 2001-09-03 22:07:39 ivan Exp $
125
126 =head1 BUGS
127
128 =head1 SEE ALSO
129
130 L<FS::cust_main>, config.html from the base documentation
131
132 =cut
133