supporters/scripts/send-renewal-notices.plx

322 lines
13 KiB
Text
Raw Normal View History

2015-12-31 10:16:30 +00:00
#!/usr/bin/perl
use strict;
use warnings;
use Encode;
use utf8;
2015-12-31 10:16:30 +00:00
use autodie qw(open close);
use DBI;
use Encode qw(encode decode);
use Encode qw(encode decode);
use Email::MIME::RFC2047::Encoder;
use Email::MIME;
2015-12-31 10:16:30 +00:00
use Date::Manip::DM5;
use Supporters;
my $encoder = Email::MIME::RFC2047::Encoder->new();
use LaTeX::Encode;
my $BIG_DONOR_CUTOFF = 500.00;
2015-12-31 10:16:30 +00:00
my $TODAY = UnixDate(ParseDate("today"), '%Y-%m-%d');
my $ONE_WEEK = UnixDate(DateCalc(ParseDate("today"), "+ 1 week"), '%Y-%m-%d');
my $ONE_MONTH = UnixDate(DateCalc(ParseDate("today"), "+ 1 month"), '%Y-%m-%d');
my $TWO_MONTHS_AGO = UnixDate(DateCalc(ParseDate("today"), "- 2 months"), '%Y-%m-%d');
my $ONE_YEAR_AGO = UnixDate(DateCalc(ParseDate("today"), "- 1 year"), '%Y-%m-%d');
my $TWO_YEARS_AGO = UnixDate(DateCalc(ParseDate("today"), "- 2 years"), '%Y-%m-%d');
my $THREE_YEARS_AGO = UnixDate(DateCalc(ParseDate("today"), "- 3 years"), '%Y-%m-%d');
2015-12-31 10:16:30 +00:00
if (@ARGV < 8 ) {
print STDERR "usage: $0 <SUPPORTERS_SQLITE_DB_FILE> <HOW_FAR_IN_ADVANCE> <REQUEST_NAME> <FROM_ADDRESS> <ALL_STAFF_ADDRESS> <EMAIL_TEMPLATE> <MONTHLY_SEARCH_REGEX> <ANNUAL_SEARCH_REGEX> <VERBOSE> <LEDGER_CMD_LINE>\n";
2015-12-31 10:16:30 +00:00
exit 1;
}
my($SUPPORTERS_SQLITE_DB_FILE, $HOW_FAR_IN_ADVANCE_CALC, $REQUEST_NAME, $FROM_ADDRESS, $ALL_STAFF_ADDRESS, $EMAIL_TEMPLATE, $MONTHLY_SEARCH_REGEX, $ANNUAL_SEARCH_REGEX, $VERBOSE,
2015-12-31 10:16:30 +00:00
@LEDGER_CMN_LINE) = @ARGV;
my $HOW_FAR_IN_ADVANCE = UnixDate(DateCalc(ParseDate("today"), "+ " . $HOW_FAR_IN_ADVANCE_CALC), '%Y-%m-%d');
die "Unable to compute how far \"$HOW_FAR_IN_ADVANCE_CALC\" is from today" unless defined $HOW_FAR_IN_ADVANCE and $HOW_FAR_IN_ADVANCE =~ /[0-9]+\-[0-9]+\-[0-9]+/;
print "$HOW_FAR_IN_ADVANCE is how far in advance we're sending\n";
2015-12-31 10:16:30 +00:00
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({});
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} = [];
}
my $lapsedCount = 0;
my(%activeCounter, %lapsedCounter);
2016-09-07 23:05:06 +00:00
my %monthExpirations;
my $isPaper = 0;
$isPaper = 1 if $EMAIL_TEMPLATE =~ /\.tex$/;
if ($isPaper) {
open(ENVELOPES, ">envelopes-ready-to-send.tex") or die "unable to open labels: $!";
}
my $totalSupporters = 0;
my $sentCount = 0;
foreach my $supporterId (sort @supporterIds) {
next unless $sp->isSupporter($supporterId);
$totalSupporters++;
2015-12-31 10:16:30 +00:00
my $expiresOn = $sp->supporterExpirationDate($supporterId);
2016-09-07 23:05:06 +00:00
my $expiresOnMonth = UnixDate(ParseDate($expiresOn), '%Y-%m');
{ no warnings 'uninitialized'; $monthExpirations{$expiresOnMonth}++; }
my $isLapsed = ( (not defined $expiresOn) or $expiresOn le $TODAY);
my $lapsesInOneWeek = ( (defined $expiresOn) and $expiresOn le $ONE_WEEK);
my $lapsesInOneMonth = ( (defined $expiresOn) and $expiresOn le $ONE_MONTH);
my $type = $sp->getType($supporterId);
# Lapses soon calculation is complicated. Annuals can use the how far in
# advance setting, but we really can't with monthlies. For all we know, a
# monthly donation is about to come in for them, so until they reach their
# 60-day expired mark, which is already included in the expiration date by
# the library, we have to assume they're donation is coming in.
my $lapsesSoon;
if (defined $expiresOn) {
$lapsesSoon = ($expiresOn le $HOW_FAR_IN_ADVANCE) if ($type =~ /ann/i);
$lapsesSoon = ($expiresOn lt $TODAY) if ($type =~ /month/i);
}
$expiresOn = "NO-FULL-SIGNUP" if not defined $expiresOn;
if ($isLapsed) {
$lapsedCount++;
$lapsedCounter{$type}++ if defined $type;
} else {
$activeCounter{$type}++ if defined $type;
}
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"
if ( ($isLapsed or $lapsesSoon) and $VERBOSE);
2016-01-03 21:17:00 +00:00
} else {
print STDERR "$supporterId received this renewal notice already on $request->{requestDate}\n"
if $VERBOSE;
}
next;
}
print STDERR "$supporterId skipped since he is not lapsed\n" if ( (not $isLapsed and not $lapsesSoon) and $VERBOSE > 1);
next unless $isLapsed or $lapsesSoon;
2016-01-23 01:12:05 +00:00
my $displayName = $sp->getDisplayName($supporterId);
if ($isPaper) {
2017-01-02 22:53:08 +00:00
next unless $sp->paperMailOk($supporterId);
my $latexDisplayName = latex_encode($displayName);
$latexDisplayName =~ s/\\unmatched\{0141\}/\L{}/g;
$latexDisplayName =~ s/\\unmatched\{0142\}/\l{}/g;
if ($latexDisplayName =~ /unmatched/) {
print "Skipping $supporterId because the address has characters I can't print in LaTeX\n name was: ", encode('UTF-8', $displayName), "\n";
next;
}
my $postalAddress = $sp->getPreferredPostalAddress($supporterId);
if (not defined $postalAddress) {
my(@postalAddresses) = $sp->getPostalAddresses($supporterId);
$postalAddress = $postalAddresses[0];
}
if ( (not defined $postalAddress) or $postalAddress =~ /^\s*$/m or $postalAddress eq $displayName) {
print "Skipping $supporterId because no postal address was available\n";
next;
}
2015-12-31 10:16:30 +00:00
my $latexPostal = latex_encode($postalAddress);
$latexPostal =~ s/\\unmatched\{0141\}/\L{}/g;
$latexPostal =~ s/\\unmatched\{0142\}/\l{}/g;
if ($latexPostal =~ /unmatched/) {
print "Skipping $supporterId because the address has characters the post office will not accept\n Address was: ", encode('UTF-8', $postalAddress), "\n";
next;
}
$latexPostal = join(' \\\\ ', split('\n', $latexPostal));
print ENVELOPES '\mlabel{}{TO: \\\\ ' . $latexPostal . "}\n";
open(MESSAGE, "<", $EMAIL_TEMPLATE);
open(LETTER, ">", sprintf("%4.4d", $sentCount++) . "-" . $supporterId . ".tex");
while (my $line = <MESSAGE> ) {
$line =~ s/FIXME-LAST-DONATE-DATE/$lastDonateDate/g;
$line =~ s/FIXME-ADDRESS/$latexPostal/g;
$line =~ s/FIXME-FULL-NAME/$latexDisplayName/g;
print LETTER $line;
}
close LETTER;
close MESSAGE;
} else {
2017-01-02 22:53:08 +00:00
next unless $sp->emailOk($supporterId);
open(MESSAGE, "<", $EMAIL_TEMPLATE);
my @message;
while (my $line = <MESSAGE> ) {
$line =~ s/FIXME_LAST_DONATE_DATE/$lastDonateDate/g;
push(@message, $line);
}
close MESSAGE;
my $fullEmailLine = "";
my $emailTo = join(' ', @emails);
my $displayName = $sp->getDisplayName($supporterId);
foreach my $email (@emails) {
$fullEmailLine .= ", " if ($fullEmailLine ne "");
my $line = "";
if (defined $displayName) {
$line .= $encoder->encode_phrase($displayName) . " ";
}
$line .= "<$email>";
$fullEmailLine .= $line;
}
open(SENDMAIL, "|/usr/lib/sendmail -f \"$FROM_ADDRESS\" -oi -oem -- $emailTo $FROM_ADDRESS") or
die "unable to run sendmail: $!";
print STDERR "Sending to $supporterId at $emailTo who expires on $expiresOn\n";
print SENDMAIL "To: $fullEmailLine\n";
print SENDMAIL @message;
close SENDMAIL;
sleep 1;
}
2015-12-31 10:16:30 +00:00
$sp->addRequest({donorId => $supporterId, requestType => $REQUEST_NAME});
}
if ($isPaper) {
close ENVELOPES;
exit 0;
}
my $subject = "Supporter lapsed report for $TODAY";
2016-12-05 20:38:35 +00:00
my $per = ( ($lapsedCount / $totalSupporters) * 100.00);
my $headerInfo = "$subject\n" . ("=" x length($subject)) .
2016-12-05 20:38:35 +00:00
"\n\nWe have " . $totalSupporters . " supporters and $lapsedCount are lapsed. That's " .
sprintf("%.2f", $per) . "%.\nActive supporter count: " . ($totalSupporters - $lapsedCount) . "\n" .
sprintf(" Of the active supporters, %.2f%% are monthly and %.2f%% are annual",
2016-12-05 20:38:35 +00:00
( ($activeCounter{Monthly} / ($totalSupporters - $lapsedCount)) * 100.00),
( ($activeCounter{Annual} / ($totalSupporters - $lapsedCount)) * 100.00)) . ".\n\n";
foreach my $type (keys %lapsedCounter) {
$headerInfo .= sprintf("%7s: Lapsed Count: %3d Active Count: %3d Percent of %7s Lapsed: %2.2f\n",
$type, $lapsedCounter{$type}, $activeCounter{$type}, $type, ($lapsedCounter{$type} / ($lapsedCounter{$type} + $activeCounter{$type})) * 100.00);
}
$headerInfo .= "\n";
my $emailText .= $headerInfo;
my $allStaffEmailText = $headerInfo;
my $bigDonorEmailText = "\n LAPSED BIG DONORS\n" .
" =================\n";
$emailText .= "\n RENEWAL DUE COUNT BY MONTH\n";
$emailText .= "\n ==========================\n";
$allStaffEmailText .= "\n RENEWAL DUE COUNT BY MONTH\n";
$allStaffEmailText .= "\n ==========================\n";
foreach my $month (sort { $a cmp $b } keys %monthExpirations) {
my $xx = sprintf("$month: %5d\n", $monthExpirations{$month});
$emailText .= $xx;
$allStaffEmailText .= $xx;
}
$emailText .= "\n";
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";
$allStaffEmailText .= "$heading\n";
foreach my $sup (sort { ($cat eq '02-lapsed') ? ($b->{expiresOn} cmp $a->{expiresOn})
: ($a->{expiresOn} cmp $b->{expiresOn}) }
@{$expireReport{$cat}{list}}) {
my $threeYearTot = $sp->donorTotalGaveInPeriod(donorId => $sup->{supporterId},
startDate => $THREE_YEARS_AGO, endDate => $TODAY);
my $twoYearTot = $sp->donorTotalGaveInPeriod(donorId => $sup->{supporterId},
startDate => $TWO_YEARS_AGO, endDate => $TODAY);
my $oneYearTot = $sp->donorTotalGaveInPeriod(donorId => $sup->{supporterId},
startDate => $ONE_YEAR_AGO, endDate => $TODAY);
$emailText .= " $sup->{expiresOn}: $sup->{supporterId}, $sup->{ledgerEntityId}, $sup->{displayName}, ";
$emailText .= "2YrTot: \$" . sprintf("%.2f", $twoYearTot). ", 3YrTot: \$" . sprintf("%.2f", $threeYearTot);
$emailText .= ", Emails: " . join(", ", @{$sup->{emails}});
$emailText .= "\n";
if ( ($threeYearTot / 3) > $BIG_DONOR_CUTOFF or ($twoYearTot / 2) > $BIG_DONOR_CUTOFF or
$oneYearTot > $BIG_DONOR_CUTOFF) {
$bigDonorEmailText .= " $sup->{expiresOn}: $sup->{supporterId}, $sup->{ledgerEntityId}, $sup->{displayName}, ";
$bigDonorEmailText .= "1YrTot: \$" . sprintf("%.2f", $oneYearTot) . "2YrTot: \$" .
sprintf("%.2f", $twoYearTot). ", 3YrTot: \$" . sprintf("%.2f", $threeYearTot);
$bigDonorEmailText .= ", Emails: " . join(", ", @{$sup->{emails}}) . "\n";
}
}
$emailText .= "\n";
}
2016-09-07 23:05:06 +00:00
my $email = Email::MIME->create(
header_str => [
To => $FROM_ADDRESS,
From => $FROM_ADDRESS,
Subject => $subject ],
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;
my $allStaffEmail = Email::MIME->create(
header_str => [
To => $ALL_STAFF_ADDRESS,
From => $FROM_ADDRESS,
Subject => $subject ],
attributes => {
content_type => 'text/plain',
charset => 'utf-8',
encoding => "quoted-printable",
disposition => 'inline' },
body_str => $allStaffEmailText);
open(SENDMAIL, "|/usr/lib/sendmail -f \"$FROM_ADDRESS\" -oi -oem -- $ALL_STAFF_ADDRESS") or
die "unable to run sendmail: $!";
print SENDMAIL $allStaffEmail->as_string;
close SENDMAIL;
my $bigDonorLapsedEmail = Email::MIME->create(
header_str => [
To => $FROM_ADDRESS,
From => $FROM_ADDRESS,
Subject => "Big Donors Lapsed/Lapsing Soon (for $TODAY)" ],
attributes => {
content_type => 'text/plain',
charset => 'utf-8',
encoding => "quoted-printable",
disposition => 'inline' },
body_str => $bigDonorEmailText);
open(SENDMAIL, "|/usr/lib/sendmail -f \"$FROM_ADDRESS\" -oi -oem -- $FROM_ADDRESS") or
die "unable to run sendmail: $!";
print SENDMAIL $bigDonorLapsedEmail->as_string;
close SENDMAIL;