2016-01-02 18:33:35 +00:00
#!/usr/bin/perl
use strict;
use warnings;
2022-06-21 17:15:56 +00:00
use autodie qw(:all);
use Getopt::Long;
2016-01-02 18:33:35 +00:00
use DBI;
use Encode qw(encode decode);
2022-06-21 17:15:56 +00:00
use File::Spec::Functions;
use Time::HiRes qw(usleep nanosleep);
use Text::CSV; # libtext-csv-perl in Debian
use Supporters;
use strict;
use warnings;
use autodie qw(open close chdir);
use DBI;
use Encode qw(encode decode);
use LaTeX::Encode;
2016-01-02 18:33:35 +00:00
2016-01-02 18:33:57 +00:00
use Supporters;
2022-06-21 17:15:56 +00:00
use utf8;
use IPC::Shareable;
use Email::MIME::RFC2047::Encoder;
my $FROM_ADDRESS = 'info@sfconservancy.org';
2016-01-02 18:33:57 +00:00
2022-06-21 17:15:56 +00:00
require 'bean-query-daemon-lib.pl';
2019-12-10 17:36:38 +00:00
my $encoder = Email::MIME::RFC2047::Encoder->new();
2016-01-02 18:33:35 +00:00
2022-06-21 17:15:56 +00:00
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";
my($VERBOSE, $stampsComCsvFile, $WHO, $EMAIL_TEMPLATE_FILE, $SUPPORTERS_SQLITE_DB_FILE) =
(0, undef, undef,undef, catfile($ENV{CONSERVANCY_REPOSITORY},
'Financial', 'Ledger', 'supporters.db'));
GetOptions("verbose=i" => \$VERBOSE, "supporterDB=s" => \$SUPPORTERS_SQLITE_DB_FILE,
'who=s' => \$WHO, 'emailTemplateFile=s' => \$EMAIL_TEMPLATE_FILE,
'stampsComCsvFile=s' => \$stampsComCsvFile);
sub UsageAndExit($) {
print STDERR "usage: $0 --stampsComCsvFile=<PATH_TO_FILE> [ --supportersDB=PATH_TO_SUPPORTERS_SQLITE_DB_FILE --verbose=N ]\n";
print STDERR "\n $_[0]\n";
exit 2;
2016-01-02 18:33:35 +00:00
}
2022-06-21 17:15:56 +00:00
UsageAndExit("Cannot read supporters db file: $SUPPORTERS_SQLITE_DB_FILE") unless defined $SUPPORTERS_SQLITE_DB_FILE
and -r $SUPPORTERS_SQLITE_DB_FILE;
UsageAndExit("Cannot read email template: $EMAIL_TEMPLATE_FILE") unless defined $EMAIL_TEMPLATE_FILE
and -r $EMAIL_TEMPLATE_FILE;
UsageAndExit("--who is required") unless defined $WHO;
UsageAndExit("Cannot read stampsComCsvFile: $stampsComCsvFile: $!") unless defined $stampsComCsvFile
and -r $stampsComCsvFile and -f $stampsComCsvFile;
2016-01-02 18:33:35 +00:00
$VERBOSE = 0 if not defined $VERBOSE;
my $dbh = DBI->connect("dbi:SQLite:dbname=$SUPPORTERS_SQLITE_DB_FILE", "", "",
{ RaiseError => 1, sqlite_unicode => 1 })
or die $DBI::errstr;
2022-06-21 17:15:56 +00:00
# my $fileName = BeancountQuerySubmit(<<SUPPORTESR_DB_READ
# SELECT ANY_META("program") as program, date, ANY_META("entity") as entity, NUMBER(COST(position))
# WHERE account ~ "^Income" AND ANY_META("project") ~ "Conservancy"
# AND ANY_META("program") ~ "Conservancy:Supporters:" AND NOT ANY_META("entity") ~ "^\\s*\$"
# SUPPORTESR_DB_READ
# , 'text');
# open(my $fh, "<", $fileName);
# my $sp = new Supporters($dbh, $fh, { monthly => '^Conservancy:Supporters:Monthly',
# annual => '^Conservancy:Supporters:(?:Annual|Match Pledge)' } );
my $sp = new Supporters($dbh, [ '/bin/false' ]);
my @emailLines;
open(my $emailFH, "<", $EMAIL_TEMPLATE_FILE);
binmode $emailFH, ":utf8";
@emailLines = <$emailFH>;
close $emailFH;
open my $csvFH, "<:encoding(utf8)", $stampsComCsvFile;
my $csv = Text::CSV->new({ quote_char => '"', binary => 1, allow_whitespace => 1, quote_space=> 0,
allow_loose_quotes => 1 });
$csv->header($csvFH, { detect_bom => 1, munge_column_names => "lc" });
my %info;
while (my $row = $csv->getline_hr($csvFH)) {
$row->{'class service'} =~ s/\s*\(\s*R\s*\)\s*$//
if (defined $row->{'class service'} and $row->{'class service'} !~ /^\s*$/);
$row->{'tracking #'} =~ s/^\s*=\s*"([^"]+)"\s*$/$1/
if (defined $row->{'tracking #'} and $row->{'tracking #'} !~ /^\s*$/);
die "no reference1" if (not defined $row->{reference1} );
my(@vals) = split(',', $row->{reference1});
my $id = shift @vals;
$info{$id} = $row;
$info{$id}{shirts} = {}; $info{$id}{extras} = [];
foreach my $item (@vals) {
if ($item =~ /\s*\++\s*(\S.*)$/) {
push(@{$info{$id}{extras}}, $1);
next;
}
my($size, $type) = split(/_+/, $item);
if ($type =~ /2018/) {
$type = 't-shirt-fy2018design-0';
} elsif ($type =~ /vint/i) {
$type = 'vintage';
} else {
die "$type is not a known t-shirt type";
}
$info{$id}{shirts}{$type} = $size;
2016-01-02 18:33:35 +00:00
}
}
2022-06-21 17:15:56 +00:00
foreach my $id (sort keys %info) {
print "#" x 75;
print "\n";
print "Supporter $id:\n";
foreach my $type (sort { $a cmp $b} keys %{$info{$id}{shirts}}) { print " Shirt: $type: $info{$id}{shirts}{$type}\n " }
foreach my $extra (sort { $a cmp $b} @{$info{$id}{extras}}) { print " EXTRA: $extra\n " }
my $sentData = "";
my $thisHow = "a package sent with stamp.com";
my $service = "The package";
my $delivered = 0;
if (defined $info{$id}{'date delivered'} and $info{$id}{'date delivered'} !~ /^\s*$/) {
if (defined $info{$id}{'class service'} and $info{$id}{'class service'} =~ /int\'l|international/i) {
$service .= " left the USA sorting facility (bound for your country) on $info{$id}{'date delivered'}";
} else {
$service .= " was delivered on $info{$id}{'date delivered'}";
$delivered = 1 if $info{$id}{'status'} and $info{$id}{'status'} =~ /eliver/i;
}
} else {
$service .= " was shipped";
$thisHow .= " shipped";
if (defined $info{$id}{'ship date'} and $info{$id}{'ship date'} !~ /^\s*$/) {
$service .= " on $info{$id}{'ship date'}";
$thisHow .= " on $info{$id}{'ship date'}";
}
}
$service .= " via $info{$id}{carrier}";
$thisHow .= " with $info{$id}{carrier}";
if (defined $info{$id}{'class service'} and $info{$id}{'class service'} !~ /^\s*$/) {
$service .= "'s $info{$id}{'class service'}";
$thisHow .= "'s $info{$id}{'class service'}";
}
if (defined $info{$id}{'tracking #'} and $info{$id}{'tracking #'} !~ /^\s*$/) {
$service .= " with tracking number of $info{$id}{'tracking #'}";
$thisHow .= " having a tracking number of $info{$id}{'tracking #'}";
}
$service .= ".";
my $deliveryStatus = " The postal service has often been quite slow during the pandemic, so please be patient, but if the packge does not arrive within a few weeks, please contact us by replying to this email.";
$deliveryStatus = "The postal service has confirmed delivery occurred, so please contact us *immediately* by replying to this email if the packge has not been received."
if $delivered;
2016-01-02 18:33:35 +00:00
2016-12-08 23:08:53 +00:00
my @requestTypes = $sp->getRequestType();
2017-01-02 19:57:42 +00:00
my $sizesSent;
2022-06-21 17:15:56 +00:00
my %need;
2016-12-08 23:08:53 +00:00
foreach my $type (@requestTypes) {
2017-01-10 19:36:50 +00:00
my $request = $sp->getRequest({ donorId => $id, requestType => $type,
2016-12-08 23:08:53 +00:00
ignoreHeldRequests => 1, ignoreFulfilledRequests => 1 });
2017-01-10 19:36:50 +00:00
if (defined $request and defined $request->{requestId} and defined $request->{requestType}) {
2022-06-21 17:15:56 +00:00
$need{$type} = $request->{requestConfiguration} if $type =~ /^\s*t-shirt/;
}
}
my $cur = 't-shirt-fy2018design-0';
if (defined $need{$cur} and defined $info{$id}{shirts}{$cur}) {
die "$id: 2018design: $need{$cur} does not match defined $info{$id}{shirts}{$cur}"
unless (($need{$cur} eq $info{$id}{shirts}{$cur}) or
($need{$cur} eq 'FittedLadies2XL' and $info{$id}{shirts}{$cur} eq 'Ladies2XL'));
my $size = $info{$id}{shirts}{$cur};
$sp->fulfillRequest({ donorId => $id, requestType => $cur, who => $WHO, how => $thisHow});
$sentData .= "A $size t-shirt in the 2018 design. ";
delete $need{$cur};
delete $info{$id}{shirts}{$cur};
}
# vintage type can fulfill other types
foreach my $shirtType (keys %{$info{$id}{shirts}}) {
my $size = $info{$id}{shirts}{$shirtType};
foreach my $key (keys %need) {
$need{$key} = "Standard" . $need{$key} if ($need{$key} =~ /^Ladies/);
if ($need{$key} eq $size) {
$sp->fulfillRequest({ donorId => $id, requestType => $key, who => $WHO, how => $thisHow});
$sentData .= "A $size t-shirt in the vintage design. ";
delete $need{$key};
delete $info{$id}{shirts}{$shirtType};
2016-01-02 18:33:35 +00:00
}
}
}
2022-06-21 17:15:56 +00:00
foreach my $shirtType (keys %{$info{$id}{shirts}}) {
my $size = $info{$id}{shirts}{$shirtType};
$sentData .= "A $size t-shirt in the $shirtType design that we included just becuase we thought you would like it. ";
}
foreach my $extra (sort { $a cmp $b} @{$info{$id}{extras}}) {
if ($extra =~ /PIN/) {
$sentData .= " A pin from our our CopyleftConf 2019! ";
} else {
die "Supporter $id: unkown extra: $extra";
}
2016-01-02 18:33:35 +00:00
}
2019-12-10 17:36:38 +00:00
my %emails;
my $email = $sp->getPreferredEmailAddress($id);
if (defined $email) {
$emails{$email} = {};
2017-08-01 16:19:45 +00:00
} else {
2019-12-10 17:36:38 +00:00
%emails = $sp->getEmailAddresses($id);
2017-08-01 16:19:45 +00:00
}
2019-12-10 17:36:38 +00:00
my(@emails) = keys(%emails);
2017-08-01 16:19:45 +00:00
my $fullEmailLine = "";
my $emailTo = join(' ', @emails);
my $displayName = $sp->getDisplayName($id);
foreach my $email (@emails) {
$fullEmailLine .= ", " if ($fullEmailLine ne "");
my $line = "";
if (defined $displayName) {
2019-12-10 17:36:38 +00:00
$line .= $encoder->encode_phrase($displayName) . " ";
2017-08-01 16:19:45 +00:00
}
$line .= "<$email>";
2019-12-10 17:36:38 +00:00
$fullEmailLine .= $line;
2016-01-02 18:33:35 +00:00
}
2022-06-21 17:15:56 +00:00
my $rtTicket = $sp->_getDonorField('rt_ticket', $id);
$rtTicket = "" if not defined $rtTicket;
$rtTicket = "[sfconservancy.org #$rtTicket]" if $rtTicket ne "";
push(@emails, 'supporters@tix.sfconservancy.org') if $rtTicket ne "";
open(my $sendmailFH, "|-", '/usr/sbin/sendmail', '-f', $FROM_ADDRESS, '-oi', '-oem', '--',
@emails);
binmode $sendmailFH, ":utf8";
print $sendmailFH "To: $fullEmailLine\n";
foreach my $line (@emailLines) {
die "no displayname for this item" if not defined $displayName or $displayName =~ /^\s*$/;
my $thisLine = $line; # Note: This is needed, apparently $line is by reference?
$thisLine =~ s/FIXME_DISPLAYNAME/$displayName/g;
$thisLine =~ s/FIXME_SUPPORTER_ID/$id/g;
$thisLine =~ s/FIXME_SENT_INFO/$sentData/g;
$thisLine =~ s/FIXME_DELVIERY_STATUS_INFO/$deliveryStatus/g;
$thisLine =~ s/FIXME_RT_TICKET_DESCRIPTOR/$rtTicket/g;
$thisLine =~ s/FIXME_POSTAL_SERVICE_INFO/$service/g;
print $sendmailFH $thisLine;
}
close $sendmailFH;
usleep(70000);
2016-01-02 18:33:35 +00:00
}
###############################################################################
#
# Local variables:
# compile-command: "perl -c send-t-shirts.plx"
# End: