From acfd230678fac31edbbf04ccd2052cc89e27edf0 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Tue, 4 Aug 2020 13:27:06 +0100
Subject: [PATCH] Bug 24786: (follow-up) Cache the register_name in the session

This leads to one DB hit less per page load in the staff client when
cash registers are enabled.
---
 C4/Auth.pm                                    | 16 ++++++++------
 C4/Context.pm                                 | 21 ++++++++++---------
 Koha/Template/Plugin/Registers.pm             |  5 +++--
 circ/set-library.pl                           |  6 +++---
 .../prog/en/modules/circ/set-library.tt       |  4 ++--
 5 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index c80d518c23..64b3f27e6c 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -38,6 +38,7 @@ use Koha::Checkouts;
 use Koha::DateUtils qw(dt_from_string);
 use Koha::Library::Groups;
 use Koha::Libraries;
+use Koha::Cash::Registers;
 use Koha::Desks;
 use Koha::Patrons;
 use Koha::Patron::Consents;
@@ -847,7 +848,7 @@ sub checkauth {
                 $session->param('branchname'),   $session->param('flags'),
                 $session->param('emailaddress'), $session->param('shibboleth'),
                 $session->param('desk_id'),      $session->param('desk_name'),
-                $session->param('register_id')
+                $session->param('register_id'),  $session->param('register_name')
             );
             C4::Context::set_shelves_userenv( 'bar', $session->param('barshelves') );
             C4::Context::set_shelves_userenv( 'pub', $session->param('pubshelves') );
@@ -1068,7 +1069,7 @@ sub checkauth {
                 }
                 my ( $borrowernumber, $firstname, $surname, $userflags,
                     $branchcode, $branchname, $emailaddress, $desk_id,
-                    $desk_name, $register_id );
+                    $desk_name, $register_id, $register_name );
 
                 if ( $return == 1 ) {
                     my $select = "
@@ -1119,6 +1120,8 @@ sub checkauth {
                     }
                     if ( $query->param('register_id') ) {
                         $register_id = $query->param('register_id');
+                        my $register = Koha::Cash::Registers->find($register_id);
+                        $register_name = $register ? $register->name : '';
                     }
                     my $branches = { map { $_->branchcode => $_->unblessed } Koha::Libraries->search };
                     if ( $type ne 'opac' and C4::Context->boolean_preference('AutoLocation') ) {
@@ -1164,6 +1167,7 @@ sub checkauth {
                     $session->param( 'interface',    $type);
                     $session->param( 'shibboleth',   $shibSuccess );
                     $session->param( 'register_id',  $register_id );
+                    $session->param( 'register_name',  $register_name );
                     $debug and printf STDERR "AUTH_4: (%s)\t%s %s - %s\n", map { $session->param($_) } qw(cardnumber firstname surname branch);
                 }
                 $session->param('cas_ticket', $cas_ticket) if $cas_ticket;
@@ -1174,7 +1178,7 @@ sub checkauth {
                     $session->param('branchname'),   $session->param('flags'),
                     $session->param('emailaddress'), $session->param('shibboleth'),
                     $session->param('desk_id'),      $session->param('desk_name'),
-                    $session->param('register_id')
+                    $session->param('register_id'),  $session->param('register_name')
                 );
 
             }
@@ -1453,7 +1457,7 @@ sub check_api_auth {
                 $session->param('branchname'),   $session->param('flags'),
                 $session->param('emailaddress'), $session->param('shibboleth'),
                 $session->param('desk_id'),      $session->param('desk_name'),
-                $session->param('register_id')
+                $session->param('register_id'),  $session->param('register_name')
             );
 
             my $ip       = $session->param('ip');
@@ -1615,7 +1619,7 @@ sub check_api_auth {
                 $session->param('branchname'),   $session->param('flags'),
                 $session->param('emailaddress'), $session->param('shibboleth'),
                 $session->param('desk_id'),      $session->param('desk_name'),
-                $session->param('register_id')
+                $session->param('register_id'),  $session->param('register_name')
             );
             return ( "ok", $cookie, $sessionID );
         } else {
@@ -1705,7 +1709,7 @@ sub check_cookie_auth {
             $session->param('branchname'),   $session->param('flags'),
             $session->param('emailaddress'), $session->param('shibboleth'),
             $session->param('desk_id'),      $session->param('desk_name'),
-            $session->param('register_id')
+            $session->param('register_id'),  $session->param('register_name')
         );
 
         my $ip       = $session->param('ip');
diff --git a/C4/Context.pm b/C4/Context.pm
index c390572711..07728ee0c4 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -809,7 +809,7 @@ sub userenv {
                            $userbranch, $branchname, $userflags,
                            $emailaddress, $shibboleth
                            $desk_id, $desk_name,
-                           $register_id);
+                           $register_id, $register_name);
 
 Establish a hash of user environment variables.
 
@@ -820,7 +820,7 @@ set_userenv is called in Auth.pm
 #'
 sub set_userenv {
     shift @_;
-    my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $shibboleth, $desk_id, $desk_name, $register_id)=
+    my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $shibboleth, $desk_id, $desk_name, $register_id, $register_name)=
     map { Encode::is_utf8( $_ ) ? $_ : Encode::decode('UTF-8', $_) } # CGI::Session doesn't handle utf-8, so we decode it here
     @_;
     my $var=$context->{"activeuser"} || '';
@@ -832,14 +832,15 @@ sub set_userenv {
         "surname"    => $usersurname,
 
         #possibly a law problem
-        "branch"       => $userbranch,
-        "branchname"   => $branchname,
-        "flags"        => $userflags,
-        "emailaddress" => $emailaddress,
-        "shibboleth"   => $shibboleth,
-        "desk_id"      => $desk_id,
-        "desk_name"    => $desk_name,
-        "register_id"  => $register_id,
+        "branch"        => $userbranch,
+        "branchname"    => $branchname,
+        "flags"         => $userflags,
+        "emailaddress"  => $emailaddress,
+        "shibboleth"    => $shibboleth,
+        "desk_id"       => $desk_id,
+        "desk_name"     => $desk_name,
+        "register_id"   => $register_id,
+        "register_name" => $register_name
     };
     $context->{userenv}->{$var} = $cell;
     return $cell;
diff --git a/Koha/Template/Plugin/Registers.pm b/Koha/Template/Plugin/Registers.pm
index ea38dff1eb..d68c834e0a 100644
--- a/Koha/Template/Plugin/Registers.pm
+++ b/Koha/Template/Plugin/Registers.pm
@@ -37,8 +37,9 @@ sub session_register_id {
 sub session_register_name {
     my ($self) = @_;
 
-    my $register = Koha::Cash::Registers->find($self->session_register_id);
-    return $register ? $register->name : '';
+    return C4::Context->userenv
+      ? C4::Context->userenv->{'register_name'}
+      : '';
 }
 
 =head2
diff --git a/circ/set-library.pl b/circ/set-library.pl
index bba39ae6d9..0911629e31 100755
--- a/circ/set-library.pl
+++ b/circ/set-library.pl
@@ -82,14 +82,14 @@ if ( $branch and my $library = Koha::Libraries->find($branch) ) {
     if ( defined($userenv_register_id)
         && ( $userenv_register_id ne $register_id ) )
     {
+        my $old_register_name = C4::Context->userenv->{'register_name'} || '';
         my $register = Koha::Cash::Registers->find($register_id);
-        $template->param(LoginRegisterID   => $register_id);   # update template for new register
-        $template->param(LoginRegisterName   => $register ? $register->name : '');   # update template for new register
         $session->param( 'register_id', $register_id );
+        $session->param( 'register_name', $register ? $register->name : '' );
         push @updated,
           {
             updated_register => 1,
-            new_register     => $register ? $register->name : ''
+            old_register     => $old_register_name
           };
     }
     $session->flush();
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/set-library.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/set-library.tt
index 49db4c3da7..be0003b93d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/set-library.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/set-library.tt
@@ -45,14 +45,14 @@
 Updated:<ul>
     [% FOREACH update IN updated %]
     [% IF ( update.updated_branch || update.updated_desk ||  update.updated_register ) %]
-        [% IF ( updated.updated.branch ) %]
+        [% IF ( updated.updated_branch ) %]
         <li>Library: [% update.old_branch or "?" | html %] &rArr; [% update.new_branch or "?" | html %]</li>
         [% END %]
         [% IF ( update.updated_desk ) %]
         <li>Desk: [% update.old_desk or "?" | html %] &rArr; [% LoginDeskname or "?" | html %]</li>
         [% END %]
         [% IF ( updated.updated_register ) %]
-        <li>Register: [% updated.new_register | html %]</li>
+        <li>Register: [% updated.old_register or "?" | html %] &rArr; [% Registers.session_register_name or "?" | html %]</li>
         [% END %]
     [% ELSE %]
         <li>ERROR - unknown</li>
-- 
2.20.1