2015-12-31 10:16:30 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use autodie qw(open close);
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use Encode qw(encode decode);
|
2016-03-07 18:29:18 +00:00
|
|
|
use Email::MIME;
|
2015-12-31 10:16:30 +00:00
|
|
|
use Date::Manip::DM5;
|
|
|
|
use Supporters;
|
|
|
|
|
|
|
|
my $TODAY = UnixDate(ParseDate("today"), '%Y-%m-%d');
|
2016-03-07 16:25:46 +00:00
|
|
|
my $ONE_WEEK = UnixDate(DateCalc(ParseDate("today"), "+ 1 week"), '%Y-%m-%d');
|
2016-03-07 18:29:18 +00:00
|
|
|
my $ONE_MONTH = UnixDate(DateCalc(ParseDate("today"), "+ 1 month"), '%Y-%m-%d');
|
2015-12-31 10:16:30 +00:00
|
|
|
|
2016-01-23 01:12:22 +00:00
|
|
|
if (@ARGV < 7 ) {
|
|
|
|
print STDERR "usage: $0 <SUPPORTERS_SQLITE_DB_FILE> <REQUEST_NAME> <FROM_ADDRESS> <EMAIL_TEMPLATE> <MONTHLY_SEARCH_REGEX> <ANNUAL_SEARCH_REGEX> <VERBOSE> <LEDGER_CMD_LINE>\n";
|
2015-12-31 10:16:30 +00:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2016-01-23 01:12:22 +00:00
|
|
|
my($SUPPORTERS_SQLITE_DB_FILE, $REQUEST_NAME, $FROM_ADDRESS, $EMAIL_TEMPLATE, $MONTHLY_SEARCH_REGEX, $ANNUAL_SEARCH_REGEX, $VERBOSE,
|
2015-12-31 10:16:30 +00:00
|
|
|
@LEDGER_CMN_LINE) = @ARGV;
|
|
|
|
|
|
|
|
|
|
|
|
my $dbh = DBI->connect("dbi:SQLite:dbname=$SUPPORTERS_SQLITE_DB_FILE", "", "",
|
|
|
|
{ RaiseError => 1, sqlite_unicode => 1 })
|
|
|
|
or die $DBI::errstr;
|
|
|
|
|
|
|
|
my $sp = new Supporters($dbh, \@LEDGER_CMN_LINE, { monthly => $MONTHLY_SEARCH_REGEX, annual => $ANNUAL_SEARCH_REGEX});
|
|
|
|
|
|
|
|
my(@supporterIds) = $sp->findDonor({});
|
|
|
|
|
2016-03-07 18:29:18 +00:00
|
|
|
my %expireReport;
|
|
|
|
$expireReport{'02-lapsed'}{description} = "Already Lapsed Supporters";
|
|
|
|
$expireReport{'00-lapsing-this-week'}{description} = "Supporters Lapsing Within a Week";
|
|
|
|
$expireReport{'01-lapsing-this-month'}{description} = "Supporters Lapsing Within a Month";
|
|
|
|
|
|
|
|
my @lapseCategories = ('00-lapsing-this-week', '01-lapsing-this-month', '02-lapsed');
|
|
|
|
foreach my $cat (@lapseCategories) {
|
|
|
|
$expireReport{$cat}{list} = [];
|
|
|
|
}
|
2016-03-07 18:34:46 +00:00
|
|
|
my $lapsedCount = 0;
|
2016-03-07 18:29:18 +00:00
|
|
|
|
2015-12-31 10:16:30 +00:00
|
|
|
foreach my $supporterId (@supporterIds) {
|
|
|
|
my $expiresOn = $sp->supporterExpirationDate($supporterId);
|
2016-03-07 16:25:46 +00:00
|
|
|
my $isLapsed = ( (not defined $expiresOn) or $expiresOn le $TODAY);
|
|
|
|
my $lapsesInOneWeek = ( (defined $expiresOn) and $expiresOn le $ONE_WEEK);
|
2016-03-07 18:29:18 +00:00
|
|
|
my $lapsesInOneMonth = ( (defined $expiresOn) and $expiresOn le $ONE_MONTH);
|
|
|
|
$expiresOn = "NO-FULL-SIGNUP" if not defined $expiresOn;
|
2016-03-07 18:34:46 +00:00
|
|
|
$lapsedCount++ if $isLapsed;
|
2016-03-07 18:29:18 +00:00
|
|
|
my %emails;
|
|
|
|
my $email = $sp->getPreferredEmailAddress($supporterId);
|
|
|
|
if (defined $email) {
|
|
|
|
$emails{$email} = {};
|
|
|
|
} else {
|
|
|
|
%emails = $sp->getEmailAddresses($supporterId);
|
|
|
|
}
|
|
|
|
my(@emails) = keys(%emails);
|
|
|
|
|
|
|
|
my $lastDonateDate = $sp->donorLastGave($supporterId);
|
|
|
|
|
|
|
|
my $cat;
|
|
|
|
if ($isLapsed) { $cat = '02-lapsed';} elsif ($lapsesInOneWeek) { $cat = '00-lapsing-this-week' }
|
|
|
|
elsif ($lapsesInOneMonth) { $cat = '01-lapsing-this-month'; }
|
|
|
|
|
|
|
|
push(@{$expireReport{$cat}{list}}, { expiresOn => $expiresOn, displayName => $sp->getDisplayName($supporterId),
|
|
|
|
ledgerEntityId => $sp->getLedgerEntityId($supporterId), supporterId => $supporterId,
|
|
|
|
emails => \@emails })
|
|
|
|
if defined $cat;
|
2015-12-31 10:16:30 +00:00
|
|
|
|
2016-01-03 21:17:00 +00:00
|
|
|
my $request = $sp->getRequest({ donorId => $supporterId, requestType => $REQUEST_NAME});
|
|
|
|
|
|
|
|
if (defined $request) {
|
|
|
|
if (defined $request->{fulfillDate}) {
|
|
|
|
print STDERR "$supporterId lapsed on $expiresOn but recorded as renewed on $request->{fulfillDate}\n"
|
2016-03-07 16:25:46 +00:00
|
|
|
if ( ($isLapsed or $lapsesInOneWeek) and $VERBOSE);
|
|
|
|
} elsif ( (not $isLapsed) and (not $lapsesInOneWeek)) {
|
2016-01-03 21:17:00 +00:00
|
|
|
$sp->fulfillRequest({donorId => $supporterId, requestType => $REQUEST_NAME,
|
|
|
|
who => $supporterId, how => "apparent renewal not noticed during import"});
|
|
|
|
print STDERR "$supporterId now expires on $expiresOn, recording rewnewal of type $REQUEST_NAME\n"
|
|
|
|
if $VERBOSE;
|
|
|
|
} else {
|
|
|
|
print STDERR "$supporterId received this renewal notice already on $request->{requestDate}\n"
|
|
|
|
if $VERBOSE;
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
}
|
2016-03-07 16:25:46 +00:00
|
|
|
print STDERR "$supporterId skipped since he is not lapsed\n" if ( (not $isLapsed and not $lapsesInOneWeek) and $VERBOSE > 1);
|
|
|
|
next unless $isLapsed or $lapsesInOneWeek;
|
2016-01-23 01:12:05 +00:00
|
|
|
|
2015-12-31 10:16:30 +00:00
|
|
|
|
|
|
|
open(MESSAGE, "<", $EMAIL_TEMPLATE);
|
|
|
|
my @message;
|
|
|
|
while (my $line = <MESSAGE> ) {
|
|
|
|
$line =~ s/FIXME_LAST_DONATE_DATE/$lastDonateDate/g;
|
|
|
|
push(@message, $line);
|
|
|
|
}
|
|
|
|
close MESSAGE;
|
2016-03-07 18:29:18 +00:00
|
|
|
my $emailTo = join(' ', @emails);
|
2015-12-31 10:16:30 +00:00
|
|
|
open(SENDMAIL, "|/usr/lib/sendmail -f \"$FROM_ADDRESS\" -oi -oem -- $emailTo $FROM_ADDRESS") or
|
|
|
|
die "unable to run sendmail: $!";
|
|
|
|
|
2016-03-07 16:25:46 +00:00
|
|
|
print STDERR "Sending to $supporterId at $emailTo who expires on $expiresOn\n";
|
2016-03-07 18:29:18 +00:00
|
|
|
print SENDMAIL "To: ", join(', ', @emails), "\n";
|
2015-12-31 10:16:30 +00:00
|
|
|
print SENDMAIL @message;
|
|
|
|
|
|
|
|
close SENDMAIL;
|
|
|
|
sleep 1;
|
|
|
|
$sp->addRequest({donorId => $supporterId, requestType => $REQUEST_NAME});
|
|
|
|
}
|
2016-03-07 18:29:18 +00:00
|
|
|
|
2016-03-07 18:34:46 +00:00
|
|
|
my $subject = "Supporter lapsed report for $TODAY";
|
|
|
|
my $per = ( ($lapsedCount / scalar(@supporterIds)) * 100.00);
|
|
|
|
my $emailText = "$subject\n" . ("=" x length($subject)) .
|
|
|
|
"\n\nWe have " . scalar(@supporterIds) . " supporters and $lapsedCount are lapsed. That's " .
|
|
|
|
sprintf("%.2f", $per) . "%.\nActive supporter count: " . (scalar(@supporterIds) - $lapsedCount) . "\n\n";
|
|
|
|
|
2016-03-07 18:29:18 +00:00
|
|
|
foreach my $cat (sort { $a cmp $b } @lapseCategories) {
|
|
|
|
my $heading = scalar(@{$expireReport{$cat}{list}}) . " " . $expireReport{$cat}{description};
|
|
|
|
$emailText .= "$heading\n";
|
|
|
|
$emailText .= "-" x length($heading);
|
|
|
|
$emailText .= "\n";
|
|
|
|
foreach my $sup (sort { $a->{expiresOn} cmp $b->{expiresOn} } @{$expireReport{$cat}{list}}) {
|
|
|
|
$emailText .= " $sup->{expiresOn}: $sup->{supporterId}, $sup->{ledgerEntityId}, $sup->{displayName}, ";
|
|
|
|
$emailText .= join(", ", @{$sup->{emails}});
|
|
|
|
$emailText .= "\n";
|
|
|
|
}
|
|
|
|
$emailText .= "\n";
|
|
|
|
}
|
|
|
|
my $email = Email::MIME->create(
|
|
|
|
header_str => [
|
|
|
|
To => $FROM_ADDRESS,
|
|
|
|
From => $FROM_ADDRESS,
|
2016-03-07 18:34:46 +00:00
|
|
|
Subject => $subject ],
|
2016-03-07 18:29:18 +00:00
|
|
|
attributes => {
|
|
|
|
content_type => 'text/plain',
|
|
|
|
charset => 'utf-8',
|
|
|
|
encoding => "quoted-printable",
|
|
|
|
disposition => 'inline' },
|
|
|
|
body_str => $emailText);
|
|
|
|
open(SENDMAIL, "|/usr/lib/sendmail -f \"$FROM_ADDRESS\" -oi -oem -- $FROM_ADDRESS") or
|
|
|
|
die "unable to run sendmail: $!";
|
|
|
|
print SENDMAIL $email->as_string;
|
|
|
|
close SENDMAIL;
|