From f42418d541d7125d86837b15304cac502e77b67f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 6 Aug 2018 15:53:33 -0300 Subject: [PATCH] Bug 21183: Replace C4::Items::GetItemnumberFromBarcode calls C4::Items::GetItemnumberFromBarcode calls can be replaced with Koha::Items->find({ barcode => $barcode }); We should make sure the barcode existed in DB and so that ->find returns an object. Note that most of the time we just wanted to know if the barcode existed. The changes are very simple, the only one that need attention is the one in batchMod.pl. It is basically reusing what we did on bug 21141. Test plan: Use the batch item modification/deletion tools to modify/delete items from their barcode (using the textarea or a file) --- C4/ImportBatch.pm | 5 +++-- C4/Items.pm | 6 +++--- circ/returns.pl | 5 +++-- labels/label-edit-batch.pl | 9 +++++---- members/mancredit.pl | 4 +++- members/maninvoice.pl | 4 +++- rotating_collections/addItems.pl | 5 ++++- serials/serials-edit.pl | 9 ++++----- tools/batchMod.pl | 27 ++++++++------------------- 9 files changed, 36 insertions(+), 38 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 3727f4152d..99bc428701 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -27,6 +27,7 @@ use C4::Items; use C4::Charset; use C4::AuthoritiesMarc; use C4::MarcModificationTemplates; +use Koha::Items; use Koha::Plugins::Handler; use Koha::Logger; @@ -747,7 +748,7 @@ sub BatchCommitItems { my $item = TransformMarcToKoha( $item_marc ); - my $duplicate_barcode = exists( $item->{'barcode'} ) && GetItemnumberFromBarcode( $item->{'barcode'} ); + my $duplicate_barcode = exists( $item->{'barcode'} ) && Koha::Items->find({ barcode => $item->{'barcode'} }); my $duplicate_itemnumber = exists( $item->{'itemnumber'} ); my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ? WHERE import_items_id = ?"); @@ -761,7 +762,7 @@ sub BatchCommitItems { $updsth->finish(); $num_items_replaced++; } elsif ( $action eq "replace" && $duplicate_barcode ) { - my $itemnumber = GetItemnumberFromBarcode( $item->{'barcode'} ); + my $itemnumber = $duplicate_barcode->itemnumber; ModItemFromMarc( $item_marc, $biblionumber, $itemnumber ); $updsth->bind_param( 1, 'imported' ); $updsth->bind_param( 2, $item->{itemnumber} ); diff --git a/C4/Items.pm b/C4/Items.pm index 83a56672ba..a9d3889093 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -739,10 +739,10 @@ sub CheckItemPreSave { # check for duplicate barcode if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) { - my $existing_itemnumber = GetItemnumberFromBarcode($item_ref->{'barcode'}); - if ($existing_itemnumber) { + my $existing_item= Koha::Items->find({barcode => $item_ref->{'barcode'}}); + if ($existing_item) { if (!exists $item_ref->{'itemnumber'} # new item - or $item_ref->{'itemnumber'} != $existing_itemnumber) { # existing item + or $item_ref->{'itemnumber'} != $existing_item->itemnumber) { # existing item $errors{'duplicate_barcode'} = $item_ref->{'barcode'}; } } diff --git a/circ/returns.pl b/circ/returns.pl index a82bdc1432..cae78ff884 100755 --- a/circ/returns.pl +++ b/circ/returns.pl @@ -591,8 +591,9 @@ $template->param( AudioAlerts => C4::Context->preference("AudioAlerts"), ); -$itemnumber = GetItemnumberFromBarcode( $barcode ); -if ( $itemnumber ) { +my $item_from_barcode = Koha::Items->find({barcode => $barcode }); # How many times do we fetch this item?!? +if ( $item_from_barcode ) { + $itemnumber = $item_from_barcode->itemnumber; my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber ); if ( $holdingBranch and $collectionBranch ) { $holdingBranch //= ''; diff --git a/labels/label-edit-batch.pl b/labels/label-edit-batch.pl index 687535974c..0026ebe483 100755 --- a/labels/label-edit-batch.pl +++ b/labels/label-edit-batch.pl @@ -25,10 +25,12 @@ use CGI qw ( -utf8 ); use C4::Auth qw(get_template_and_user); use C4::Output qw(output_html_with_http_headers); -use C4::Items qw(GetItem GetItemnumberFromBarcode); +use C4::Items qw(GetItem); use C4::Creators; use C4::Labels; +use Koha::Items; + my $cgi = new CGI; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { @@ -90,9 +92,8 @@ elsif ($op eq 'add') { push @item_numbers, $number; } elsif ($number_type eq "barcode" ) { # we must test in case an invalid barcode is passed in; we effectively disgard them atm - if( my $item_number = GetItemnumberFromBarcode($number) ){ - push @item_numbers, $item_number; - } + my $item = Koha::Items->find({barcode => $number}); + push @item_numbers, $item->itemnumber if $item; } } } diff --git a/members/mancredit.pl b/members/mancredit.pl index 8fdef1e2b4..813461eaeb 100755 --- a/members/mancredit.pl +++ b/members/mancredit.pl @@ -34,6 +34,7 @@ use C4::Items; use C4::Members::Attributes qw(GetBorrowerAttributes); use Koha::Account; +use Koha::Items; use Koha::Patrons; use Koha::Patron::Categories; use Koha::Token; @@ -67,7 +68,8 @@ if ($add){ my $barcode = $input->param('barcode'); my $item_id; if ($barcode) { - $item_id = GetItemnumberFromBarcode($barcode); + my $item = Koha::Items->find({barcode => $barcode}); + $item_id = $item->itemnumber if $item; } my $description = $input->param('desc'); my $note = $input->param('note'); diff --git a/members/maninvoice.pl b/members/maninvoice.pl index a1be118b9e..db7a447ffa 100755 --- a/members/maninvoice.pl +++ b/members/maninvoice.pl @@ -33,6 +33,7 @@ use C4::Items; use C4::Members::Attributes qw(GetBorrowerAttributes); use Koha::Token; +use Koha::Items; use Koha::Patrons; use Koha::Patron::Categories; @@ -61,7 +62,8 @@ if ($add){ my $barcode=$input->param('barcode'); my $itemnum; if ($barcode) { - $itemnum = GetItemnumberFromBarcode($barcode); + my $item = Koha::Items->find({barcode => $barcode}); + $itemnum = $item->itemnumber if $item; } my $desc=$input->param('desc'); my $amount=$input->param('amount'); diff --git a/rotating_collections/addItems.pl b/rotating_collections/addItems.pl index cd24d50fd7..7d4fd644bb 100755 --- a/rotating_collections/addItems.pl +++ b/rotating_collections/addItems.pl @@ -24,6 +24,8 @@ use C4::Context; use C4::RotatingCollections; use C4::Items; +use Koha::Items; + use CGI qw ( -utf8 ); my $query = new CGI; @@ -44,7 +46,8 @@ if ( $query->param('action') eq 'addItem' ) { my $colId = $query->param('colId'); my $barcode = $query->param('barcode'); my $removeItem = $query->param('removeItem'); - my $itemnumber = GetItemnumberFromBarcode($barcode); + my $item = Koha::Items->find({barcode => $barcode}); + my $itemnumber = $item ? $item->itemnumber : undef; my ( $success, $errorCode, $errorMessage ); diff --git a/serials/serials-edit.pl b/serials/serials-edit.pl index e5f69e8192..928ff47ab3 100755 --- a/serials/serials-edit.pl +++ b/serials/serials-edit.pl @@ -72,7 +72,9 @@ use C4::Output; use C4::Context; use C4::Serials; use C4::Search qw/enabled_staff_search_views/; + use Koha::DateUtils; +use Koha::Items; use Koha::Serial::Items; use List::MoreUtils qw/uniq/; @@ -372,11 +374,8 @@ if ( $op and $op eq 'serialchangestatus' ) { ) ) { - $exists = GetItemnumberFromBarcode( - $bib_record->subfield( - $barcodetagfield, $barcodetagsubfield - ) - ); + my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield ); + $exists = Koha::Items->find({barcode => $barcode}); } # push @errors,"barcode_not_unique" if($exists); diff --git a/tools/batchMod.pl b/tools/batchMod.pl index 0e985a2c7b..3af72744eb 100755 --- a/tools/batchMod.pl +++ b/tools/batchMod.pl @@ -244,15 +244,10 @@ if ($op eq "show"){ @contentlist = uniq @contentlist; if ($filecontent eq 'barcode_file') { - foreach my $barcode (@contentlist) { - - my $itemnumber = GetItemnumberFromBarcode($barcode); - if ($itemnumber) { - push @itemnumbers,$itemnumber; - } else { - push @notfoundbarcodes, $barcode; - } - } + my $existing_items = Koha::Items->search({ itemnumber => \@contentlist }); + @itemnumbers = $existing_items->get_column('itemnumber'); + my %exists = map {$_=>1} @{$existing_items->get_column('barcode')}; + @notfoundbarcodes = grep { !$exists{$_} } @contentlist; } elsif ( $filecontent eq 'itemid_file') { @itemnumbers = Koha::Items->search({ itemnumber => \@contentlist })->get_column('itemnumber'); @@ -269,16 +264,10 @@ if ($op eq "show"){ if ( my $list=$input->param('barcodelist')){ push my @barcodelist, uniq( split(/\s\n/, $list) ); - foreach my $barcode (@barcodelist) { - - my $itemnumber = GetItemnumberFromBarcode($barcode); - if ($itemnumber) { - push @itemnumbers,$itemnumber; - } else { - push @notfoundbarcodes, $barcode; - } - } - + my $existing_items = Koha::Items->search({ barcode => \@barcodelist }); + @itemnumbers = $existing_items->get_column('itemnumber'); + my %exists = map {$_=>1} @{$existing_items->get_column('barcode')}; + @notfoundbarcodes = grep { !$exists{$_} } @barcodelist; } } -- 2.11.0