my $msg_str = "$date_str [$$] $level $msg\n";
if ($die) {
- die $msg_str;
+ warn $msg_str;
+ exit 0;
} else {
if ($level eq 'ERR' or $level eq 'WARNING') {
binmode($fh, ":utf8");
- my $header = readline($fh);
- if (!$csv->parse($header)) {
- announce('ERR', "Unable to parse CSV header: $header", 1);
- }
+ my $header = readline($fh) || '';
+
+ announce('ERR', "Unable to parse CSV header: '$header'", 1)
+ unless $header && $csv->parse($header);
$csv->column_names($csv->fields);
+ my $skipped = 0;
while (my $phash = $csv->getline_hr($fh)) {
+ if (!$phash->{student_id}) { $skipped++; next; }
+
+ # Remove any non-alphanumeric characters.
+ $phash->{student_id} =~ s/[^\w]//oag;
+
# If the ID value is less than 4 characters, pad the value
# with zeros to be 4 characters long. This will effect
- # the barcode and password.
- # Only do this for teacher accounts.
+ # the barcode and password. Only do this for teacher accounts.
$phash->{student_id} = sprintf('%0.4d', $phash->{student_id})
if $is_teacher && length($phash->{student_id}) < 4;
}
$fh->close;
+
+ announce('WARNING', "$skipped entries had no student_id") if $skipped;
}
# Determine which patrons in the file are new by testing whether their
my $all_barcodes = join(',', @all_barcodes);
- # generate the SQL to determine which barcodes don't exist
+ # Generate the SQL to determine which barcodes don't yet exist.
+ # Confirm the incoming barcode does not match an existing
+ # barcode or username, since either would cause an insert failure.
my $SQL = <<SQL;
SELECT subq.bc
FROM(
SELECT UNNEST('{$all_barcodes}'::TEXT[]) AS bc
) AS subq
- WHERE NOT EXISTS (
- SELECT TRUE FROM actor.card WHERE barcode = subq.bc
- )
+ WHERE
+ NOT EXISTS (
+ SELECT TRUE FROM actor.card WHERE barcode = subq.bc
+ ) AND NOT EXISTS (
+ SELECT TRUE FROM actor.usr WHERE usrname = subq.bc
+ )
SQL
my $new_barcodes = $db_handle->selectall_arrayref($SQL);
return;
}
+ # NOTE: exit early for non-new barcodes since patron updates
+ # are not supported. This makes the script run faster.
+ return unless $new_barcodes{$phash->{barcode}};
+
return unless translate_patron_data($phash);
$summary_stats{trans}++;