Bug 32804: Do not adjust biblionumber when replacing items during import
authorNick Clemens <nick@bywatersolutions.com>
Thu, 2 Feb 2023 20:16:49 +0000 (20:16 +0000)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 17 Mar 2023 12:59:11 +0000 (09:59 -0300)
This patch adjust the item matching at import to confirm that for a duplicate itemnumber or barcode
matches an existing item in the DB and uses the original biblionumber when updating the item.

When ordering in a consortium the items may be moved around, duplicate biblios added, and various matches found.
We should not allow importing of items to move them from 1 biblio to another, but  we should allow the imports
to succeed and assume itemnumber or barcode matches are authoritative. The responsibility for correct matching of items to biblio should fall on the creator of the files

To test:
 1 - Be using the sample data in koha testing docker
 2 - Stage the sample file on this report
 3 - Match on KohaBiblio(999$c) / Item processing: replace
 4 - Note the biblio match is "The complete novels / Austen, Jane"
 5 - View the staged marc, note the barcode 39999000004090 in an item
 6 - Search for that barcode
 7 - You find biblio "Five novels : complete and unabridged / Gustave Flaubert"
 8 - Import the file
 9 - Check the db:
     SELECT * FROM items WHERE biblionumber != biblioitemnumber;
10 - Note the mismatch
11 - Fix the item and set it as 'Music' type
     UPDATE items SET biblionumber = biblioitemnumber, itype='MU' WHERE biblionumber != biblioitemnumber;
12 - Apply patch, restart all
13 - Stage and import the file with the same settings
14 - Confirm the item is modified on its original biblio (99) and that item type is Book again
15 - Change itemnumber to avoid itemnumber match and reset type
     UPDATE items SET itype='MU', itemnumber=999 WHERE itemnumber=212;
16 - Stage and import with the same setttings
17 - Confirm the marcode match worked and item is updated to book on original record
18 - Delete the original item
19 - Stage and import the file with the same settings
20 - The item is successfully created
21 - Stage and import, but item processing option is 'add'
22 - Confirm 1 item ignored
23 - Check the db
     SELECT * FROM import_items WHERE barcode=39999000004090
24 - Confirm there is a line with 'error' and duplicate_barcode

JD amended patch
-        # We assume that when replaicing tiems we do not want to move them - the onus is on the importer to
+        # We assume that when replacing items we do not want to move them - the onus is on the importer to

Signed-off-by: Andrew Fuerste-Henry <andrewfh@dubcolib.org>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
C4/ImportBatch.pm

index 0956ef5..b073a2b 100644 (file)
@@ -764,13 +764,20 @@ sub _batchCommitItems {
 
         my $item = TransformMarcToKoha({ record => $item_marc, kohafields => ['items.barcode','items.itemnumber'] });
 
-        my $duplicate_barcode = exists( $item->{'barcode'} ) && Koha::Items->find({ barcode => $item->{'barcode'} });
+        my $item_match;
+        my $duplicate_barcode = exists( $item->{'barcode'} );
         my $duplicate_itemnumber = exists( $item->{'itemnumber'} );
 
+        # We assume that when replacing items we do not want to move them - the onus is on the importer to
+        # ensure the correct items/records are being updated
         my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ?, import_error = ? WHERE import_items_id = ?");
-        if ( $action eq "replace" && $duplicate_itemnumber ) {
+        if (
+            $action eq "replace" &&
+            $duplicate_itemnumber &&
+            ( $item_match = Koha::Items->find( $item->{itemnumber} ))
+        ) {
             # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
-            ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber}, { skip_record_index => 1 } );
+            ModItemFromMarc( $item_marc, $item_match->biblionumber, $item->{itemnumber}, { skip_record_index => 1 } );
             $updsth->bind_param( 1, 'imported' );
             $updsth->bind_param( 2, $item->{itemnumber} );
             $updsth->bind_param( 3, undef );
@@ -778,9 +785,12 @@ sub _batchCommitItems {
             $updsth->execute();
             $updsth->finish();
             $num_items_replaced++;
-        } elsif ( $action eq "replace" && $duplicate_barcode ) {
-            my $itemnumber = $duplicate_barcode->itemnumber;
-            ModItemFromMarc( $item_marc, $biblionumber, $itemnumber, { skip_record_index => 1 } );
+        } elsif (
+            $action eq "replace" &&
+            $duplicate_barcode &&
+            ( $item_match = Koha::Items->find({ barcode => $item->{'barcode'} }) )
+        ) {
+            ModItemFromMarc( $item_marc, $item_match->biblionumber, $item_match->itemnumber, { skip_record_index => 1 } );
             $updsth->bind_param( 1, 'imported' );
             $updsth->bind_param( 2, $item->{itemnumber} );
             $updsth->bind_param( 3, undef );
@@ -788,7 +798,11 @@ sub _batchCommitItems {
             $updsth->execute();
             $updsth->finish();
             $num_items_replaced++;
-        } elsif ($duplicate_barcode) {
+        } elsif (
+            # We aren't replacing, but the incoming file ahs a barcode, we need to check if it exists
+            $duplicate_barcode &&
+            ( $item_match = Koha::Items->find({ barcode => $item->{'barcode'} }) )
+        ) {
             $updsth->bind_param( 1, 'error' );
             $updsth->bind_param( 2, undef );
             $updsth->bind_param( 3, 'duplicate item barcode' );