From af485d0819e8d25efb7da532465b7b56b8bd0a15 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 Content-Type: text/plain; charset=utf-8 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 --- C4/Biblio.pm | 8 +++--- C4/ILSDI/Services.pm | 8 +++--- C4/SIP/ILS/Item.pm | 12 +++------ acqui/parcel.pl | 12 +++++---- catalogue/ISBDdetail.pl | 8 +++--- catalogue/MARCdetail.pl | 8 +++--- catalogue/detail.pl | 6 +++-- catalogue/imageviewer.pl | 8 +++--- catalogue/labeledMARCdetail.pl | 7 +++--- catalogue/moredetail.pl | 8 +++--- .../prog/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, 123 insertions(+), 101 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index abb3bed..b42875d 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -41,6 +41,7 @@ use C4::Debug; use Koha::Caches; use Koha::Authority::Types; use Koha::Acquisition::Currencies; +use Koha::Holds; use Koha::SearchEngine; use Koha::Libraries; @@ -438,10 +439,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 23ec85a..d6d7630 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 578f382..c7b8ea7 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 22a036f..8d59182 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 4679b9e..6a663f8 100755 --- a/catalogue/ISBDdetail.pl +++ b/catalogue/ISBDdetail.pl @@ -47,6 +47,8 @@ use C4::Members; # to use GetMember use C4::Serials; # CountSubscriptionFromBiblionumber use C4::Search; # enabled_staff_search_views use C4::Acquisition qw(GetOrdersByBiblionumber); + +use Koha::Biblios; use Koha::RecordProcessor; @@ -165,9 +167,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 09f9778..be7107e 100755 --- a/catalogue/MARCdetail.pl +++ b/catalogue/MARCdetail.pl @@ -59,6 +59,8 @@ use C4::Acquisition; use C4::Members; # to use GetMember use C4::Serials; #uses getsubscriptionsfrombiblionumber GetSubscriptionsFromBiblionumber use C4::Search; # enabled_staff_search_views + +use Koha::Biblios; use Koha::BiblioFrameworks; use List::MoreUtils qw( uniq ); @@ -340,8 +342,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 a31a0c5..3043661 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 fe64ecc..16492a0 100755 --- a/catalogue/imageviewer.pl +++ b/catalogue/imageviewer.pl @@ -29,6 +29,8 @@ use C4::Images; use C4::Search; use C4::Acquisition qw(GetOrdersByBiblionumber); +use Koha::Biblios; + my $query = new CGI; my ( $template, $borrowernumber, $cookie ) = get_template_and_user( { @@ -107,8 +109,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 c4b9019..d494ffc 100755 --- a/catalogue/labeledMARCdetail.pl +++ b/catalogue/labeledMARCdetail.pl @@ -31,6 +31,7 @@ use C4::Members; # to use GetMember use C4::Search; # enabled_staff_search_views use C4::Acquisition qw(GetOrdersByBiblionumber); +use Koha::Biblios; use Koha::BiblioFrameworks; my $query = new CGI; @@ -151,8 +152,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 125b103..b63c532 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 7962a9f..6fa0ddc 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 @@ -173,11 +173,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 a7e643c..1c9f73b 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 f1fe2ff..0b92e27 100755 --- a/opac/opac-reserve.pl +++ b/opac/opac-reserve.pl @@ -163,12 +163,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 9c4238f..47d373f 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 61b9e6e..b58753f 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 37d90d9..c5d2c06 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; @@ -260,14 +260,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' ); @@ -307,10 +306,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 bb15e88..3290f3a 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; BEGIN { use_ok('C4::UsageStats'); @@ -290,9 +291,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 2c2d32e..32b9e32 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.1.4