From 2861d291c3478a5c2f2ba89fa30280484f743a82 Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Mon, 12 Jun 2017 09:17:26 +0200 Subject: [PATCH] Revert "Bug 18295: C4::Items - Remove get_itemnumbers_of" This reverts commit 42a73053001dc033ea50bd386496b28021906148. --- C4/Items.pm | 51 ++++++++++++++++++++++++++++++++++--- cataloguing/merge.pl | 13 +++++----- labels/label-item-search.pl | 34 +++++++++++++++---------- reserve/request.pl | 6 +++-- 4 files changed, 78 insertions(+), 26 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index 1d3db7a3cb..6c2757078d 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -76,6 +76,7 @@ BEGIN { GetItemsLocationInfo GetHostItemsInfo GetItemnumbersForBiblio + get_itemnumbers_of get_hostitemnumbers_of GetItemnumberFromBarcode GetBarcodeFromItemnumber @@ -1391,6 +1392,41 @@ sub GetItemnumbersForBiblio { return \@items; } +=head2 get_itemnumbers_of + + my @itemnumbers_of = get_itemnumbers_of(@biblionumbers); + +Given a list of biblionumbers, return the list of corresponding itemnumbers +for each biblionumber. + +Return a reference on a hash where keys are biblionumbers and values are +references on array of itemnumbers. + +=cut + +sub get_itemnumbers_of { + my @biblionumbers = @_; + + my $dbh = C4::Context->dbh; + + my $query = ' + SELECT itemnumber, + biblionumber + FROM items + WHERE biblionumber IN (?' . ( ',?' x scalar @biblionumbers - 1 ) . ') + '; + my $sth = $dbh->prepare($query); + $sth->execute(@biblionumbers); + + my %itemnumbers_of; + + while ( my ( $itemnumber, $biblionumber ) = $sth->fetchrow_array ) { + push @{ $itemnumbers_of{$biblionumber} }, $itemnumber; + } + + return \%itemnumbers_of; +} + =head2 get_hostitemnumbers_of my @itemnumbers_of = get_hostitemnumbers_of($biblionumber); @@ -1426,9 +1462,18 @@ sub get_hostitemnumbers_of { foreach my $hostfield ( $marcrecord->field($tag) ) { my $hostbiblionumber = $hostfield->subfield($biblio_s); my $linkeditemnumber = $hostfield->subfield($item_s); - my $is_from_biblio = Koha::Items->search({ itemnumber => $linkeditemnumber, biblionumber => $hostbiblionumber }); - push @returnhostitemnumbers, $linkeditemnumber - if $is_from_biblio; + my @itemnumbers; + if ( my $itemnumbers = + get_itemnumbers_of($hostbiblionumber)->{$hostbiblionumber} ) + { + @itemnumbers = @$itemnumbers; + } + foreach my $itemnumber (@itemnumbers) { + if ( $itemnumber eq $linkeditemnumber ) { + push( @returnhostitemnumbers, $itemnumber ); + last; + } + } } return @returnhostitemnumbers; diff --git a/cataloguing/merge.pl b/cataloguing/merge.pl index 11bb4e2cb4..ee9f1fa067 100755 --- a/cataloguing/merge.pl +++ b/cataloguing/merge.pl @@ -31,7 +31,6 @@ use C4::Reserves qw/MergeHolds/; use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/; use Koha::BiblioFrameworks; -use Koha::Items; use Koha::MetadataRecord; my $input = new CGI; @@ -88,14 +87,14 @@ if ($merge) { # Moving items from the other record to the reference record foreach my $biblionumber (@biblionumbers) { - my $items = Koha::Items->search({ biblionumber => $biblionumber }); - while ( my $item = $items->next) { - my $res = MoveItemFromBiblio( $item->itemnumber, $biblionumber, $ref_biblionumber ); - if ( not defined $res ) { - push @notmoveditems, $item->itemnumber; - } + my $itemnumbers = get_itemnumbers_of($biblionumber); + foreach my $itemnumber (@{ $itemnumbers->{$biblionumber} }) { + my $res = MoveItemFromBiblio($itemnumber, $biblionumber, $ref_biblionumber); + if (not defined $res) { + push @notmoveditems, $itemnumber; } } + } # If some items could not be moved : if (scalar(@notmoveditems) > 0) { my $itemlist = join(' ',@notmoveditems); diff --git a/labels/label-item-search.pl b/labels/label-item-search.pl index d3189871d9..3ef8dbff2a 100755 --- a/labels/label-item-search.pl +++ b/labels/label-item-search.pl @@ -30,12 +30,11 @@ use C4::Output qw(output_html_with_http_headers); use C4::Context; use C4::Search qw(SimpleSearch); use C4::Biblio qw(TransformMarcToKoha); -use C4::Items qw(GetItemInfosOf); +use C4::Items qw(GetItemInfosOf get_itemnumbers_of); use C4::Creators::Lib qw(html_table); use C4::Debug; - use Koha::DateUtils; -use Koha::Items; + use Koha::ItemTypes; use Koha::SearchEngine::Search; @@ -140,21 +139,28 @@ if ($show_results) { push (@results_set, $biblio); my $biblionumber = $biblio->{'biblionumber'}; #DEBUG Notes: Grab the item numbers associated with this MARC record... - my $items = Koha::Items->search({ biblionumber => $biblionumber }, { order_by => { -desc => 'itemnumber' }}); + my $itemnums = get_itemnumbers_of($biblionumber); #DEBUG Notes: Retrieve the item data for each number... - while ( my $item = $items->next ) { - #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data... - if ( $item->biblionumber eq $results_set[$i]->{'biblionumber'} ) { - my $item_data; - $item_data->{'_item_number'} = $item->itemnumber; - $item_data->{'_item_call_number'} = ( $item->itemcallnumber || 'NA' ); - $item_data->{'_date_accessioned'} = $item->dateaccessioned; - $item_data->{'_barcode'} = ( $item->barcode || 'NA' ); - $item_data->{'_add'} = $item->itemnumber; - push @row_data, $item_data; + if (my $iii = $itemnums->{$biblionumber}) { + my $item_results = GetItemInfosOf(@$iii); + foreach my $item ( keys %$item_results ) { + #DEBUG Notes: Build an array element 'item' of the correct bib (results) hash which contains item-specific data... + if ($item_results->{$item}->{'biblionumber'} eq $results_set[$i]->{'biblionumber'}) { + my $item_data; + $item_data->{'_item_number'} = $item_results->{$item}->{'itemnumber'}; + $item_data->{'_item_call_number'} = ($item_results->{$item}->{'itemcallnumber'} ? $item_results->{$item}->{'itemcallnumber'} : 'NA'); + $item_data->{'_date_accessioned'} = $item_results->{$item}->{'dateaccessioned'}; + $item_data->{'_barcode'} = ( $item_results->{$item}->{'barcode'} ? $item_results->{$item}->{'barcode'} : 'NA'); + $item_data->{'_add'} = $item_results->{$item}->{'itemnumber'}; + unshift (@row_data, $item_data); # item numbers are given to us in descending order by get_itemnumbers_of()... + } } $results_set[$i]->{'item_table'} = html_table($display_columns, \@row_data); } + else { + # FIXME: Some error trapping code needed + warn sprintf('No item numbers retrieved for biblio number: %s', $biblionumber); + } } ( $template, $loggedinuser, $cookie ) = get_template_and_user( diff --git a/reserve/request.pl b/reserve/request.pl index b33ae70b3a..ba4eb0adef 100755 --- a/reserve/request.pl +++ b/reserve/request.pl @@ -293,10 +293,12 @@ foreach my $biblionumber (@biblionumbers) { my $fixedRank = $count+1; my %itemnumbers_of_biblioitem; + my @itemnumbers; ## $items is array of 'item' table numbers - my $items = Koha::Items->search({ biblionumber => $biblionumber }); - my @itemnumbers = $items->get_column('itemnumber'); + if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){ + @itemnumbers = @$items; + } my @hostitems = get_hostitemnumbers_of($biblionumber); if (@hostitems){ $template->param('hostitemsflag' => 1); -- 2.39.5