From e745808733e848e2f9798d9d314fe87210ec2ff9 Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Fri, 4 Mar 2022 16:48:16 -1000 Subject: [PATCH] Bug 30232: Logged in user performance for CanUserUseBudget() Some acquisition methods can take borrowernumber or a Koha::Patron->unblessed. When called with logged in user, sometimes in a loop, performance will be better if fetching Koha::Patron of logged in user only once. This patch optimises call for CanUserUseBudget(); You may benchmark on a user with a lot of budgets acess. Test plan : Play with impacted pages and check budgets list is the same than wihtout patch --- acqui/acqui-home.pl | 5 ++++- acqui/basket.pl | 7 ++++--- acqui/booksellers.pl | 2 +- acqui/invoice.pl | 6 ++++-- acqui/invoices.pl | 6 +++++- acqui/parcels.pl | 6 +++++- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/acqui/acqui-home.pl b/acqui/acqui-home.pl index 05eb16bf06..ea48eece42 100755 --- a/acqui/acqui-home.pl +++ b/acqui/acqui-home.pl @@ -46,6 +46,9 @@ my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user( } ); +my $logged_in_patron = Koha::Patrons->find($loggedinuser); +my $logged_in_patron_unblessed = $logged_in_patron->unblessed; + my $status = $query->param('status') || "ASKED"; # Get current branch count and total viewable count, if they don't match then pass # both to template @@ -77,7 +80,7 @@ my $totavail_active = 0; my @budget_loop; foreach my $budget ( @{$budget_arr} ) { - next unless (CanUserUseBudget($loggedinuser, $budget, $userflags)); + next unless (CanUserUseBudget($logged_in_patron_unblessed, $budget, $userflags)); my $patron = Koha::Patrons->find( $budget->{budget_owner_id} ); if ( $patron ) { diff --git a/acqui/basket.pl b/acqui/basket.pl index 0710861946..f61127f83d 100755 --- a/acqui/basket.pl +++ b/acqui/basket.pl @@ -85,7 +85,8 @@ our ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user( } ); -my $logged_in_patron = Koha::Patrons->find( $loggedinuser ); +my $logged_in_patron = Koha::Patrons->find($loggedinuser); +my $logged_in_patron_unblessed = $logged_in_patron->unblessed; our $basket = GetBasket($basketno); $booksellerid = $basket->{booksellerid} unless $booksellerid; @@ -95,7 +96,7 @@ my $rs = $schema->resultset('VendorEdiAccount')->search( { vendor_id => $booksellerid, } ); $template->param( ediaccount => ($rs->count > 0)); -unless (CanUserManageBasket($logged_in_patron->unblessed, $basket, $userflags)) { +unless (CanUserManageBasket($logged_in_patron_unblessed, $basket, $userflags)) { $template->param( cannot_manage_basket => 1, basketno => $basketno, @@ -370,7 +371,7 @@ if ( $op eq 'list' ) { my $budgets = GetBudgetHierarchy; my $has_budgets = 0; foreach my $r (@{$budgets}) { - next unless (CanUserUseBudget($loggedinuser, $r, $userflags)); + next unless (CanUserUseBudget($logged_in_patron_unblessed, $r, $userflags)); $has_budgets = 1; last; diff --git a/acqui/booksellers.pl b/acqui/booksellers.pl index 959f829d48..ce285c45f0 100755 --- a/acqui/booksellers.pl +++ b/acqui/booksellers.pl @@ -108,7 +108,7 @@ my $userbranch = $userenv->{branch}; my $budgets = GetBudgetHierarchy; my $has_budgets = 0; foreach my $r (@{$budgets}) { - next unless (CanUserUseBudget($loggedinuser, $r, $userflags)); + next unless (CanUserUseBudget($logged_in_patron_unblessed, $r, $userflags)); $has_budgets = 1; last; diff --git a/acqui/invoice.pl b/acqui/invoice.pl index 7240d9319f..5da329e3ef 100755 --- a/acqui/invoice.pl +++ b/acqui/invoice.pl @@ -52,7 +52,9 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( } ); -my $logged_in_patron = Koha::Patrons->find( $loggedinuser ); +my $logged_in_patron = Koha::Patrons->find($loggedinuser); +my $logged_in_patron_unblessed = $logged_in_patron->unblessed; + my $invoiceid = $input->param('invoiceid'); my $op = $input->param('op'); @@ -279,7 +281,7 @@ my $shipmentcost_budgetid = $details->{shipmentcost_budgetid}; my $budget_loop = []; my $budgets = GetBudgetHierarchy(); foreach my $r ( @{$budgets} ) { - next unless ( CanUserUseBudget( $loggedinuser, $r, $flags ) ); + next unless ( CanUserUseBudget( $logged_in_patron_unblessed, $r, $flags ) ); my $selected = $shipmentcost_budgetid ? $r->{budget_id} eq $shipmentcost_budgetid : 0; diff --git a/acqui/invoices.pl b/acqui/invoices.pl index 9720ead33e..173eac24a4 100755 --- a/acqui/invoices.pl +++ b/acqui/invoices.pl @@ -36,6 +36,7 @@ use C4::Acquisition qw( GetInvoices GetInvoice ); use C4::Budgets qw( GetBudget GetBudgets CanUserUseBudget ); use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Acquisition::Booksellers; +use Koha::Patrons; my $input = CGI->new; my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( @@ -47,6 +48,9 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( } ); +my $logged_in_patron = Koha::Patrons->find($loggedinuser); +my $logged_in_patron_unblessed = $logged_in_patron->unblessed; + my $invoicenumber = $input->param('invoicenumber'); my $supplierid = $input->param('supplierid'); my $shipmentdatefrom = $input->param('shipmentdatefrom'); @@ -107,7 +111,7 @@ foreach (@suppliers) { my $budgets = GetBudgets(); my @budgets_loop; foreach my $budget (@$budgets) { - push @budgets_loop, $budget if CanUserUseBudget( $loggedinuser, $budget, $flags ); + push @budgets_loop, $budget if CanUserUseBudget( $logged_in_patron_unblessed, $budget, $flags ); } my (@openedinvoices, @closedinvoices); diff --git a/acqui/parcels.pl b/acqui/parcels.pl index cd6e2562b1..c6f1f8168c 100755 --- a/acqui/parcels.pl +++ b/acqui/parcels.pl @@ -76,6 +76,7 @@ use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget ); use Koha::Acquisition::Booksellers; use Koha::DateUtils qw( output_pref dt_from_string ); +use Koha::Patrons; my $input = CGI->new; my $booksellerid = $input->param('booksellerid'); @@ -96,6 +97,9 @@ our ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( } ); +my $logged_in_patron = Koha::Patrons->find($loggedinuser); +my $logged_in_patron_unblessed = $logged_in_patron->unblessed; + my $invoicenumber = $input->param('invoice'); my $shipmentcost = $input->param('shipmentcost'); my $shipmentcost_budgetid = $input->param('shipmentcost_budgetid'); @@ -179,7 +183,7 @@ if ($count_parcels) { my $budget_loop = []; my $budgets = GetBudgetHierarchy; foreach my $r (@{$budgets}) { - next unless (CanUserUseBudget($loggedinuser, $r, $flags)); + next unless (CanUserUseBudget($logged_in_patron_unblessed, $r, $flags)); push @{$budget_loop}, { b_id => $r->{budget_id}, b_txt => $r->{budget_name}, -- 2.35.1