update billing documentation for the new world of invoice events
[freeside.git] / FS / bin / freeside-daily
1 #!/usr/bin/perl -w
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_d);
13 getopts("d:");
14 my $user = shift or die &usage;
15
16 adminsuidsetup $user;
17
18 my @cust_main = @ARGV
19   ? map { qsearchs('cust_main', { custnum => $_ } ) } @ARGV
20   : qsearch('cust_main', {} )
21 ;
22
23 #we're at now now (and later).
24 my($time)= $opt_d ? str2time($opt_d) : $^T;
25
26 my($cust_main,%saw);
27 foreach $cust_main ( @cust_main ) {
28
29   my $error;
30
31   $error = $cust_main->bill( 'time' => $time );
32   warn "Error billing, custnum ". $cust_main->custnum. ": $error" if $error;
33
34   $cust_main->apply_payments;
35   $cust_main->apply_credits;
36
37   $error=$cust_main->collect( 'invoice_time' => $time );
38   warn "Error collecting, custnum". $cust_main->custnum. ": $error" if $error;
39
40 }
41
42 # subroutines
43
44 sub untaint_argv {
45   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
46     #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
47     # Date::Parse
48     $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
49     $ARGV[$_]=$1;
50   }
51 }
52
53 sub usage {
54   die "Usage:\n\n  freeside-daily [ -d 'date' ] user [ custnum custnum ... ]\n";
55 }
56
57 =head1 NAME
58
59 freeside-daily - Run daily billing and invoice collection events.
60
61 =head1 SYNOPSIS
62
63   freeside-daily [ -d 'date' ] user [ custnum custnum ... ]
64
65 =head1 DESCRIPTION
66
67 Bills customers and runs invoice collection events.  Should be run from
68 crontab daily.
69
70 This script replaces freeside-bill from 1.3.1.
71
72 Bills customers.  Searches for customers who are due for billing and calls
73 the bill and collect methods of a cust_main object.  See L<FS::cust_main>.
74
75   -d: Pretend it's 'date'.  Date is in any format Date::Parse is happy with,
76       but be careful.
77
78 user: From the mapsecrets file - see config.html from the base documentation
79
80 custnum: if one or more customer numbers are specified, only bills those
81 customers.  Otherwise, bills all customers.
82
83 =head1 BUGS
84
85 =head1 SEE ALSO
86
87 L<FS::cust_main>, config.html from the base documentation
88
89 =cut
90