From 298a5995b91b32e54980486e0a272a5959ebc3ed Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Wed, 7 Jun 2023 14:29:05 -0400
Subject: [PATCH] Bug 33945: Add ability to delay the loading of the current
 checkouts table on the checkouts page

If a librarian has opted to load the checkouts table automatically on the checkouts page, it will trigger a call to svc/checkouts. If a librarian is checkout out 10 items to a patron, that means svc/checkouts is called uselessly 9 times, with only the 10th time being used to display the checkouts table.

It would be useful to add a delay such that the table only load if the page has been display for a given number of seconds. That way the continuous scanning does not trigger useless svc/checkouts calls, but the librarian also does not need to click the load checkouts button manually.

Test Plan:
1) Apply this patch
2) Run updatedatabase.pl
3) Verify "Always show checkouts immediately" retains its' original
   behavior of loading the checkouts table immediately
4) Set LoadIssuesTableDelay to a non-zero integer
5) Verify the automatic table loading is delayed by that number of
   seconds

Signed-off-by: Sam Lau <samalau@gmail.com>
---
 installer/data/mysql/atomicupdate/bug_33945.pl    | 15 +++++++++++++++
 installer/data/mysql/mandatory/sysprefs.sql       |  1 +
 .../en/modules/admin/preferences/circulation.pref |  5 +++++
 .../prog/en/modules/circ/circulation.tt           |  1 +
 .../prog/en/modules/members/moremember.tt         |  1 +
 koha-tmpl/intranet-tmpl/prog/js/checkouts.js      |  9 ++++++++-
 6 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100755 installer/data/mysql/atomicupdate/bug_33945.pl

diff --git a/installer/data/mysql/atomicupdate/bug_33945.pl b/installer/data/mysql/atomicupdate/bug_33945.pl
new file mode 100755
index 0000000000..0a9f002077
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_33945.pl
@@ -0,0 +1,15 @@
+use Modern::Perl;
+
+return {
+    bug_number => "33945",
+    description => "Add system preference LoadIssuesTableDelay",
+    up => sub {
+        my ($args) = @_;
+        my ($dbh, $out) = @$args{qw(dbh out)};
+        # Do you stuffs here
+        $dbh->do(q{
+            INSERT IGNORE INTO systempreferences (`variable`,`value`,`options`,`explanation`,`type`)
+            VALUES ('LoadIssuesTableDelay','0','','Delay before auto-loading checkouts table on checkouts screen','Integer')
+        });
+    },
+};
diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql
index db1dcabd01..bf22bf191d 100644
--- a/installer/data/mysql/mandatory/sysprefs.sql
+++ b/installer/data/mysql/mandatory/sysprefs.sql
@@ -350,6 +350,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('LinkerRelink','1',NULL,'If ON the authority linker will relink headings that have previously been linked every time it runs.','YesNo'),
 ('ListOwnerDesignated', NULL, NULL, 'Designated list owner at patron deletion', 'Free'),
 ('ListOwnershipUponPatronDeletion', 'delete', 'delete|transfer', 'Defines the action on their public or shared lists when patron is deleted', 'Choice'),
+('LoadIssuesTableDelay','0','','Delay before auto-loading checkouts table on checkouts screen','Integer'),
 ('LoadSearchHistoryToTheFirstLoggedUser', '1', NULL, 'If ON, the next user will automatically get the last searches in their history', 'YesNo'),
 ('LocalCoverImages','0','1','Display local cover images on intranet details pages.','YesNo'),
 ('LocalHoldsPriority',  '0', NULL,  'Enables the LocalHoldsPriority feature',  'YesNo'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
index 6c3ee2c27b..31a876aee4 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
@@ -171,6 +171,11 @@ Circulation:
             - circulation desks with circulation.
 
     Checkout policy:
+        -
+            - Delay the automatic loading of the checkouts table on the checkouts page by
+            - pref: LoadIssuesTableDelay
+              class: integer
+            - seconds when "Always show checkouts immediately" is enabled.
         -
             - pref: OnSiteCheckoutAutoCheck
               choices:
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
index 6fc4e740c5..39391963c1 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -1039,6 +1039,7 @@
 
     <script>
         /* Set some variable needed in circulation.js */
+        const LoadIssuesTableDelay = [% Koha.Preference('LoadIssuesTableDelay') | html %];
         var logged_in_user_borrowernumber = "[% logged_in_user.borrowernumber | html %]";
         var ClaimReturnedLostValue = "[% Koha.Preference('ClaimReturnedLostValue') | html %]";
         var ClaimReturnedChargeFee = "[% Koha.Preference('ClaimReturnedChargeFee') | html %]";
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
index 6baa7e2ace..e74dce16d4 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
@@ -919,6 +919,7 @@
     [% Asset.js("js/members-menu.js") | $raw %]
     [% Asset.js("js/recalls.js") | $raw %]
     <script>
+        const LoadIssuesTableDelay = 0;
 
         table_settings_issues_table = [% TablesSettings.GetTableSettings( 'members', 'moremember', 'issues-table', 'json' ) | $raw %]
         table_settings_relatives_issues_table = [% TablesSettings.GetTableSettings( 'members', 'moremember', 'relatives-issues-table', 'json' ) | $raw %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js b/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
index 534fec1cda..d150a3db47 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
@@ -1,6 +1,8 @@
 /* global __ */
 
 $(document).ready(function() {
+    var loadIssuesTableDelayTimeoutId;
+
     var barcodefield = $("#barcode");
 
     var onHoldDueDateSet = false;
@@ -242,6 +244,7 @@ $(document).ready(function() {
         barcodefield.focus();
     });
     $('#issues-table-load-now-button').click(function(){
+        if ( loadIssuesTableDelayTimeoutId ) clearTimeout(loadIssuesTableDelayTimeoutId);
         LoadIssuesTable();
         barcodefield.focus();
         return false;
@@ -252,7 +255,11 @@ $(document).ready(function() {
     });
 
     if ( Cookies.get("issues-table-load-immediately-" + script) == "true" ) {
-        LoadIssuesTable();
+        if ( LoadIssuesTableDelay ) {
+            loadIssuesTableDelayTimeoutId = setTimeout( function(){ LoadIssuesTable() }, LoadIssuesTableDelay * 1000);
+        } else {
+            LoadIssuesTable();
+        }
         $('#issues-table-load-immediately').prop('checked', true);
     }
     $('#issues-table-load-immediately').on( "change", function(){
-- 
2.30.2