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