This commit was manufactured by cvs2svn to create tag 'freeside_2_1_0'.
[freeside.git] / FS / FS / Cron / alert_expiration.pm
1 package FS::Cron::alert_expiration;
2
3 use vars qw( @ISA @EXPORT_OK);
4 use Exporter;
5 use FS::Record qw(qsearch);
6 use FS::Conf;
7 use FS::cust_main;
8 use FS::Misc;
9 use Time::Local;
10 use Date::Parse qw(str2time);
11
12
13 @ISA = qw( Exporter );
14 @EXPORT_OK = qw( alert_expiration );
15
16 my $warning_time = 30 * 24 * 60 * 60;
17 my $urgent_time = 15 * 24 * 60 * 60;
18 my $panic_time = 5 * 24 * 60 * 60;
19 my $window_time = 24 * 60 * 60;
20
21 sub alert_expiration {
22   my $conf = new FS::Conf;
23   my $smtpmachine = $conf->config('smtpmachine');
24   
25   my %opt = @_;
26   my ($_date) = $opt{'d'} ? str2time($opt{'d'}) : $^T;
27   $_date += $opt{'y'} * 86400 if $opt{'y'};
28   my ($sec, $min, $hour, $mday, $mon, $year) = (localtime($_date)) [0..5];
29   $mon++;
30
31   my $debug = 0;
32   $debug = 1 if $opt{'v'};
33   $debug = $opt{'l'} if $opt{'l'};
34
35   $FS::cust_main::DEBUG = $debug;
36
37   # Get a list of customers.
38  
39   my %limit;
40   $limit{'agentnum'} = $opt{'a'} if $opt{'a'};
41   $limit{'payby'}    = $opt{'p'} if $opt{'p'};
42
43   my @customers;
44
45   if(my @custnums = @ARGV) {
46     # We're given an explicit list of custnums, so select those.  Then check against 
47     # -a and -p to avoid doing anything unexpected.
48     foreach (@custnums) {
49       my $customer = FS::cust_main->by_key($_);
50       if($customer and (!$opt{'a'} or $customer->agentnum == $opt{'a'})
51                    and (!$opt{'p'} or $customer->payby eq $opt{'p'}) ) {
52         push @customers, $customer;
53       }
54     }
55   }
56   else { # no @ARGV
57     @customers = qsearch('cust_main', \%limit);
58   }
59   return if(!@customers);
60   foreach my $customer (@customers) {
61     my $paydate = $customer->paydate;
62     next if $paydate =~ /^\s*$/; # skip empty expiration dates
63     
64     my $custnum = $customer->custnum;
65     my $first   = $customer->first;
66     my $last    = $customer->last;
67     my $company = $customer->company;
68     my $payby   = $customer->payby;
69     my $payinfo = $customer->payinfo;
70     my $daytime = $customer->daytime;
71     my $night   = $customer->night;
72
73     my ($paymonth, $payyear) = $customer->paydate_monthyear;
74     $paymonth--; # localtime() convention
75     $payday = 1; # This is enforced by FS::cust_main::check.
76     my $expire_time;
77     if($payby eq 'CARD' || $payby eq 'DCRD') {
78       # Credit cards expire at the end of the month/year.
79       if($paymonth == 11) {
80         $payyear++;
81         $paymonth = 0;
82       } else {
83         $paymonth++;
84       }
85       $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear) - 1;
86     }
87     else {
88       $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
89     }
90     
91     if (grep { $expire_time < $_date + $_ &&
92                $expire_time > $_date + $_ - $window_time } 
93                ($warning_time, $urgent_time, $panic_time) ) {
94       my $agentnum = $customer->agentnum;
95       $mail_sender = $conf->config('invoice_from', $agentnum);
96       $failure_recipient = $conf->config('invoice_from', $agentnum) 
97         || 'postmaster';
98       
99       my @alerter_template = $conf->config('alerter_template', $agentnum)
100         or die 'cannot load config file alerter_template';
101
102       my $alerter = new Text::Template(TYPE   => 'ARRAY',
103                                        SOURCE => [ 
104                                          map "$_\n", @alerter_template
105                                          ])
106         or die "can't create Text::Template object: $Text::Template::ERROR";
107
108       $alerter->compile()
109         or die "can't compile template: $Text::Template::ERROR";
110       
111       my @packages = $customer->ncancelled_pkgs;
112       if(@packages) {
113         my @invoicing_list = $customer->invoicing_list;
114         my @to_addrs = grep { $_ ne 'POST' } @invoicing_list;
115         if(@to_addrs) {
116           # Set up template fields.
117           my %fill_in;
118           $fill_in{$_} = $customer->getfield($_) 
119             foreach(qw(first last company));
120           $fill_in{'expdate'} = $expire_time;
121           $fill_in{'company_name'} = $conf->config('company_name', $agentnum);
122           $fill_in{'company_address'} =
123             join("\n",$conf->config('company_address',$agentnum))."\n";
124           if($payby eq 'CARD' || $payby eq 'DCRD') {
125             $fill_in{'payby'} = "credit card (".
126               substr($customer->payinfo, 0, 2) . "xxxxxxxxxx" .
127               substr($payinfo, -4) . ")";
128           }
129           elsif($payby eq 'COMP') {
130             $fill_in{'payby'} = 'complimentary account';
131           }
132           else {
133             $fill_in{'payby'} = 'current method';
134           }
135           # Send it already!
136           my $error = FS::Misc::send_email ( 
137             from    =>  $mail_sender,
138             to      =>  [ @to_addrs ],
139             subject =>  'Billing Arrangement Expiration',
140             body    =>  [ $alerter->fill_in( HASH => \%fill_in ) ],
141           );
142           die "can't send expiration alert: $error"
143             if $error;
144         } 
145         else { # if(@to_addrs)
146           push @{$agent_failure_body{$customer->agentnum}},
147             sprintf(qq{%5d %-32.32s %4s %10s %12s %12s},
148               $custnum,
149               $first . " " . $last . "   " . $company,
150               $payby,
151               $paydate,
152               $daytime,
153               $night );
154         }
155       } # if(@packages)
156     } # if(expired)
157   } # foreach(@customers)
158
159   # Failure notification
160   foreach my $agentnum (keys %agent_failure_body) {
161     $mail_sender = $conf->config('invoice_from', $agentnum)
162       if($conf->exists('invoice_from', $agentnum));
163     $failure_recipient = $conf->config('invoice_from', $agentnum)
164       if($conf->exists('invoice_from', $agentnum));
165     my $error = FS::Misc::send_email (
166       from    =>  $mail_sender,
167       to      =>  $failure_recipient,
168       subject =>  'Unnotified Billing Arrangement Expirations',
169       body    =>  [ @{$agent_failure_body{$agentnum}} ],
170       );
171     die "can't send alerter failure email to $failure_recipient: $error"
172       if $error;
173   }
174
175 }
176
177 1;