add per-agent invoice templates, add per-package suspend invoice events, fix automati...
[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 driver_name dbh datasrc);
8 use FS::Record qw(qsearch qsearchs);
9 use FS::Conf;
10 use FS::cust_main;
11
12 &untaint_argv;  #what it sounds like  (eww)
13 use vars qw($opt_d $opt_v $opt_p $opt_s $opt_y);
14 getopts("p:d:vsy:");
15 my $user = shift or die &usage;
16
17 adminsuidsetup $user;
18
19 $FS::cust_main::DEBUG = 1 if $opt_v;
20
21 my %search;
22 $search{'payby'} = $opt_p if $opt_p;
23
24 my @cust_main = @ARGV
25   ? map { qsearchs('cust_main', { custnum => $_, %search } ) } @ARGV
26   : qsearch('cust_main', \%search )
27 ;
28
29 #we're at now now (and later).
30 my($time)= $opt_d ? str2time($opt_d) : $^T;
31 $time += $opt_y * 86400 if $opt_y;
32
33 my($cust_main,%saw);
34 foreach $cust_main ( @cust_main ) {
35
36   # $^T not $time because -d is for pre-printing invoices
37   foreach my $cust_pkg (
38     grep { $_->expire && $_->expire <= $^T } $cust_main->ncancelled_pkgs
39   ) {
40     my $error = $cust_pkg->cancel;
41     warn "Error cancelling expired pkg ". $cust_pkg->pkgnum. " for custnum ".
42          $cust_main->custnum. ": $error"
43       if $error;
44   }
45
46   my $error = $cust_main->bill( 'time'    => $time,
47                                 'resetup' => $opt_s, );
48   warn "Error billing, custnum ". $cust_main->custnum. ": $error" if $error;
49
50   $cust_main->apply_payments;
51   $cust_main->apply_credits;
52
53   $error = $cust_main->collect( 'invoice_time' => $time );
54   warn "Error collecting, custnum". $cust_main->custnum. ": $error" if $error;
55
56 }
57
58 if ( driver_name eq 'Pg' ) {
59   dbh->{AutoCommit} = 1; #so we can vacuum
60   foreach my $statement ( 'vacuum', 'vacuum analyze' ) {
61     my $sth = dbh->prepare($statement) or die dbh->errstr;
62     $sth->execute or die $sth->errstr;
63   }
64 }
65
66 #local hack
67 my $conf = new FS::Conf;
68 my $dest = $conf->config('dump-scpdest');
69 if ( $dest ) {
70   datasrc =~ /dbname=([\w\.]+)$/ or die "unparsable datasrc ". datasrc;
71   my $database = $1;
72   eval "use Net::SCP qw(scp);";
73   if ( driver_name eq 'Pg' ) {
74     system("pg_dump $database >/var/tmp/$database.sql")
75   } else {
76     die "database dumps not yet supported for ". driver_name;
77   }
78   scp("/var/tmp/$database.sql", $dest);
79   unlink "/var/tmp/$database.sql" or die $!;
80 }
81
82 # subroutines
83
84 sub untaint_argv {
85   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
86     #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
87     # Date::Parse
88     $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
89     $ARGV[$_]=$1;
90   }
91 }
92
93 sub usage {
94   die "Usage:\n\n  freeside-daily [ -d 'date' ] user [ custnum custnum ... ]\n";
95 }
96
97 =head1 NAME
98
99 freeside-daily - Run daily billing and invoice collection events.
100
101 =head1 SYNOPSIS
102
103   freeside-daily [ -d 'date' ] [ -y days ] [ -p 'payby' ] [ -s ] [ -v ] user [ custnum custnum ... ]
104
105 =head1 DESCRIPTION
106
107 Bills customers and runs invoice collection events.  Should be run from
108 crontab daily.
109
110 This script replaces freeside-bill from 1.3.1.
111
112 Bills customers.  Searches for customers who are due for billing and calls
113 the bill and collect methods of a cust_main object.  See L<FS::cust_main>.
114
115   -d: Pretend it's 'date'.  Date is in any format Date::Parse is happy with,
116       but be careful.
117
118   -y: In addition to -d, which specifies an absolute date, the -y switch
119       specifies an offset, in days.  For example, "-y 15" would increment the
120       "pretend date" 15 days from whatever was specified by the -d switch
121       (or now, if no -d switch was given).
122
123   -p: Only process customers with the specified payby (I<CARD>, I<DCRD>, I<CHEK>, I<DCHK>, I<BILL>, I<COMP>, I<LECB>)
124
125   -s: re-charge setup fees
126
127   -v: enable debugging
128
129 user: From the mapsecrets file - see config.html from the base documentation
130
131 custnum: if one or more customer numbers are specified, only bills those
132 customers.  Otherwise, bills all customers.
133
134 =head1 BUGS
135
136 =head1 SEE ALSO
137
138 L<FS::cust_main>, config.html from the base documentation
139
140 =cut
141