1db786120e4368c250baa3efb51906ddfdd9b1a9
[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);
14 getopts("d:v");
15 my $user = shift or die &usage;
16
17 adminsuidsetup $user;
18
19 $FS::cust_main::Debug = 1 if $opt_v;
20
21 my @cust_main = @ARGV
22   ? map { qsearchs('cust_main', { custnum => $_ } ) } @ARGV
23   : qsearch('cust_main', {} )
24 ;
25
26 #we're at now now (and later).
27 my($time)= $opt_d ? str2time($opt_d) : $^T;
28
29 my($cust_main,%saw);
30 foreach $cust_main ( @cust_main ) {
31
32   # $^T not $time because -d is for pre-printing invoices
33   foreach my $cust_pkg (
34     grep { $_->expire && $_->expire <= $^T } $cust_main->ncancelled_pkgs
35   ) {
36     my $error = $cust_pkg->cancel;
37     warn "Error cancelling expired pkg ". $cust_pkg->pkgnum. " for custnum ".
38          $cust_main->custnum. ": $error"
39       if $error;
40   }
41
42   my $error = $cust_main->bill( 'time' => $time );
43   warn "Error billing, custnum ". $cust_main->custnum. ": $error" if $error;
44
45   $cust_main->apply_payments;
46   $cust_main->apply_credits;
47
48   $error = $cust_main->collect( 'invoice_time' => $time );
49   warn "Error collecting, custnum". $cust_main->custnum. ": $error" if $error;
50
51 }
52
53 if ( driver_name eq 'Pg' ) {
54   dbh->{AutoCommit} = 1; #so we can vacuum
55   foreach my $statement ( 'vacuum', 'vacuum analyze' ) {
56     my $sth = dbh->prepare($statement) or die dbh->errstr;
57     $sth->execute or die $sth->errstr;
58   }
59 }
60
61 #local hack
62 my $conf = new FS::Conf;
63 my $dest = $conf->config('dump-scpdest');
64 if ( $dest ) {
65   datasrc =~ /dbname=([\w\.]+)$/ or die "unparsable datasrc ". datasrc;
66   my $database = $1;
67   eval "use Net::SCP qw(scp);";
68   if ( driver_name eq 'Pg' ) {
69     system("pg_dump $database >/var/tmp/$database.sql")
70   } else {
71     die "database dumps not yet supported for ". driver_name;
72   }
73   scp("/var/tmp/$database.sql", $dest);
74   unlink "/var/tmp/$database.sql" or die $!;
75 }
76
77 # subroutines
78
79 sub untaint_argv {
80   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
81     #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
82     # Date::Parse
83     $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
84     $ARGV[$_]=$1;
85   }
86 }
87
88 sub usage {
89   die "Usage:\n\n  freeside-daily [ -d 'date' ] user [ custnum custnum ... ]\n";
90 }
91
92 =head1 NAME
93
94 freeside-daily - Run daily billing and invoice collection events.
95
96 =head1 SYNOPSIS
97
98   freeside-daily [ -d 'date' ] user [ custnum custnum ... ]
99
100 =head1 DESCRIPTION
101
102 Bills customers and runs invoice collection events.  Should be run from
103 crontab daily.
104
105 This script replaces freeside-bill from 1.3.1.
106
107 Bills customers.  Searches for customers who are due for billing and calls
108 the bill and collect methods of a cust_main object.  See L<FS::cust_main>.
109
110   -d: Pretend it's 'date'.  Date is in any format Date::Parse is happy with,
111       but be careful.
112
113 user: From the mapsecrets file - see config.html from the base documentation
114
115 custnum: if one or more customer numbers are specified, only bills those
116 customers.  Otherwise, bills all customers.
117
118 =head1 BUGS
119
120 =head1 SEE ALSO
121
122 L<FS::cust_main>, config.html from the base documentation
123
124 =cut
125