Ticket #39615 Fix versions for upgrades
[freeside.git] / bin / rebill
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Std;
5 use Date::Parse;
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw( qsearch );
8 use cust_main_special;
9
10 &untaint_argv;  #what it sounds like  (eww)
11 use vars qw(%opt);
12 getopts("p:a:d:sy:n", \%opt);
13
14 my $user = shift or die &usage;
15 adminsuidsetup $user;
16
17 my (@custnums) = @ARGV;
18
19 my $time = $opt{d} ? str2time($opt{d}) : $^T;
20 $time += $opt{y} * 86400 if $opt{y};
21 my $invoice_time = $opt{n} ? $^T : $time;
22
23 my %args = (
24   'time'         => $time,
25   'invoice_time' => $invoice_time,
26   'actual_time'  => $^T, #when freeside-bill was started
27                          #(not, when using -m, freeside-queued)
28   'resetup'      => ( $opt{'s'} ? $opt{'s'} : 0 ),
29   'backbill'     => $time,
30 );
31
32 my $extra_sql = ( $opt{a} || $opt{p} ) ? ' AND ' : ' WHERE ';
33 $extra_sql .= "( ". join( ' OR ', map{ "custnum = $_" } @custnums ). " )";
34 $extra_sql = '' unless scalar @custnums;
35
36 my @cust = qsearch( { table   => 'cust_main',
37                       hashref => { $opt{a} ? ( 'agentnum' => $opt{a} ) : (),
38                                    $opt{p} ? ( 'payby'    => $opt{p} ) : (),
39                                  },
40                       extra_sql => $extra_sql,
41                     }
42                   );
43
44 foreach my $cust ( @cust ) {
45   my $balance = $cust->balance;
46   cust_main_special::bill($cust, %args);
47   if ($balance != $cust->balance){
48     $cust->apply_payments_and_credits;
49     my $error = $cust->collect(%args);
50     warn "Error collecting, custnum ". $cust->custnum. ": $error" if $error;
51   }
52 }
53
54
55 ###
56 # subroutines
57 ###
58
59 sub untaint_argv {
60   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
61     #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
62     # Date::Parse
63     $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
64     $ARGV[$_]=$1;
65   }
66 }
67
68 sub usage {
69   die "Usage:\n\n  freeside-daily [ -d 'date' ] [ -y days ] [ -p 'payby' ] [ -a agentnum ] [ -s ] user [ custnum custnum ... ]\n";
70 }
71
72 ###
73 # documentation
74 ###
75
76 =head1 NAME
77
78 freeside-daily - Run daily billing and invoice collection events.
79
80 =head1 SYNOPSIS
81
82   freeside-daily [ -d 'date' ] [ -y days ] [ -p 'payby' ] [ -a agentnum ] [ -s ] user [ custnum custnum ... ]
83
84 =head1 DESCRIPTION
85
86 Bills customers and runs invoice collection events.  Should be run from
87 crontab daily.
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   -y: In addition to -d, which specifies an absolute date, the -y switch
96       specifies an offset, in days.  For example, "-y 15" would increment the
97       "pretend date" 15 days from whatever was specified by the -d switch
98       (or now, if no -d switch was given).
99
100   -n: When used with "-d" and/or "-y", specifies that invoices should be dated
101       with today's date, irregardless of the pretend date used to pre-generate
102       the invoices.
103
104   -p: Only process customers with the specified payby (I<CARD>, I<DCRD>, I<CHEK>, I<DCHK>, I<BILL>, I<COMP>, I<LECB>)
105
106   -a: Only process customers with the specified agentnum
107
108   -s: re-charge setup fees
109
110   -v: enable debugging
111
112   -l: debugging level
113
114   -m: Experimental multi-process mode uses the job queue for multi-process and/or multi-machine billing.
115
116   -r: Multi-process mode dry run option
117
118   -k: skip notify_flat_delay and vacuum
119
120 user: Freeside user
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