Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 115 lines (100 sloc) 2.893 kB
#!/usr/bin/env perl
use 5.14.1;
use Dancer qw/:syntax/;
use FindBin;
use Cwd qw/realpath/;
use lib "$FindBin::Bin/../lib";
use Text::CSV_XS;
use Biopay::Member;
BEGIN {
Dancer::Config::setting('appdir',realpath("$FindBin::Bin/.."));
Dancer::Config::setting('views',realpath("$FindBin::Bin/../views"));
Dancer::Config::load();
}
my $csv = Text::CSV_XS->new;
open my $fh, "<:encoding(utf8)", "members.csv" or die "members.csv: $!";
while (my $row = $csv->getline ($fh)) {
my ($mid, $expiry, $name, $phone, $email, $joined, $address) = @$row;
unless (($mid||='') and $name) {
print "Skipping empty member id $mid\n";
next;
}
unless ($mid =~ m/^\d+$/) {
die "Sorry, member ID '$mid' does not look good.\n";
}
# Intentionally ignore all existing expiry times - they are a mess.
# VBC hasn't charged membership in years.
$expiry = 0;
if ($joined ||= 0) {
if ($joined =~ m(^(\d+)[/-](\d+)[/-](\d+)$)) {
$joined = dmy_to_epoch($1, $2, $3);
}
else {
die "Couldn't parse date format for $mid: '$joined'";
}
$expiry = $joined + (60*60*24*365);
}
my $data = {
id => $mid,
dues_paid_until => $expiry,
name => $name,
phone_num => $phone,
email => $email,
start_epoch => $joined,
address => $address,
};
next unless $mid;
my $m = Biopay::Member->By_id($mid);
if ($m and $name) {
if ($m->email or $m->start_epoch) {
print "Skipping #" . $m->id . " - " . $m->name . "\n";
}
else {
update_member($m, $data);
}
}
else {
print "Creating $mid - $name ...\n";
my $m = Biopay::Member->Create(
member_id => $mid,
(map { $_ => $data->{$_} }
grep { defined $data->{$_} }
qw/name phone_num address email start_epoch dues_paid_until/)
);
}
}
exit;
sub update_member {
my $m = shift;
my $data = shift;
my $changed = 0;
print "Checking member #$data->{id} ($data->{name})\n";
for my $attr (qw/name phone_num email address/) {
if (($m->$attr||'') ne $data->{$attr}) {
print " ... updating $attr to $data->{$attr}\n";
$m->$attr($data->{$attr});
$changed++;
}
}
if (my $joined = $data->{start_epoch}) {
if (($m->start_epoch||0) != $joined) {
$m->start_epoch($joined);
$changed++;
}
}
if ($changed) {
print " ... Saving!\n";
$m->save;
}
}
sub dmy_to_epoch {
my ($d, $m, $y) = @_;
if ($m > 12) {
my $x = $m;
$m = $d;
$d = $x;
}
my $dt = DateTime->new(year => $y, month => $m, day => $d);
$dt->set_time_zone('America/Vancouver');
return $dt->epoch;
}
Jump to Line
Something went wrong with that request. Please try again.