Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / bin / freeside-daily
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Std;
5 use FS::UID qw(adminsuidsetup);
6 use FS::Conf;
7 use FS::Log;
8
9 &untaint_argv;  #what it sounds like  (eww)
10 use vars qw(%opt);
11 getopts("p:a:d:vl:sy:nmrkg:o", \%opt);
12
13 my $user = shift or die &usage;
14 adminsuidsetup $user;
15 my $log = FS::Log->new('daily');
16 $log->info('start');
17
18 #you can skip this by not having a NetworkMonitoringSystem configured
19 use FS::Cron::nms_report qw(nms_report);
20 nms_report(%opt);
21
22 #no way to skip this yet, but should be harmless/quick
23 use FS::Cron::expire_banned_pay qw(expire_banned_pay);
24 expire_banned_pay(%opt);
25
26 #you can skip this by setting the disable_cron_billing config
27 use FS::Cron::bill qw(bill);
28 bill(%opt);
29
30 #you can skip this just by not having the config
31 use FS::Cron::breakage qw(reconcile_breakage);
32 reconcile_breakage(%opt);
33
34 #you can skip this just by not having the config
35 use FS::Cron::upload qw(upload);
36 upload(%opt);
37
38 use FS::Cron::set_lata_have_usage qw(set_lata_have_usage);
39 set_lata_have_usage(%opt);
40
41 # we used to send alerts about upcoming credit card expiration here
42 my $conf = new FS::Conf;
43 if($conf->exists('alert_expiration')) {
44   warn "WARNING: the alert_expiration option is obsolete.  If you ran 
45   freeside-upgrade, it should have configured credit card expiration alerts 
46   as billing events.\n";
47 }
48
49 #what to do about the below when using -m?  that is the question.
50
51 #you don't want to skip this, besides, it should be cheap
52 use FS::Cron::expire_user_pref qw(expire_user_pref);
53 expire_user_pref();
54
55 unless ( $opt{k} ) {
56   use FS::Cron::notify qw(notify_flat_delay);
57   notify_flat_delay(%opt);
58 }
59
60 #same
61 use FS::Cron::rt_tasks qw(rt_daily);
62 rt_daily(%opt);
63
64 #does nothing unless batch-gateway-* configs are set
65 use FS::Cron::pay_batch qw(batch_submit batch_receive);
66 batch_submit(%opt);
67 batch_receive(%opt);
68
69 #you can skip this by not having the config
70 use FS::Cron::agent_email qw(agent_email);
71 agent_email(%opt);
72
73 my $deldir = "$FS::UID::cache_dir/cache.$FS::UID::datasrc/";
74 unlink <${deldir}.invoice*>;
75 unlink <${deldir}.letter*>;
76 unlink <${deldir}.CGItemp*>;
77
78 #backup should be last
79 #you can skip this just by not having the config
80 use FS::Cron::backup qw(backup);
81 backup();
82
83 #except we'd rather not start cleanup jobs until the backup is done
84 use FS::Cron::cleanup qw(cleanup);
85 cleanup();
86
87 $log->info('finish');
88
89 ###
90 # subroutines
91 ###
92
93 sub untaint_argv {
94   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
95     #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
96     # Date::Parse
97     $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
98     $ARGV[$_]=$1;
99   }
100 }
101
102 sub usage {
103   die "Usage:\n\n  freeside-daily [ -d 'date' ] [ -y days ] [ -p 'payby' ] [ -a agentnum,agentnum,... ] [ -s ] [ -v ] [ -l level ] [ -m ] [ -k ] user [ custnum custnum ... ]\n";
104 }
105
106 ###
107 # documentation
108 ###
109
110 =head1 NAME
111
112 freeside-daily - Run daily billing and invoice collection events.
113
114 =head1 SYNOPSIS
115
116   freeside-daily [ -d 'date' ] [ -y days ] [ -p 'payby' ] [ -a agentnum,agentnum,... ] [ -s ] [ -o ] [ -v ] [ -l level ] [ -m ] [ -r ] [ -k ] user [ custnum custnum ... ]
117
118 =head1 DESCRIPTION
119
120 Bills customers and runs invoice collection events.  Should be run from
121 crontab daily.
122
123 Bills customers.  Searches for customers who are due for billing and calls
124 the bill and collect methods of a cust_main object.  See L<FS::cust_main>.
125
126   -d: Pretend it's 'date'.  Date is in any format Date::Parse is happy with,
127       but be careful.
128
129   -y: In addition to -d, which specifies an absolute date, the -y switch
130       specifies an offset, in days.  For example, "-y 15" would increment the
131       "pretend date" 15 days from whatever was specified by the -d switch
132       (or now, if no -d switch was given).
133
134   -n: When used with "-d" and/or "-y", specifies that invoices should be dated
135       with today's date, irregardless of the pretend date used to pre-generate
136       the invoices.
137
138   -p: Only process customers with the specified payby (CARD, DCRD, CHEK, DCHK, BILL, COMP, LECB)
139
140   -a: Only process customers with the specified agentnum.  Multiple agentnums can be specified, separated with commas.
141
142   -g: Don't process the provided pkgpart (or pkgparts, specified as a comma-
143       separated list).
144
145   -s: re-charge setup fees
146
147   -o: For packages which are more than one billing period behind, only charge for one billing period rather than catching up.
148
149   -v: enable debugging
150
151   -l: debugging level
152
153   -m: Multi-process mode uses the job queue for multi-process and/or multi-machine billing.
154
155   -r: Multi-process mode dry run option
156
157   -k: skip notify_flat_delay
158
159 user: Typically "fs_daily"
160
161 custnum: if one or more customer numbers are specified, only bills those
162 customers.  Otherwise, bills all customers.
163
164 =head1 BUGS
165
166 =head1 SEE ALSO
167
168 L<FS::cust_main>, config.html from the base documentation
169
170 =cut
171