2016-01-03 22:14:04 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use autodie qw(open close);
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use Encode qw(encode decode);
|
|
|
|
use Date::Manip::DM5;
|
|
|
|
use Supporters;
|
|
|
|
|
2016-12-08 01:10:11 +00:00
|
|
|
my $SPECIAL_THRESHOLD = 10_001;
|
|
|
|
|
2016-01-03 22:14:04 +00:00
|
|
|
my $TODAY = UnixDate(ParseDate("today"), '%Y-%m-%d');
|
2016-12-08 01:10:11 +00:00
|
|
|
my $ONE_YEAR_AGO = UnixDate(DateCalc(ParseDate("today"), "- 1 years"), '%Y-%m-%d');
|
2016-01-03 22:14:04 +00:00
|
|
|
|
|
|
|
if (@ARGV < 5 ) {
|
|
|
|
print STDERR "usage: $0 <SUPPORTERS_SQLITE_DB_FILE> <MONTHLY_SEARCH_REGEX> <ANNUAL_SEARCH_REGEX> <VERBOSE> <LEDGER_CMD_LINE>\n";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
my($SUPPORTERS_SQLITE_DB_FILE, $MONTHLY_SEARCH_REGEX, $ANNUAL_SEARCH_REGEX, $VERBOSE,
|
|
|
|
@LEDGER_CMD_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_CMD_LINE, { monthly => $MONTHLY_SEARCH_REGEX, annual => $ANNUAL_SEARCH_REGEX});
|
|
|
|
my(@supporterIds) = $sp->findDonor({});
|
|
|
|
|
|
|
|
my $lapsedCount = 0;
|
2016-12-08 01:10:11 +00:00
|
|
|
my $yearTot = 0.00;
|
|
|
|
my %specialContributions;
|
2016-01-03 22:14:04 +00:00
|
|
|
foreach my $supporterId (@supporterIds) {
|
|
|
|
my $expiresOn = $sp->supporterExpirationDate($supporterId);
|
2024-11-03 23:50:51 +00:00
|
|
|
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.
|
2016-01-03 22:14:04 +00:00
|
|
|
my $isLapsed = ( (not defined $expiresOn) or $expiresOn lt $TODAY);
|
2024-11-03 23:50:51 +00:00
|
|
|
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;
|
2016-12-08 01:10:11 +00:00
|
|
|
if (not $isLapsed) {
|
|
|
|
my $lastYearGave = $sp->donorTotalGaveInPeriod(donorId => $supporterId,
|
|
|
|
startDate => $ONE_YEAR_AGO, endDate => $TODAY);
|
|
|
|
($lastYearGave >= $SPECIAL_THRESHOLD) ? ($specialContributions{$supporterId} = $lastYearGave) :
|
|
|
|
($yearTot += $lastYearGave);
|
|
|
|
} else {
|
|
|
|
my $lapsedStr = (defined $expiresOn) ? "lapsed on $expiresOn" : "never gave enough to be a supporter";
|
|
|
|
print "$supporterId (", $sp->getLedgerEntityId($supporterId), ") $lapsedStr\n" if $isLapsed;
|
|
|
|
$lapsedCount++;
|
|
|
|
}
|
2016-01-03 22:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $per = ( ($lapsedCount / scalar(@supporterIds)) * 100.00);
|
2016-12-08 01:10:11 +00:00
|
|
|
my $activeCount = scalar(@supporterIds) - $lapsedCount;
|
2016-01-03 22:14:04 +00:00
|
|
|
print "\n\nWe have ", scalar(@supporterIds), " supporters and $lapsedCount are lapsed. That's ",
|
2016-12-08 01:10:11 +00:00
|
|
|
sprintf("%.2f", $per), "%.\nActive supporter count: ", $activeCount, "\n";
|
|
|
|
|
2024-11-03 23:50:35 +00:00
|
|
|
print "\n\nTotal (non speical) Given in Year in last year by active supporters: ", sprintf("%.2f\n", $yearTot);
|
2016-12-08 01:10:11 +00:00
|
|
|
print "Average annual contribution by non-lapsed donors: ", sprintf("%.2f\n\n", $yearTot / $activeCount);
|
|
|
|
|
|
|
|
print "\n\nSpecial Contributions: \n" if (keys(%specialContributions) > 0);
|
|
|
|
foreach my $key (sort keys %specialContributions) {
|
|
|
|
print sprintf("%8d: %.2f\n", $key, $specialContributions{$key});
|
|
|
|
}
|
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Local variables:
|
|
|
|
# compile-command: "perl -c calculate-renewal-rate.plx"
|
|
|
|
# End:
|
2016-01-03 22:14:04 +00:00
|
|
|
|