Bug 7131: (follow-up) allow overlaying by barcode
authorKyle M Hall <kyle@bywatersolutions.com>
Fri, 5 Apr 2013 11:53:47 +0000 (07:53 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 30 Oct 2013 04:34:04 +0000 (04:34 +0000)
This patch adds the ability to overlay by either itemnumber,
or barcode. Itemnumbers take precendence over barcodes, which
allows us to batch update item barcodes with an overlay.

Test Plan:
1) Create a new record with 2 items, make sure to give it a unique ISBN
2) Download the record as MARCXML
3) Edit the MARC XML
   a) Delete one of the two items
   b) Change the barcode in the barcode field to something unused
4) Transform the xml file into marc with xml2marc
5) Browse to 'Stage MARC records for import'
6) Upload the binary marc file
7) Choose the following options:
    Record matching rule: ISBN
    Action if matching record found: Ignore incoming record
    Action if no match is found: Ignore incoming record
    Check for embedded item record data: Yes
    How to process items: Replace items if matching bib was found
8) Click 'Stage for import' button
9) Verify a matching record was found, then click 'Manage staged records' link
10) Verify the rules are still set correctly
11) Click 'Import this batch into the catalog'
12) The import should tell you:
    1 record was ignored
    1 item was replaced
13) View the record details and verify the item's barcode was replaced
    with your updated barcode value
14) Download the record as MARCXML
15) Edit the MARC XML
    a) Delete one of the two items
    b) Delete the itemnumber field for the remaining item
    c) Alter the item's callnumber to a new value
16) Repeat steps 4 through 12
17) View the record details and verify the item's callnumber was replace
    with your updated callnumber value

Signed-off-by: Henry Bankhead <hbankhead@losgatosca.gov>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
C4/ImportBatch.pm
installer/data/mysql/updatedatabase.pl

index f9d7b41..3bf71e1 100644 (file)
@@ -654,9 +654,10 @@ sub BatchCommitRecords {
             SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
             SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
         } elsif ($record_result eq 'ignore') {
+            $recordid = $record_match;
             $num_ignored++;
             $recordid = $record_match;
-            if ($record_type eq 'biblio' and defined $recordid and $item_result eq 'create_new') {
+            if ($record_type eq 'biblio' and defined $recordid and ( $item_result eq 'create_new' || $item_result eq 'replace' ) ) {
                 my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result);
                 $num_items_added += $bib_items_added;
          $num_items_replaced += $bib_items_replaced;
@@ -688,19 +689,20 @@ sub BatchCommitItems {
     my $dbh = C4::Context->dbh;
 
     my ( $num_items_added, $num_items_errored, $num_items_replaced ) = 0;
-    my $sth = $dbh->prepare("
+    my $sth = $dbh->prepare( "
         SELECT import_items_id, import_items.marcxml, encoding
         FROM import_items
         JOIN import_records USING (import_record_id)
         WHERE import_record_id = ?
         ORDER BY import_items_id
-    ");
+    " );
     $sth->bind_param( 1, $import_record_id );
     $sth->execute();
+
     while ( my $row = $sth->fetchrow_hashref() ) {
         my $item_marc = MARC::Record->new_from_xml( StripNonXmlChars( $row->{'marcxml'} ), 'UTF-8', $row->{'encoding'} );
 
-        #delete date_due subfield as to not accidentally delete item checkout due dates
+        # Delete date_due subfield as to not accidentally delete item checkout due dates
         my ( $MARCfield, $MARCsubfield ) = GetMarcFromKohaField( 'items.onloan', GetFrameworkCode($biblionumber) );
         $item_marc->field($MARCfield)->delete_subfield( code => $MARCsubfield );
 
@@ -711,6 +713,7 @@ sub BatchCommitItems {
 
         my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?");
         if ( $action eq "replace" && $duplicate_itemnumber ) {
+            # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying
             ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} );
             $updsth->bind_param( 1, 'imported' );
             $updsth->bind_param( 2, $item->{itemnumber} );
@@ -718,6 +721,15 @@ sub BatchCommitItems {
             $updsth->execute();
             $updsth->finish();
             $num_items_replaced++;
+        } elsif ( $action eq "replace" && $duplicate_barcode ) {
+            my $itemnumber = GetItemnumberFromBarcode( $item->{'barcode'} );
+            ModItemFromMarc( $item_marc, $biblionumber, $itemnumber );
+            $updsth->bind_param( 1, 'imported' );
+            $updsth->bind_param( 2, $item->{itemnumber} );
+            $updsth->bind_param( 3, $row->{'import_items_id'} );
+            $updsth->execute();
+            $updsth->finish();
+            $num_items_replaced++;
         } elsif ($duplicate_barcode) {
             $updsth->bind_param( 1, 'error' );
             $updsth->bind_param( 2, 'duplicate item barcode' );
index b6aba4a..25dcf1c 100755 (executable)
@@ -7583,6 +7583,19 @@ if ( CheckVersion($DBversion) ) {
     SetVersion($DBversion);
 }
 
+$DBversion = "3.13.00.XXX";
+if ( CheckVersion($DBversion) ) {
+    $dbh->do("
+        ALTER TABLE `import_batches`
+        CHANGE `item_action` `item_action`
+          ENUM( 'always_add', 'add_only_for_matches', 'add_only_for_new', 'ignore', 'replace' )
+          NOT NULL DEFAULT 'always_add'
+    ");
+    print "Upgrade to $DBversion done (Bug 7131 - way to overlay items in in marc import)\n";
+    SetVersion($DBversion);
+}
+
+
 =head1 FUNCTIONS
 
 =head2 TableExists($table)