JBAS-1717 Student card processor ignores duplicate IDs
authorBill Erickson <berickxx@gmail.com>
Tue, 10 Jan 2017 23:08:44 +0000 (18:08 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/utility-scripts/import_students/generate-patrons-from-csv.pl

index 45c7203..3244841 100755 (executable)
@@ -256,14 +256,16 @@ sub iterate_csv_rows {
 
     $csv->column_names($csv->fields);
 
-    my $skipped = 0;
+    my %seen;
     while (my $phash = $csv->getline_hr($fh)) {
 
-        if (!$phash->{student_id}) { $skipped++; next; }
+        next unless $phash->{student_id};
 
-        # Remove any non-alphanumeric characters.
+        # Remove all non-alphanumeric characters.
         $phash->{student_id} =~ s/[^\w]//oag;
 
+        next unless $phash->{student_id};
+
         # 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.
@@ -271,11 +273,17 @@ sub iterate_csv_rows {
             if $is_teacher && length($phash->{student_id}) < 4;
 
         # All teacher ID's, which can be alpha-numeric, are upper case.
+        # Student ID's can also be alpha-numeric, but remain in the
+        # provided case.
         $phash->{student_id} = uc($phash->{student_id}) if $is_teacher;
 
-        # Teacher cards have an extra 't' between district code and ID.
-        $phash->{barcode} = $district_code . 
-            ($is_teacher ? 't' : '') . $phash->{student_id};
+        # Avoid processing duplicates
+        next if $seen{$phash->{student_id}};
+        $seen{$phash->{student_id}} = 1;
+
+        $phash->{barcode}  = $district_code;
+        $phash->{barcode} .= 't' if $is_teacher;
+        $phash->{barcode} .= $phash->{student_id};
 
         $row_handler->($phash);
     }
@@ -286,8 +294,6 @@ sub iterate_csv_rows {
     }
 
     $fh->close;
-
-    announce('WARNING', "$skipped entries had no student_id") if $skipped;
 }
 
 # Determine which patrons in the file are new by testing whether their
@@ -306,7 +312,7 @@ sub find_new_patrons {
 
     iterate_csv_rows($row_handler);
 
-    announce('INFO', "File contains ".scalar(@all_barcodes)." total students");
+    announce('INFO', "File contains ".scalar(@all_barcodes)." unique students");
 
     my $all_barcodes = join(',', @all_barcodes);