From 87afa5142bd12c0d5ff630dbf27d8703ca0ea131 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 7 Dec 2016 02:29:01 +0000 Subject: [PATCH] Bug 17736: Replace GetReservesFromBiblionumber with Koha::Biblio->holds The C4::Reserve::GetReservesFromBiblionumber took 3 parameters, the biblionumber, an optional itemnumber and a "all_dates" flag. If set, the subroutine returned all the holds placed on a given bibliographic record, even the ones placed in the future. Almost all of the calls had this flag set, they will be replaced with a call to Koha::Biblio->holds. But 5 did not have it: - C4::Biblio::DelBiblio -tools/batch_delete_records.pl => These 2 were wrong, we want to retrieve the holds to cancel them before deleting the record. We need to get all the holds, even the ones placed in the future /!\ CHANGE IN THE BEHAVIOR - acqui/parcel.pl => 1 call per item were made to this subroutine. They have been replaced with only 1 call to the new method Koha::Biblios->holds_placed_before_today Then we filter on the itemnumbers. I think this is wrong: we need the number of holds to know if the record can be deleted, so even if future holds exist, the deletion should not be possible. - serials/routing-preview.pl - C4::ILSDI::Services::GetRecords - C4::SIP::ILS::Item->new => Seems ok, we just one to display holds placed before today Test plan: I would suggest to test this patch with patches from bug 17737 and bug 17738, to place different kind of holds (biblio and item level, future and past). Then do a whole workflow to detect bug, view a record, delete record, order, place a hold on an item which has been ordered, etc. The hold's informations should always be the same without or without these patches. Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall --- C4/Biblio.pm | 8 +++-- C4/ILSDI/Services.pm | 8 +++-- C4/SIP/ILS/Item.pm | 12 +++----- acqui/parcel.pl | 12 ++++---- catalogue/ISBDdetail.pl | 6 ++-- catalogue/MARCdetail.pl | 6 ++-- catalogue/detail.pl | 6 ++-- catalogue/imageviewer.pl | 6 ++-- catalogue/labeledMARCdetail.pl | 6 ++-- catalogue/moredetail.pl | 8 ++--- .../en/modules/tools/batch_delete_records.tt | 4 +-- opac/opac-detail.pl | 18 +++++++----- opac/opac-reserve.pl | 10 +++---- serials/routing-preview.pl | 27 ++++++++--------- t/db_dependent/Holds.t | 29 ++++++++++--------- t/db_dependent/Reserves.t | 25 ++++++++-------- t/db_dependent/UsageStats.t | 7 +++-- tools/batch_delete_records.pl | 19 ++++++++---- 18 files changed, 116 insertions(+), 101 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 4b33d9a131..f475056f9c 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -43,6 +43,7 @@ use Koha::Authority::Types; use Koha::Acquisition::Currencies; use Koha::Biblio::Metadata; use Koha::Biblio::Metadatas; +use Koha::Holds; use Koha::SearchEngine; use Koha::Libraries; @@ -406,10 +407,11 @@ sub DelBiblio { } # We delete any existing holds + my $biblio = Koha::Biblios->find( $biblionumber ); + my $holds = $biblio->holds; require C4::Reserves; - my $reserves = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber }); - foreach my $res ( @$reserves ) { - C4::Reserves::CancelReserve({ reserve_id => $res->{'reserve_id'} }); + while ( my $hold = $holds->next ) { + C4::Reserves::CancelReserve({ reserve_id => $hold->reserve_id }); # TODO Replace with $hold->cancel } # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio diff --git a/C4/ILSDI/Services.pm b/C4/ILSDI/Services.pm index b735d38791..64630b6b80 100644 --- a/C4/ILSDI/Services.pm +++ b/C4/ILSDI/Services.pm @@ -25,7 +25,7 @@ use C4::Items; use C4::Circulation; use C4::Accounts; use C4::Biblio; -use C4::Reserves qw(AddReserve GetReservesFromBiblionumber GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved IsAvailableForItemLevelRequest); +use C4::Reserves qw(AddReserve GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved IsAvailableForItemLevelRequest); use C4::Context; use C4::AuthoritiesMarc; use XML::Simple; @@ -35,6 +35,7 @@ use DateTime; use C4::Auth; use C4::Members::Attributes qw(GetBorrowerAttributes); +use Koha::Biblios; use Koha::Libraries; =head1 NAME @@ -217,7 +218,8 @@ sub GetRecords { # Get most of the needed data my $biblioitemnumber = $biblioitem->{'biblioitemnumber'}; - my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber }); + my $biblio = Koha::Biblios->find( $biblionumber ); + my $holds = $biblio->holds_placed_before_today->unblessed; my $issues = GetBiblioIssues($biblionumber); my $items = GetItemsByBiblioitemnumber($biblioitemnumber); @@ -236,7 +238,7 @@ sub GetRecords { # Hashref building... $biblioitem->{'items'}->{'item'} = $items; - $biblioitem->{'reserves'}->{'reserve'} = $reserves; + $biblioitem->{'reserves'}->{'reserve'} = $holds; $biblioitem->{'issues'}->{'issue'} = $issues; push @records, $biblioitem; diff --git a/C4/SIP/ILS/Item.pm b/C4/SIP/ILS/Item.pm index 578f3828a4..c7b8ea7d0a 100644 --- a/C4/SIP/ILS/Item.pm +++ b/C4/SIP/ILS/Item.pm @@ -22,6 +22,7 @@ use C4::Circulation; use C4::Members; use C4::Reserves; use Koha::Database; +use Koha::Biblios; =encoding UTF-8 @@ -65,12 +66,6 @@ use Koha::Database; ); =cut -sub priority_sort { - defined $a->{priority} or return -1; - defined $b->{priority} or return 1; - return $a->{priority} <=> $b->{priority}; -} - sub new { my ($class, $item_id) = @_; my $type = ref($class) || $class; @@ -99,8 +94,9 @@ sub new { } my $borrower = GetMember(borrowernumber=>$issue->{'borrowernumber'}); $item->{patron} = $borrower->{'cardnumber'}; - my $reserves = GetReservesFromBiblionumber({ biblionumber => $item->{biblionumber} }); - $item->{hold_queue} = [ sort priority_sort @$reserves ]; + my $biblio = Koha::Biblios->find( $item->{biblionumber } ); + my $holds = $biblio->holds_placed_before_today->unblessed; + $item->{hold_queue} = $holds; $item->{hold_shelf} = [( grep { defined $_->{found} and $_->{found} eq 'W' } @{$item->{hold_queue}} )]; $item->{pending_queue} = [( grep {(! defined $_->{found}) or $_->{found} ne 'W' } @{$item->{hold_queue}} )]; $self = $item; diff --git a/acqui/parcel.pl b/acqui/parcel.pl index 4e11f2b18e..a747aa5208 100755 --- a/acqui/parcel.pl +++ b/acqui/parcel.pl @@ -65,11 +65,11 @@ use C4::Items; use CGI qw ( -utf8 ); use C4::Output; use C4::Suggestions; -use C4::Reserves qw/GetReservesFromBiblionumber/; use Koha::Acquisition::Bookseller; use Koha::Biblios; use Koha::DateUtils; +use Koha::Biblios; use JSON; @@ -140,10 +140,12 @@ for my $order ( @orders ) { $line{invoice} = $invoice->{invoicenumber}; $line{holds} = 0; my @itemnumbers = GetItemnumbersFromOrder( $order->{ordernumber} ); - for my $itemnumber ( @itemnumbers ) { - my $holds = GetReservesFromBiblionumber({ biblionumber => $line{biblionumber}, itemnumber => $itemnumber }); - $line{holds} += scalar( @$holds ); - } + my $biblio = Koha::Biblios->find( $order->{ordernumber} ); + $line{holds} = $biblio->holds_placed_before_today->search( + { + itemnumber => { -in => \@itemnumbers }, + } + )->count; $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} ); $line{tax_value} = $line{tax_value_on_receiving}; diff --git a/catalogue/ISBDdetail.pl b/catalogue/ISBDdetail.pl index bb9a29cd07..60b6053dc4 100755 --- a/catalogue/ISBDdetail.pl +++ b/catalogue/ISBDdetail.pl @@ -168,9 +168,9 @@ $template->param (countorders => $count_orders_using_biblio); my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ; $template->param (countdeletedorders => $count_deletedorders_using_biblio); -my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 }); -my $holdcount = scalar( @$holds ); -$template->param( holdcount => scalar ( @$holds ) ); +my $biblio = Koha::Biblios->find( $biblionumber ); +my $holds = $biblio->holds; +$template->param( holdcount => $holds->count ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/catalogue/MARCdetail.pl b/catalogue/MARCdetail.pl index 8b32935c78..6a6bdb4278 100755 --- a/catalogue/MARCdetail.pl +++ b/catalogue/MARCdetail.pl @@ -343,8 +343,8 @@ $template->param (countorders => $count_orders_using_biblio); my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ; $template->param (countdeletedorders => $count_deletedorders_using_biblio); -my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 }); -my $holdcount = scalar( @$holds ); -$template->param( holdcount => scalar ( @$holds ) ); +$biblio = Koha::Biblios->find( $biblionumber ); +my $holds = $biblio->holds; +$template->param( holdcount => $holds->count ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/catalogue/detail.pl b/catalogue/detail.pl index a31a0c5fac..30436612db 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -42,6 +42,7 @@ use C4::HTML5Media; use C4::CourseReserves qw(GetItemCourseReservesInfo); use C4::Acquisition qw(GetOrdersByBiblionumber); use Koha::AuthorisedValues; +use Koha::Biblios; use Koha::Patrons; use Koha::Virtualshelves; @@ -461,8 +462,9 @@ if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->pref } #we only need to pass the number of holds to the template -my $holds = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 }); -$template->param( holdcount => scalar ( @$holds ) ); +my $biblio = Koha::Biblios->find( $biblionumber ); +my $holds = $biblio->holds; +$template->param( holdcount => $holds->count ); my $StaffDetailItemSelection = C4::Context->preference('StaffDetailItemSelection'); if ($StaffDetailItemSelection) { diff --git a/catalogue/imageviewer.pl b/catalogue/imageviewer.pl index c5c4cd0a44..3317db1909 100755 --- a/catalogue/imageviewer.pl +++ b/catalogue/imageviewer.pl @@ -110,8 +110,8 @@ $template->param (countorders => $count_orders_using_biblio); my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ; $template->param (countdeletedorders => $count_deletedorders_using_biblio); -my $holds= C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 }); -my $holdcount = scalar( @$holds ); -$template->param( holdcount => scalar ( @$holds ) ); +$biblio = Koha::Biblios->find( $biblionumber ); +my $holds = $biblio->holds; +$template->param( holdcount => $holds->count ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/catalogue/labeledMARCdetail.pl b/catalogue/labeledMARCdetail.pl index 6b554f46ba..cd07f73135 100755 --- a/catalogue/labeledMARCdetail.pl +++ b/catalogue/labeledMARCdetail.pl @@ -153,8 +153,8 @@ $template->param (countorders => $count_orders_using_biblio); my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ; $template->param (countdeletedorders => $count_deletedorders_using_biblio); -my $holds= C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 }); -my $holdcount = scalar( @$holds ); -$template->param( holdcount => scalar ( @$holds ) ); +$biblio = Koha::Biblios->find( $biblionumber ); +my $holds = $biblio->holds; +$template->param( holdcount => $holds->count ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/catalogue/moredetail.pl b/catalogue/moredetail.pl index 125b103006..b63c53249c 100755 --- a/catalogue/moredetail.pl +++ b/catalogue/moredetail.pl @@ -32,10 +32,10 @@ use C4::Auth; use C4::Serials; use C4::Members; # to use GetMember use C4::Search; # enabled_staff_search_views -use C4::Reserves qw(GetReservesFromBiblionumber); use Koha::Acquisition::Booksellers; use Koha::AuthorisedValues; +use Koha::Biblios; use Koha::DateUtils; use Koha::Items; use Koha::Patrons; @@ -258,9 +258,9 @@ $template->param (countorders => $count_orders_using_biblio); my $count_deletedorders_using_biblio = scalar @deletedorders_using_biblio ; $template->param (countdeletedorders => $count_deletedorders_using_biblio); -my $holds = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 }); -my $holdcount = scalar( @$holds ); -$template->param( holdcount => scalar ( @$holds ) ); +my $biblio = Koha::Biblios->find( $biblionumber ); +my $holds = $biblio->holds; +$template->param( holdcount => $holds->count ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_delete_records.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_delete_records.tt index 4d0eb181f4..58249ee857 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_delete_records.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_delete_records.tt @@ -181,11 +181,11 @@ $(document).ready(function() { [% FOR biblio IN records %] - + [% INCLUDE 'biblio-default-view.inc' biblionumber=biblio.biblionumber %][% biblio.title %][% IF ( biblio.subtitle ) %][% FOREACH subtitle IN biblio.subtitle %] [% subtitle.subfield |html %][% END %][% END %] [% biblio.itemnumbers.size %] - [% biblio.reserves.size %] + [% biblio.holds_count %] [% biblio.issues_count %] [% END %] diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index a7e643c4e1..1c9f73b3a9 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -49,6 +49,7 @@ use C4::HTML5Media; use C4::CourseReserves qw(GetItemCourseReservesInfo); use Koha::RecordProcessor; use Koha::AuthorisedValues; +use Koha::Biblios; use Koha::Virtualshelves; use Koha::Ratings; use Koha::Reviews; @@ -591,15 +592,16 @@ for ( C4::Context->preference("OPACShowHoldQueueDetails") ) { } my $has_hold; if ( $show_holds_count || $show_priority) { - my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 }); - $template->param( holds_count => scalar( @$reserves ) ) if $show_holds_count; - foreach (@$reserves) { - $item_reserves{ $_->{itemnumber} }++ if $_->{itemnumber}; - if ($show_priority && $_->{borrowernumber} == $borrowernumber) { + my $biblio = Koha::Biblios->find( $biblionumber ); + my $holds = $biblio->holds; + $template->param( holds_count => $holds->count ); + while ( my $hold = $holds->new ) { + $item_reserves{ $hold->itemnumber }++ if $hold->itemnumber; + if ($show_priority && $hold->borrowernumber == $borrowernumber) { $has_hold = 1; - $_->{itemnumber} - ? ($priority{ $_->{itemnumber} } = $_->{priority}) - : ($template->param( priority => $_->{priority} )); + $hold->itemnumber + ? ($priority{ $hold->itemnumber } = $hold->priority) + : ($template->param( priority => $hold->priority )); } } } diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl index 743a3ca60c..650326f7d3 100755 --- a/opac/opac-reserve.pl +++ b/opac/opac-reserve.pl @@ -172,12 +172,12 @@ foreach my $biblioNumber (@biblionumbers) { } # Compute the priority rank. - my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblioNumber, all_dates => 1 }); - my $rank = scalar( @$reserves ); + my $biblio = Koha::Biblios->find( $biblioNumber ); + my $holds = $biblio->holds; + my $rank = $holds->count; $biblioData->{reservecount} = 1; # new reserve - foreach my $res (@{$reserves}) { - my $found = $res->{found}; - if ( $found && $found eq 'W' ) { + while ( my $hold = $holds->next ) { + if ( $hold->is_waiting ) { $rank--; } else { diff --git a/serials/routing-preview.pl b/serials/routing-preview.pl index 9c4238fed2..47d373f5e3 100755 --- a/serials/routing-preview.pl +++ b/serials/routing-preview.pl @@ -33,6 +33,8 @@ use C4::Biblio; use C4::Items; use C4::Serials; use URI::Escape; + +use Koha::Biblios; use Koha::Libraries; my $query = new CGI; @@ -62,38 +64,37 @@ my ($template, $loggedinuser, $cookie); if($ok){ # get biblio information.... - my $biblio = $subs->{'biblionumber'}; - my ($count2,@bibitems) = GetBiblioItemByBiblioNumber($biblio); + my $biblionumber = $subs->{'biblionumber'}; + my ($count2,@bibitems) = GetBiblioItemByBiblioNumber($biblionumber); my @itemresults = GetItemsInfo( $subs->{biblionumber} ); my $branch = $itemresults[0]->{'holdingbranch'}; my $branchname = Koha::Libraries->find($branch)->branchname; if (C4::Context->preference('RoutingListAddReserves')){ # get existing reserves ..... - my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblio }); - my $count = scalar( @$reserves ); - my $totalcount = $count; - foreach my $res (@$reserves) { - if ($res->{'found'} eq 'W') { - $count--; - } - } + + my $biblio = Koha::Biblios->find( $biblionumber ); + my $holds = $biblio->holds_placed_before_today; + my $count = $holds->count; + while ( my $hold = $holds->next ) { + $count-- if $hold->is_waiting; + } my $notes; my $title = $subs->{'bibliotitle'}; for my $routing ( @routinglist ) { my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? LIMIT 1'); - $sth->execute($biblio,$routing->{borrowernumber}); + $sth->execute($biblionumber,$routing->{borrowernumber}); my $reserve = $sth->fetchrow_hashref; if($routing->{borrowernumber} == $reserve->{borrowernumber}){ ModReserve({ rank => $routing->{ranking}, - biblionumber => $biblio, + biblionumber => $biblionumber, borrowernumber => $routing->{borrowernumber}, branchcode => $branch }); } else { - AddReserve($branch,$routing->{borrowernumber},$biblio,\@bibitems,$routing->{ranking}, undef, undef, $notes,$title); + AddReserve($branch,$routing->{borrowernumber},$biblionumber,\@bibitems,$routing->{ranking}, undef, undef, $notes,$title); } } } diff --git a/t/db_dependent/Holds.t b/t/db_dependent/Holds.t index 61b9e6e751..b58753f858 100755 --- a/t/db_dependent/Holds.t +++ b/t/db_dependent/Holds.t @@ -15,6 +15,7 @@ use C4::Members; use C4::Calendar; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Biblios; use Koha::Holds; BEGIN { @@ -84,14 +85,14 @@ foreach my $borrowernumber ( @borrowernumbers ) { ); } -my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber }); -is( scalar(@$reserves), $borrowers_count, "Test GetReserves()" ); - -is( $reserves->[0]->{priority}, 1, "Reserve 1 has a priority of 1" ); -is( $reserves->[1]->{priority}, 2, "Reserve 2 has a priority of 2" ); -is( $reserves->[2]->{priority}, 3, "Reserve 3 has a priority of 3" ); -is( $reserves->[3]->{priority}, 4, "Reserve 4 has a priority of 4" ); -is( $reserves->[4]->{priority}, 5, "Reserve 5 has a priority of 5" ); +my $biblio = Koha::Biblios->find( $biblionumber ); +my $holds = $biblio->holds; +is( $holds->count, $borrowers_count, 'Test GetReserves()' ); +is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" ); +is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" ); +is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" ); +is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" ); +is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" ); my ( $reservedate, $borrowernumber, $branch_1code, $reserve_id ) = GetReservesFromItemnumber($itemnumber); is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "GetReservesFromItemnumber should return a valid reserve date"); @@ -122,8 +123,8 @@ ok( GetReserveCount( $borrowernumbers[0] ), "Test GetReserveCount()" ); CancelReserve({ 'reserve_id' => $reserve_id }); -$reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber }); -is( scalar(@$reserves), $borrowers_count - 1, "Test CancelReserve()" ); +$holds = $biblio->holds; +is( $holds->count, $borrowers_count - 1, "Test CancelReserve()" ); ( $reservedate, $borrowernumber, $branch_1code, $reserve_id ) = GetReservesFromItemnumber($itemnumber); @@ -202,9 +203,9 @@ my $reserve2 = GetReserveInfo( $reserve->{'reserve_id'} ); ok( $reserve->{'reserve_id'} eq $reserve2->{'reserve_id'}, "Test GetReserveInfo()" ); -$reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 }); -$reserve = $reserves->[1]; -AlterPriority( 'top', $reserve->{'reserve_id'} ); +$holds = $biblio->holds; +my $hold = $holds->next; +AlterPriority( 'top', $hold->reserve_id ); $reserve = GetReserve( $reserve->{'reserve_id'} ); is( $reserve->{'priority'}, '1', "Test AlterPriority(), move to top" ); @@ -429,7 +430,7 @@ t::lib::Mocks::mock_preference('ReservesMaxPickUpDelay', 1); my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time); $year += 1900; $mon += 1; -$reserves = $dbh->selectall_arrayref('SELECT * FROM reserves', { Slice => {} }); +my $reserves = $dbh->selectall_arrayref('SELECT * FROM reserves', { Slice => {} }); $reserve = $reserves->[0]; my $calendar = C4::Calendar->new(branchcode => $reserve->{branchcode}); $calendar->insert_single_holiday( diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index 536354d25e..1ab65d25f5 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 72; +use Test::More tests => 71; use Test::MockModule; use Test::Warn; @@ -268,14 +268,13 @@ AddReserve($branch_1, $requesters{$branch_1}, $bibnum2, ModReserveAffect($itemnum_cpl, $requesters{$branch_3}, 0); # Now it should have different priorities. -my $title_reserves = GetReservesFromBiblionumber({biblionumber => $bibnum2}); -# Sort by reserve number in case the database gives us oddly ordered results -my @reserves = sort { $a->{reserve_id} <=> $b->{reserve_id} } @$title_reserves; -is($reserves[0]{priority}, 0, 'Item is correctly waiting'); -is($reserves[1]{priority}, 1, 'Item is correctly priority 1'); -is($reserves[2]{priority}, 2, 'Item is correctly priority 2'); - -@reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting(); +my $biblio = Koha::Biblios->find( $bibnum2 ); +my $holds = $biblio->holds({}, { order_by => 'reserve_id' });; +is($holds->next->priority, 0, 'Item is correctly waiting'); +is($holds->next->priority, 1, 'Item is correctly priority 1'); +is($holds->next->priority, 2, 'Item is correctly priority 2'); + +my @reserves = Koha::Holds->search({ borrowernumber => $requesters{$branch_3} })->waiting(); is( @reserves, 1, 'GetWaiting got only the waiting reserve' ); is( $reserves[0]->borrowernumber(), $requesters{$branch_3}, 'GetWaiting got the reserve for the correct borrower' ); @@ -315,10 +314,10 @@ is( $messages->{ResFound}->{borrowernumber}, $requesters{$branch_3}, 'for generous library, its items fill first hold request in line (bug 10272)'); -my $reserves = GetReservesFromBiblionumber({biblionumber => $biblionumber}); -isa_ok($reserves, 'ARRAY'); -is(scalar @$reserves, 1, "Only one reserves for this biblio"); -my $reserve_id = $reserves->[0]->{reserve_id}; +$biblio = Koha::Biblios->find( $biblionumber ); +$holds = $biblio->holds; +is($holds->count, 1, "Only one reserves for this biblio"); +my $reserve_id = $holds->next->reserve_id; $reserve = GetReserve($reserve_id); isa_ok($reserve, 'HASH', "GetReserve return"); diff --git a/t/db_dependent/UsageStats.t b/t/db_dependent/UsageStats.t index 2625efa2da..6c1992594a 100644 --- a/t/db_dependent/UsageStats.t +++ b/t/db_dependent/UsageStats.t @@ -19,6 +19,7 @@ use Test::More tests => 57; use t::lib::Mocks qw(mock_preference); use POSIX qw(strftime); use Data::Dumper; +use Koha::Biblios; use Koha::Libraries; @@ -299,9 +300,9 @@ sub construct_objects_needed { # ---------- Add 1 old_reserves AddReserve( $branchcode, $borrowernumber1, $biblionumber1, '', 1, undef, undef, '', 'Title', undef, undef ); - my $reserves1 = GetReservesFromBiblionumber( { biblionumber => $biblionumber1 } ); - my $reserve_id1 = $reserves1->[0]->{reserve_id}; - my $reserve1 = CancelReserve( { reserve_id => $reserve_id1 } ); + my $biblio = Koha::Biblios->find( $biblionumber1 ); + my $holds = $biblio->holds; + CancelReserve( { reserve_id => $holds->next->reserve_id } ); # ---------- Add 1 aqbudgets $query = ' diff --git a/tools/batch_delete_records.pl b/tools/batch_delete_records.pl index 2b82796541..195f767e7f 100755 --- a/tools/batch_delete_records.pl +++ b/tools/batch_delete_records.pl @@ -28,6 +28,8 @@ use C4::Output; use C4::AuthoritiesMarc; use C4::Biblio; +use Koha::Biblios; + my $input = new CGI; my $dbh = C4::Context->dbh; my $op = $input->param('op') // q|form|; @@ -68,7 +70,7 @@ if ( $op eq 'form' ) { for my $record_id ( uniq @record_ids ) { if ( $recordtype eq 'biblio' ) { # Retrieve biblio information - my $biblio = C4::Biblio::GetBiblio( $record_id ); + my $biblio = Koha::Biblios->find( $record_id ); unless ( $biblio ) { push @messages, { type => 'warning', @@ -77,10 +79,12 @@ if ( $op eq 'form' ) { }; next; } + my $holds_count = $biblio->holds->count; + $biblio = $biblio->unblessed; my $record = &GetMarcBiblio( $record_id ); $biblio->{subtitle} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $record_id ) ); $biblio->{itemnumbers} = C4::Items::GetItemnumbersForBiblio( $record_id ); - $biblio->{reserves} = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $record_id }); + $biblio->{holds_count} = $holds_count; $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id ); push @records, $biblio; } else { @@ -127,6 +131,9 @@ if ( $op eq 'form' ) { my $biblionumber = $record_id; # First, checking if issues exist. # If yes, nothing to do + my $biblio = Koha::Biblios->find( $biblionumber ); + + # TODO Replace with $biblio->get_issues->count if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) { push @messages, { type => 'warning', @@ -138,17 +145,17 @@ if ( $op eq 'form' ) { } # Cancel reserves - my $reserves = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber }); - for my $reserve ( @$reserves ) { + my $holds = $biblio->holds; + while ( my $hold = $holds->next ) { eval{ - C4::Reserves::CancelReserve( { reserve_id => $reserve->{reserve_id} } ); + C4::Reserves::CancelReserve( { reserve_id => $hold->reserve_id } ); }; if ( $@ ) { push @messages, { type => 'error', code => 'reserve_not_cancelled', biblionumber => $biblionumber, - reserve_id => $reserve->{reserve_id}, + reserve_id => $hold->reserve_id, error => $@, }; $dbh->rollback; -- 2.39.5