Bug 30155: Don't get items that can fillholds if there are no holds
authorNick Clemens <nick@bywatersolutions.com>
Tue, 22 Feb 2022 16:54:03 +0000 (16:54 +0000)
committerFridolin Somers <fridolin.somers@biblibre.com>
Wed, 16 Mar 2022 08:30:50 +0000 (22:30 -1000)
This makes two changes:
1 - We no longer call get_items_that_can_fill if there are no holds
2 - The subroutine will return an empty Koha::Items object if there are no holds passed

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
Koha/Holds.pm
circ/pendingreserves.pl
t/db_dependent/Koha/Holds.t

index 187e61b..5784cd7 100644 (file)
@@ -111,6 +111,9 @@ Items that are not:
 sub get_items_that_can_fill {
     my ( $self ) = @_;
 
+    return Koha::Items->new->empty()
+      unless $self->count() > 0;
+
     my @itemnumbers = $self->search({ 'me.itemnumber' => { '!=' => undef } })->get_column('itemnumber');
     my @biblionumbers = $self->search({ 'me.itemnumber' => undef })->get_column('biblionumber');
     my @bibs_or_items;
index 12e7c19..14f0dc8 100755 (executable)
@@ -193,8 +193,10 @@ my $holds = Koha::Holds->search(
 my @biblionumbers = $holds->get_column('biblionumber');
 
 my $all_items;
-foreach my $item ( $holds->get_items_that_can_fill->as_list ) {
-    push @{$all_items->{$item->biblionumber}}, $item;
+if ( $holds->count ) {
+    foreach my $item ( $holds->get_items_that_can_fill->as_list ) {
+        push @{ $all_items->{ $item->biblionumber } }, $item;
+    }
 }
 
 # patrons count per biblio
index e3c2735..2d99ded 100755 (executable)
@@ -411,7 +411,7 @@ subtest 'Desks' => sub {
 };
 
 subtest 'get_items_that_can_fill' => sub {
-    plan tests => 3;
+    plan tests => 5;
 
     my $biblio = $builder->build_sample_biblio;
     my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' }); # For 1, 2, 3, 4
@@ -503,6 +503,11 @@ subtest 'get_items_that_can_fill' => sub {
     is_deeply( [ map { $_->itemnumber } $items->as_list ],
         [ $item_2->itemnumber ], 'Only item 2 is available for filling the hold' );
 
+    my $no_holds = Koha::Holds->new->empty();
+    my $no_items = $no_holds->get_items_that_can_fill();
+    is( ref $no_items, "Koha::Items", "Routine returns a Koha::Items object");
+    is( $no_items->count, 0, "Object is empty when called on no holds");
+
 };
 
 subtest 'set_waiting+patron_expiration_date' => sub {