From 47b396f594451c796627d86e317ab48051790136 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 4 Aug 2020 12:12:56 +0200 Subject: [PATCH] Bug 26132: Remove raw SQL query for patron_maxissueqty Same as before but for patron_maxissueqty Signed-off-by: Nick Clemens --- C4/Circulation.pm | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index d0d67184db..c95be4bf89 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -427,12 +427,12 @@ sub TooMany { ); + my $patron = Koha::Patrons->find($borrower->{borrowernumber}); # if a rule is found and has a loan limit set, count # how many loans the patron already has that meet that # rule if (defined($maxissueqty_rule) and $maxissueqty_rule->rule_value ne "") { - my $patron = Koha::Patrons->find($borrower->{borrowernumber}); my $checkouts = $patron->checkouts->search( {}, { prefetch => 'item' } ); if ( $maxissueqty_rule->branchcode ) { if ( C4::Context->preference('CircControl') eq 'PickupLibrary' ) { @@ -517,38 +517,31 @@ 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 @bind_params = (); - my $branch_count_query = q| - SELECT COUNT(*) AS total, COALESCE(SUM(onsite_checkout), 0) AS onsite_checkouts - FROM issues - JOIN items USING (itemnumber) - WHERE borrowernumber = ? - |; - push @bind_params, $borrower->{borrowernumber}; - - if (C4::Context->preference('CircControl') eq 'PickupLibrary') { - $branch_count_query .= " AND issues.branchcode = ? "; - push @bind_params, $branch; + my $checkouts = $patron->checkouts->search({}, { prefetch => 'item' }); + if ( C4::Context->preference('CircControl') eq 'PickupLibrary' ) { + $checkouts = $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 { - $branch_count_query .= " AND items.homebranch = ? "; - push @bind_params, $branch; + $checkouts = $checkouts->search({ 'item.homebranch' => $branch }); } - my ( $checkout_count, $onsite_checkout_count ) = $dbh->selectrow_array( $branch_count_query, {}, @bind_params ); + + my $checkout_count = $checkouts->count; + my $onsite_checkout_count = $checkouts->search({ onsite_checkout => 1 })->count; my $max_checkouts_allowed = $branch_borrower_circ_rule->{patron_maxissueqty}; my $max_onsite_checkouts_allowed = $branch_borrower_circ_rule->{patron_maxonsiteissueqty} || undef; - my $qty_over = _check_max_qty({ - checkout_count => $checkout_count, - onsite_checkout_count => $onsite_checkout_count, - onsite_checkout => $onsite_checkout, - max_checkouts_allowed => $max_checkouts_allowed, - max_onsite_checkouts_allowed => $max_onsite_checkouts_allowed, - switch_onsite_checkout => $switch_onsite_checkout - }); + my $qty_over = _check_max_qty( + { + checkout_count => $checkout_count, + onsite_checkout_count => $onsite_checkout_count, + onsite_checkout => $onsite_checkout, + max_checkouts_allowed => $max_checkouts_allowed, + max_onsite_checkouts_allowed => $max_onsite_checkouts_allowed, + switch_onsite_checkout => $switch_onsite_checkout + } + ); return $qty_over if defined $qty_over; - } if ( not defined( $maxissueqty_rule ) and not defined($branch_borrower_circ_rule->{patron_maxissueqty}) ) { -- 2.11.0