From ad4e02f91d3c18e7705f5c6f58006ebf3e676613 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Sun, 2 Dec 2007 13:41:44 -0600 Subject: [PATCH] warn on attempts to add duplicate item barcodes during batch import Signed-off-by: Chris Cormack Signed-off-by: Joshua Ferraro --- C4/ImportBatch.pm | 46 +++++++++++++------ .../en/modules/tools/manage-marc-import.tmpl | 1 + misc/commit_biblios_file.pl | 7 ++- misc/migration_tools/bulkmarcimport.pl | 11 ++++- tools/manage-marc-import.pl | 4 +- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 64dcaffbff..06d5d0769a 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -412,7 +412,7 @@ sub BatchFindBibDuplicates { =over 4 -my ($num_added, $num_updated, $num_items_added, $num_ignored) = +my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) = BatchCommitBibRecords($batch_id, $progress_interval, $progress_callback); =back @@ -436,6 +436,7 @@ sub BatchCommitBibRecords { my $num_added = 0; my $num_updated = 0; my $num_items_added = 0; + my $num_items_errored = 0; my $num_ignored = 0; # commit (i.e., save, all records in the batch) # FIXME biblio only at the moment @@ -464,7 +465,9 @@ sub BatchCommitBibRecords { my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); $sth->execute($biblionumber, $rowref->{'import_record_id'}); $sth->finish(); - $num_items_added += BatchCommitItems($rowref->{'import_record_id'}, $biblionumber); + my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber); + $num_items_added += $bib_items_added; + $num_items_errored += $bib_items_errored; SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); } else { $num_updated++; @@ -487,21 +490,23 @@ sub BatchCommitBibRecords { my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"); $sth2->execute($biblionumber, $rowref->{'import_record_id'}); $sth2->finish(); - $num_items_added += BatchCommitItems($rowref->{'import_record_id'}, $biblionumber); + my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $biblionumber); + $num_items_added += $bib_items_added; + $num_items_errored += $bib_items_errored; SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied'); SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); } } $sth->finish(); SetImportBatchStatus($batch_id, 'imported'); - return ($num_added, $num_updated, $num_items_added, $num_ignored); + return ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored); } =head2 BatchCommitItems =over 4 -$num_items_added = BatchCommitItems($import_record_id, $biblionumber); +($num_items_added, $num_items_errored) = BatchCommitItems($import_record_id, $biblionumber); =back @@ -513,6 +518,7 @@ sub BatchCommitItems { my $dbh = C4::Context->dbh; my $num_items_added = 0; + my $num_items_errored = 0; my $sth = $dbh->prepare("SELECT import_items_id, import_items.marcxml, encoding FROM import_items JOIN import_records USING (import_record_id) @@ -522,17 +528,29 @@ sub BatchCommitItems { $sth->execute(); while (my $row = $sth->fetchrow_hashref()) { my $item_marc = MARC::Record->new_from_xml($row->{'marcxml'}, 'UTF-8', $row->{'encoding'}); - my ($item_biblionumber, $biblionumber, $itemnumber) = AddItem($item_marc, $biblionumber); - my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?"); - $updsth->bind_param(1, 'imported'); - $updsth->bind_param(2, $itemnumber); - $updsth->bind_param(3, $row->{'import_items_id'}); - $updsth->execute(); - $updsth->finish(); - $num_items_added++; + # FIXME - duplicate barcode check needs to become part of AddItem() + my $item = TransformMarcToKoha($dbh, $item_marc); + my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'}); + if ($duplicate_barcode) { + my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, import_error = ? WHERE import_items_id = ?"); + $updsth->bind_param(1, 'error'); + $updsth->bind_param(2, 'duplicate item barcode'); + $updsth->bind_param(3, $row->{'import_items_id'}); + $updsth->execute(); + $num_items_errored++; + } else { + my ($item_biblionumber, $biblioitemnumber, $itemnumber) = AddItem($item_marc, $biblionumber); + my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?"); + $updsth->bind_param(1, 'imported'); + $updsth->bind_param(2, $itemnumber); + $updsth->bind_param(3, $row->{'import_items_id'}); + $updsth->execute(); + $updsth->finish(); + $num_items_added++; + } } $sth->finish(); - return $num_items_added; + return ($num_items_added, $num_items_errored); } =head2 BatchRevertBibRecords diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl index 7743faee5b..9adc53ecf5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tmpl @@ -98,6 +98,7 @@ Number of records added Number of records updated Number of items added + Number of items ignored because of duplicate barcode Number of records ignored diff --git a/misc/commit_biblios_file.pl b/misc/commit_biblios_file.pl index 09febad7ab..f0cbd06d3e 100755 --- a/misc/commit_biblios_file.pl +++ b/misc/commit_biblios_file.pl @@ -64,7 +64,8 @@ sub process_batch { my ($import_batch_id) = @_; print "... importing MARC records -- please wait\n"; - my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRecords($import_batch_id, 100, \&print_progress); + my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) = + BatchCommitBibRecords($import_batch_id, 100, \&print_progress); print "... finished importing MARC records\n"; print <<_SUMMARY_; @@ -76,6 +77,10 @@ Number of new bibs added: $num_added Number of bibs replaced: $num_updated Number of bibs ignored: $num_ignored Number of items added: $num_items_added +Number of items ignored: $num_items_errored + +Note: an item is ignored if its barcode is a +duplicate of one already in the database. _SUMMARY_ } diff --git a/misc/migration_tools/bulkmarcimport.pl b/misc/migration_tools/bulkmarcimport.pl index cc186cb4fd..254dca0e50 100755 --- a/misc/migration_tools/bulkmarcimport.pl +++ b/misc/migration_tools/bulkmarcimport.pl @@ -304,8 +304,15 @@ while ( my $record = $batch->next() ) { } else { warn "ADDED biblio NB $bibid in DB\n" if $verbose; for ( my $it = 0 ; $it <= $#items ; $it++ ) { - eval { AddItem( $items[$it], $bibid, $oldbibitemnum ); }; - warn "ERROR: Adding item $it, rec $i failed\n" if ($@); + # FIXME - duplicate barcode check needs to become part of AddItem() + my $itemhash = TransformMarcToKoha($dbh, $items[$it]); + my $duplicate_barcode = exists($itemhash->{'barcode'}) && GetItemnumberFromBarcode($itemhash->{'barcode'}); + if ($duplicate_barcode) { + warn "ERROR: cannot add item $itemhash->{'barcode'} for biblio $bibid: duplicate barcode\n" if $verbose; + } else { + eval { AddItem( $items[$it], $bibid, $oldbibitemnum ); }; + warn "ERROR: Adding item $it, rec $i failed\n" if ($@); + } } } } diff --git a/tools/manage-marc-import.pl b/tools/manage-marc-import.pl index 8242db0478..ebc6b5cab6 100755 --- a/tools/manage-marc-import.pl +++ b/tools/manage-marc-import.pl @@ -157,13 +157,15 @@ sub commit_batch { $job = put_in_background($import_batch_id); $callback = progress_callback($job); } - my ($num_added, $num_updated, $num_items_added, $num_ignored) = BatchCommitBibRecords($import_batch_id, 50, $callback); + my ($num_added, $num_updated, $num_items_added, $num_items_errored, $num_ignored) = + BatchCommitBibRecords($import_batch_id, 50, $callback); my $results = { did_commit => 1, num_added => $num_added, num_updated => $num_updated, num_items_added => $num_items_added, + num_items_errored => $num_items_errored, num_ignored => $num_ignored }; if ($runinbackground) { -- 2.39.5