Bug 21183: Replace C4::Items::GetItemnumberFromBarcode calls
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 6 Aug 2018 18:53:33 +0000 (15:53 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 30 Aug 2018 13:40:35 +0000 (13:40 +0000)
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)

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

C4/ImportBatch.pm
C4/Items.pm
circ/returns.pl
labels/label-edit-batch.pl
members/mancredit.pl
members/maninvoice.pl
rotating_collections/addItems.pl
serials/serials-edit.pl
tools/batchMod.pl

index 3727f41..99bc428 100644 (file)
@@ -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} );
index 2f588c4..f4e6d65 100644 (file)
@@ -745,10 +745,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'};
             }
         }
index a726e59..dada0c4 100755 (executable)
@@ -593,8 +593,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 //= '';
index 6875359..0026ebe 100755 (executable)
@@ -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;
             }
         }
     }
index 8fdef1e..813461e 100755 (executable)
@@ -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');
index a1be118..db7a447 100755 (executable)
@@ -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');
index cd24d50..7d4fd64 100755 (executable)
@@ -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 );
 
index e5f69e8..928ff47 100755 (executable)
@@ -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);
index 503f5ce..c6c424f 100755 (executable)
@@ -245,15 +245,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');
@@ -270,16 +265,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;
         }
     }