From ae008e912c1970d22a0ddb909dc29efd820cd03c Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Sun, 16 Sep 2018 01:29:41 +0300
Subject: [PATCH] Bug 20447: Ignore holdings records in staff UI when staging
 records

Importing holdings is not a typical action and can be done with bulk import if necessary.
---
 C4/ImportBatch.pm          | 28 ++++++++++++++++++++++++++--
 tools/stage-marc-import.pl |  2 +-
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm
index 99bc428..06b561c 100644
--- a/C4/ImportBatch.pm
+++ b/C4/ImportBatch.pm
@@ -1505,13 +1505,24 @@ sub RecordsFromISO2709File {
 
     open IN, "<$input_file" or die "$0: cannot open input file $input_file: $!\n";
     my @marc_records;
+    my $count = 0;
     $/ = "\035";
     while (<IN>) {
         s/^\s+//;
         s/\s+$//;
         next unless $_; # skip if record has only whitespace, as might occur
                         # if file includes newlines between each MARC record
+        ++$count;
         my ($marc_record, $charset_guessed, $char_errors) = MarcToUTF8Record($_, $marc_type, $encoding);
+        # Ignore holdings records
+        if ($record_type eq 'biblio' && $marc_type eq 'MARC21') {
+            my $leader = $marc_record->leader();
+            if ($leader =~ /^.{6}[uvxy]/) {
+                push @errors, "Ignoring record $count (holdings record)";
+                next;
+            }
+        }
+
         push @marc_records, $marc_record;
         if ($charset_guessed ne $encoding) {
             push @errors,
@@ -1536,15 +1547,28 @@ Returns two array refs.
 =cut
 
 sub RecordsFromMARCXMLFile {
-    my ( $filename, $encoding ) = @_;
+    my ( $filename, $record_type, $encoding ) = @_;
+
+    my $marcflavour = C4::Context->preference('marcflavour');
     my $batch = MARC::File::XML->in( $filename );
     my ( @marcRecords, @errors, $record );
+    my $count = 0;
     do {
+        ++$count;
         eval { $record = $batch->next( $encoding ); };
         if ($@) {
             push @errors, $@;
         }
-        push @marcRecords, $record if $record;
+        # Ignore holdings records
+        my $valid = 1;
+        if ($record && $record_type eq 'biblio' && $marcflavour eq 'MARC21') {
+            my $leader = $record->leader();
+            if ($leader =~ /^.{6}[uvxy]/) {
+                push @errors, "Ignoring record $count (holdings record)";
+                $valid = 0;
+            }
+        }
+        push @marcRecords, $record if $record && $valid;
     } while( $record );
     return (\@errors, \@marcRecords);
 }
diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl
index d76c35a..8663317 100755
--- a/tools/stage-marc-import.pl
+++ b/tools/stage-marc-import.pl
@@ -93,7 +93,7 @@ if ($completedJobID) {
 
     my ( $errors, $marcrecords );
     if( $format eq 'MARCXML' ) {
-        ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $encoding);
+        ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $file, $record_type, $encoding);
     } elsif( $format eq 'ISO2709' ) {
         ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( $file, $record_type, $encoding );
     } else { # plugin based
-- 
2.7.4