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