MT2889 : Fix statistics' itemtype when item-level_itypes is ON
[koha.git] / misc / link_bibs_to_authorities.pl
index 0ef713a..9954fd6 100755 (executable)
@@ -32,6 +32,7 @@ if (not $result or $want_help) {
 
 my $num_bibs_processed = 0;
 my $num_bibs_modified = 0;
+my $num_bad_bibs = 0;
 my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
 process_bibs();
@@ -62,6 +63,7 @@ Bib authority heading linking report
 ------------------------------------
 Number of bibs checked:       $num_bibs_processed
 Number of bibs modified:      $num_bibs_modified
+Number of bibs with errors:   $num_bad_bibs
 _SUMMARY_
 }
 
@@ -69,6 +71,12 @@ sub process_bib {
     my $biblionumber = shift;
 
     my $bib = GetMarcBiblio($biblionumber);
+    unless (defined $bib) {
+        print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
+        $num_bad_bibs++;
+        return;
+    }
+
     my $headings_changed = LinkBibHeadingsToAuthorities($bib);
 
     if ($headings_changed) {   
@@ -77,6 +85,11 @@ sub process_bib {
             print "Bib $biblionumber ($title): $headings_changed headings changed\n";
         }
         if (not $test_only) {
+            # delete any item tags
+            my ($itemtag, $itemsubfield) = GetMarcFromKohaField("items.itemnumber", '');
+            foreach my $field ($bib->field($itemtag)) {
+                $bib->delete_field($field);
+            }
             ModBiblio($bib, $biblionumber, GetFrameworkCode($biblionumber));
             $num_bibs_modified++;
         }
@@ -103,10 +116,6 @@ Parameters:
     --test                  only test the authority linking
                             and report the results; do not
                             change the bib records.
-    --comment <comment>     optional comment to describe
-                            the record batch; if the comment
-                            has spaces in it, surround the
-                            comment with quotation marks.
     --help or -h            show this message.
 _USAGE_
 }