summaryrefslogtreecommitdiff
path: root/FS/FS/Cron/alert_expiration.pm
blob: 364fc60c75ad88b09db99a9855fe0011dae61feb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package FS::Cron::alert_expiration;

use vars qw( @ISA @EXPORT_OK);
use Exporter;
use FS::Record qw(qsearch qsearchs);
use FS::Conf;
use FS::cust_main;
use FS::Misc;
use Time::Local;
use Date::Parse qw(str2time);


@ISA = qw( Exporter );
@EXPORT_OK = qw( alert_expiration );

my $warning_time = 30 * 24 * 60 * 60;
my $urgent_time = 15 * 24 * 60 * 60;
my $panic_time = 5 * 24 * 60 * 60;
my $window_time = 24 * 60 * 60;

sub alert_expiration {
  my $conf = new FS::Conf;
  my $smtpmachine = $conf->config('smtpmachine');
  
  my %opt = @_;
  my ($_date) = $opt{'d'} ? str2time($opt{'d'}) : $^T;
  $_date += $opt{'y'} * 86400 if $opt{'y'};
  my ($sec, $min, $hour, $mday, $mon, $year) = (localtime($_date)) [0..5];
  $mon++;

  my $debug = 0;
  $debug = 1 if $opt{'v'};
  $debug = $opt{'l'} if $opt{'l'};

  $FS::cust_main::DEBUG = $debug;

  # Get a list of customers.
 
  my %limit;
  $limit{'agentnum'} = $opt{'a'} if $opt{'a'};
  $limit{'payby'}    = $opt{'p'} if $opt{'p'};

  my @customers;

  if(my @custnums = @ARGV) {
    # We're given an explicit list of custnums, so select those.  Then check against 
    # -a and -p to avoid doing anything unexpected.
    foreach (@custnums) {
      my $customer = FS::cust_main->by_key($_);
      if($customer and (!$opt{'a'} or $customer->agentnum == $opt{'a'})
                   and (!$opt{'p'} or $customer->payby eq $opt{'p'}) ) {
        push @customers, $customer;
      }
    }
  }
  else { # no @ARGV
    @customers = qsearch('cust_main', \%limit);
  }
  return if(!@customers);
  foreach my $customer (@customers) {
    next if !($customer->ncancelled_pkgs); # skip inactive customers
    my $paydate = $customer->paydate;
    next if $paydate =~ /^\s*$/; # skip empty expiration dates
    
    my $custnum = $customer->custnum;
    my $first   = $customer->first;
    my $last    = $customer->last;
    my $company = $customer->company;
    my $payby   = $customer->payby;
    my $payinfo = $customer->payinfo;
    my $daytime = $customer->daytime;
    my $night   = $customer->night;

    my ($paymonth, $payyear) = $customer->paydate_monthyear;
    $paymonth--; # localtime() convention
    $payday = 1; # This is enforced by FS::cust_main::check.
    my $expire_time;
    if($payby eq 'CARD' || $payby eq 'DCRD') {
      # Credit cards expire at the end of the month/year.
      if($paymonth == 11) {
        $payyear++;
        $paymonth = 0;
      } else {
        $paymonth++;
      }
      $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear) - 1;
    }
    else {
      $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
    }
    
    if (grep { $expire_time < $_date + $_ &&
               $expire_time > $_date + $_ - $window_time } 
               ($warning_time, $urgent_time, $panic_time) ) {
      # Send an expiration notice.
      my $agentnum = $customer->agentnum;
      my $error = '';

      my $msgnum = $conf->config('alerter_msgnum', $agentnum);
      if ( $msgnum ) { # new hotness
        my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
        $error = $msg_template->send('cust_main' => $customer);
      }
      else { #!$msgnum, the hard way
        $mail_sender = $conf->config('invoice_from', $agentnum);
        $failure_recipient = $conf->config('invoice_from', $agentnum) 
          || 'postmaster';
       
        my @alerter_template = $conf->config('alerter_template', $agentnum)
          or die 'cannot load config file alerter_template';

        my $alerter = new Text::Template(TYPE   => 'ARRAY',
                                         SOURCE => [ 
                                           map "$_\n", @alerter_template
                                           ])
          or die "can't create Text::Template object: $Text::Template::ERROR";

        $alerter->compile()
          or die "can't compile template: $Text::Template::ERROR";
        
        my @invoicing_list = $customer->invoicing_list;
        my @to_addrs = grep { $_ ne 'POST' } @invoicing_list;
        if(@to_addrs) {
          # Set up template fields.
          my %fill_in;
          $fill_in{$_} = $customer->getfield($_) 
            foreach(qw(first last company));
          $fill_in{'expdate'} = $expire_time;
          $fill_in{'company_name'} = $conf->config('company_name', $agentnum);
          $fill_in{'company_address'} =
            join("\n",$conf->config('company_address',$agentnum))."\n";
          if($payby eq 'CARD' || $payby eq 'DCRD') {
            $fill_in{'payby'} = "credit card (".
              substr($customer->payinfo, 0, 2) . "xxxxxxxxxx" .
              substr($payinfo, -4) . ")";
          }
          elsif($payby eq 'COMP') {
            $fill_in{'payby'} = 'complimentary account';
          }
          else {
            $fill_in{'payby'} = 'current method';
          }
          # Send it already!
          $error = FS::Misc::send_email ( 
            from    =>  $mail_sender,
            to      =>  [ @to_addrs ],
            subject =>  'Billing Arrangement Expiration',
            body    =>  [ $alerter->fill_in( HASH => \%fill_in ) ],
          );
      } 
      else { # if(@to_addrs)
        push @{$agent_failure_body{$customer->agentnum}},
          sprintf(qq{%5d %-32.32s %4s %10s %12s %12s},
            $custnum,
            $first . " " . $last . "   " . $company,
            $payby,
            $paydate,
            $daytime,
            $night );
      }
    } # if($msgnum)
    
# should we die here rather than report failure as below?
    die "can't send expiration alert: $error"
      if $error;
    
    } # if(expired)
  } # foreach(@customers)

  # Failure notification
  foreach my $agentnum (keys %agent_failure_body) {
    $mail_sender = $conf->config('invoice_from', $agentnum)
      if($conf->exists('invoice_from', $agentnum));
    $failure_recipient = $conf->config('invoice_from', $agentnum)
      if($conf->exists('invoice_from', $agentnum));
    my $error = FS::Misc::send_email (
      from    =>  $mail_sender,
      to      =>  $failure_recipient,
      subject =>  'Unnotified Billing Arrangement Expirations',
      body    =>  [ @{$agent_failure_body{$agentnum}} ],
      );
    die "can't send alerter failure email to $failure_recipient: $error"
      if $error;
  }

}

1;