summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-expiration-alerter
blob: ffd75f9a5a12907a94d980c311c8daa93837955b (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/perl -Tw

use strict;
use Date::Format;
use Time::Local;
use Text::Template;
use Getopt::Std;
use Net::SMTP;
use Mail::Header;
use Mail::Internet;
use FS::Conf;
use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearch);
use FS::cust_main;

use vars qw($smtpmachine @body);

#hush, perl!
$FS::alerter::_template::first = "";
$FS::alerter::_template::last = "";
$FS::alerter::_template::company = "";
$FS::alerter::_template::payby = "";
$FS::alerter::_template::expdate = "";

# Set the mail program  and other variables
my $mail_sender = "billing\@mydomain.tld";  # or invoice_from if available
my $failure_recipient = "postmaster";       # or invoice_from if available
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;

&untaint_argv;	#what it sounds like  (eww)

#we're at now now (and later).
my($_date)= $^T;

# Get the current month
my ($sec,$min,$hour,$mday,$mon,$year) =
	(localtime($_date) )[0,1,2,3,4,5]; 
$mon++;

# Login to the database
my $user = shift or die &usage;
adminsuidsetup $user;

# Get the needed configuration files
my $conf = new FS::Conf;
$smtpmachine = $conf->config('smtpmachine');
$mail_sender = $conf->config('invoice_from')
  if $conf->exists('invoice_from');
$failure_recipient = $conf->config('invoice_from')
  if $conf->exists('invoice_from');


my(@customers)=qsearch('cust_main',{});
if (scalar(@customers) == 0)
{
  exit 1;
}

# Prepare for sending email

$ENV{MAILADDRESS} = $mail_sender;
my $header = new Mail::Header ( [
  "From: Account Processor",
  "To: $failure_recipient",
  "Sender: $mail_sender",
  "Reply-To: $mail_sender",
  "Subject: Unnotified Billing Arrangement Expirations",
] );

my @alerter_template = $conf->config('alerter_template')
  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 new Text::Template object:  Text::Template::ERROR";
$alerter->compile() or die "can't compile template:  Text::Template::ERROR";

# Now I can start looping
foreach my $customer (@customers)
{
  my $paydate = $customer->getfield('paydate');
  next if $paydate =~ /^\s*$/; #skip empty expiration dates

  my $custnum = $customer->getfield('custnum');
  my $first = $customer->getfield('first');
  my $last = $customer->getfield('last');
  my $company = $customer->getfield('company');
  my $payby = $customer->getfield('payby');
  my $payinfo = $customer->getfield('payinfo');
  my $daytime = $customer->getfield('daytime');
  my $night = $customer->getfield('night');

  my ($payyear,$paymonth,$payday) = split (/-/,$paydate);

  my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);

  #credit cards expire at the end of the month/year of their exp date
  if ($payby eq 'CARD' || $payby eq 'DCRD') {
    ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
    $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
    $expire_time--;
  }

  if (($expire_time < $_date + $warning_time &&
    $expire_time > $_date + $warning_time - $window_time) ||
      ($expire_time < $_date + $urgent_time &&
       $expire_time > $_date + $urgent_time - $window_time) ||
      ($expire_time < $_date + $panic_time &&
       $expire_time > $_date + $panic_time - $window_time)) {



    my @packages = $customer->ncancelled_pkgs;
    if (scalar(@packages) != 0) {
      my @invoicing_list = $customer->invoicing_list;
      if ( grep { $_ ne 'POST' } @invoicing_list ) { 
        my $header = new Mail::Header ( [
          "From: $mail_sender",
          "To: ". join(', ', grep { $_ ne 'POST' } @invoicing_list ),
          "Sender: $mail_sender",
          "Reply-To: $mail_sender",
          "Date: ". time2str("%a, %d %b %Y %X %z", time),
          "Subject: Billing Arrangement Expiration",
        ] );
        $FS::alerter::_template::first = $first;
        $FS::alerter::_template::last = $last;
        $FS::alerter::_template::company = $company;
        if ($payby eq 'CARD' || $payby eq 'DCRD') {
          $FS::alerter::_template::payby = "credit card (" .
            substr($payinfo, 0, 2) . "xxxxxxxxxx" .
            substr($payinfo, -4) . ")";
        }elsif ($payby eq 'COMP') {
          $FS::alerter::_template::payby = "complimentary account";
        }else{
          $FS::alerter::_template::payby = "current method";
        }
        $FS::alerter::_template::expdate = $expire_time;

        $FS::alerter::_template::company_name = $conf->config('company_name');
        $FS::alerter::_template::company_address =
          join("\n", $conf->config('company_address') ). "\n";

        my $message = new Mail::Internet (
          'Header' => $header,
          'Body' => [ $alerter->fill_in( PACKAGE => 'FS::alerter::_template' ) ],
        );
        $!=0;
        $message->smtpsend( Host => $smtpmachine )
          or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
            or die "Can't send expiration email: $!";

      } elsif ( ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list ) { 
        push @body, sprintf(qq{%5d %-32.32s %4s %10s %12s %12s},
          $custnum,
          $first . " " . $last . "   " . $company,
          $payby,
          $paydate,
          $daytime,
          $night);
      }
    }
  }
}

# Now I need to send EMAIL
if (scalar(@body)) {
  my $message = new Mail::Internet (
    'Header' => $header,
    'Body' => [ (@body) ],
  );
  $!=0;
  $message->smtpsend( Host => $smtpmachine )
    or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
      or die "can't send alerter failure email to $failure_recipient".
             " via server $smtpmachine with SMTP: $!";
}

# subroutines
sub untaint_argv {
  foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
    $ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal argument \"$ARGV[$_]\"";
    $ARGV[$_]=$1;
  }
}

sub usage {
  die "Usage:\n\n  freeside-expiration-alerter user\n";
}

=head1 NAME

freeside-expiration-alerter - Emails notifications of credit card expirations.

=head1 SYNOPSIS

  freeside-expiration-alerter user

=head1 DESCRIPTION

Emails customers notice that their credit card or other billing arrangement
is about to expire.  Usually run as a cron job.

user: From the mapsecrets file - see config.html from the base documentation

=head1 BUGS

Yes..... Use at your own risk. No guarantees or warrantees of any
kind apply to this program. Parts of this program are hacked from
other GNU licensed software created mainly by Ivan Kohler.

This is released under the GNU Public License. See www.gnu.org
for more information regarding this license.

=head1 SEE ALSO

L<FS::cust_main>, config.html from the base documentation

=head1 AUTHOR

Jeff Finucane <jeff@cmh.net>

=cut