Bug 26132: Don't prefetch if not needed
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 4 Aug 2020 10:21:26 +0000 (12:21 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 13 Aug 2020 08:15:33 +0000 (10:15 +0200)
We only need to prefetch items if CircControl is set to ItemHomeLibrary

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

C4/Circulation.pm

index 57c03d8..60c4171 100644 (file)
@@ -432,14 +432,17 @@ sub TooMany {
     # rule
     if (defined($maxissueqty_rule) and $maxissueqty_rule->rule_value ne "") {
 
-        my $checkouts = $patron->checkouts->search( {}, { prefetch => 'item' } );
+        my $checkouts;
         if ( $maxissueqty_rule->branchcode ) {
             if ( C4::Context->preference('CircControl') eq 'PickupLibrary' ) {
-                $checkouts = $checkouts->search({ 'me.branchcode' => $maxissueqty_rule->branchcode });
+                $checkouts = $patron->checkouts->search(
+                    { 'me.branchcode' => $maxissueqty_rule->branchcode } );
             } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
                 ; # if branch is the patron's home branch, then count all loans by patron
             } else {
-                $checkouts = $checkouts->search({ 'item.homebranch' => $maxissueqty_rule->branchcode });
+                $checkouts = $patron->checkouts->search(
+                    { 'item.homebranch' => $maxissueqty_rule->branchcode },
+                    { prefetch          => 'item' } );
             }
         }
         my $sum_checkouts;
@@ -516,13 +519,15 @@ sub TooMany {
     # Now count total loans against the limit for the branch
     my $branch_borrower_circ_rule = GetBranchBorrowerCircRule($branch, $cat_borrower);
     if (defined($branch_borrower_circ_rule->{patron_maxissueqty}) and $branch_borrower_circ_rule->{patron_maxissueqty} ne '') {
-        my $checkouts = $patron->checkouts->search({}, { prefetch => 'item' });
+        my $checkouts;
         if ( C4::Context->preference('CircControl') eq 'PickupLibrary' ) {
-            $checkouts = $checkouts->search({ 'me.branchcode' => $branch });
+            $checkouts = $patron->checkouts->search(
+                { 'me.branchcode' => $branch} );
         } elsif (C4::Context->preference('CircControl') eq 'PatronLibrary') {
             ; # if branch is the patron's home branch, then count all loans by patron
         } else {
-            $checkouts = $checkouts->search({ 'item.homebranch' => $branch });
+            $checkouts = $patron->checkouts->search(
+                { 'item.homebranch' => $branch} );
         }
 
         my $checkout_count = $checkouts->count;