From 3e7ecf2fd6187182fea83ba7b322b8be0c592ff8 Mon Sep 17 00:00:00 2001 From: Michael Hafen Date: Thu, 29 Nov 2012 11:25:15 -0700 Subject: [PATCH] Fixing MARC import with holding in 952 and repeated subfields Content-Type: text/plain; charset="utf-8" Found a marc record from a vendor which has holdings in the 952, like Koha does. The vendor tried to have a repeating 'z' subfield (public notes), but Koha failed to parse it, and instead created seperate item records for the repeated subfields. Also adding the home and holding branch if they aren't set, like they weren't in this file. --- C4/ImportBatch.pm | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 15f1acb..8b6de32 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -424,6 +424,48 @@ sub AddItemsToImportBiblio { my $dbh = C4::Context->dbh; my ($item_tag,$item_subfield) = &GetMarcFromKohaField("items.itemnumber",''); foreach my $item_field ($marc_record->field($item_tag)) { + my ($b_tag,$b_sub) = &GetMarcFromKohaField("items.barcode",''); + my ($c_tag,$c_sub) = &GetMarcFromKohaField("items.itemcallnumber",''); + unless ( $item_field->subfield( $item_subfield ) || $item_field->subfield( $b_sub ) || $item_field->subfield( $c_sub ) ) { + # No itemnumber, barcode, or call number. + # This is probably a repeated subfield stuffed into it's own + # field. The best action would be to join it to the previous + # item record so we don't loose any data. + # Yes, I have actually seen records like this from a vendor + my $import_id = $import_items_ids[ $#import_items_ids ]; + my $sth = $dbh->prepare("SELECT marcxml FROM import_items WHERE import_items_id = ?"); + $sth->bind_param(1, $import_id); + $sth->execute(); + my ( $xml ) = $sth->fetchrow_array; + my $item_marc = MARC::Record->new_from_xml( $xml ); + foreach my $subfield ( $item_field->subfields() ) { + my $data = $item_marc->subfield( $item_tag, $subfield->[0] ); + $data .= " | " if ( $data ); + $data .= $subfield->[1]; + for my $field ( $item_marc->field( $item_tag ) ) { + $field->update( $subfield->[0] => $data ); + } + } + + $sth = $dbh->prepare("UPDATE import_items SET marcxml = ? WHERE import_items_id = ?"); + $sth->bind_param(1, $item_marc->as_xml()); + $sth->bind_param(2, $import_id); + $sth->execute(); + + next; + } + + # make sure home and holding branch are set + my $branch = C4::Context::userenv->{branch}; + my ($h_tag,$h_sub) = &GetMarcFromKohaField("items.homebranch",''); + my ($h2_tag,$h2_sub) = &GetMarcFromKohaField("items.holdingbranch",''); + unless ( $item_field->subfield( $h_sub ) ) { + $item_field->add_subfields( $h_sub, $branch ) if ( $branch ); + } + unless ( $item_field->subfield( $h2_sub ) ) { + $item_field->add_subfields( $h2_sub, $branch ) if ( $branch ); + } + my $item_marc = MARC::Record->new(); $item_marc->leader("00000 a "); # must set Leader/09 to 'a' $item_marc->append_fields($item_field); -- 1.7.10.4