From 9045c364d0954bd3e71c452208b73efebf9735cf Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 20 Jun 2024 12:01:34 +0100
Subject: [PATCH] Bug 37141: Add 'Show completed' option to bookings tab

This patch adds the 'Show completed' filter toggle option to the
bookings display tab on both the patron details and circulation pages.

Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
---
 .../prog/en/includes/patron-detail-tabs.inc   |  3 ++
 .../intranet-tmpl/prog/js/tables/bookings.js  | 37 +++++++++++++++----
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-detail-tabs.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-detail-tabs.inc
index eebb3d11114..d831c30673b 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-detail-tabs.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-detail-tabs.inc
@@ -228,6 +228,9 @@
 
             [% WRAPPER tab_panel tabname="bookings" %]
                 [% IF ( bookings_count ) %]
+                    <fieldset class="action filters" style="cursor:pointer;">
+                        <a id="expired_filter" class="filtered"><i class="fa fa-bars"></i> Show expired</a>
+                    </fieldset>
                     <table id="bookings_table" style="width: 100% !Important;"></table>
                 [% ELSE %]
                     <p>Patron has nothing booked.</p>
diff --git a/koha-tmpl/intranet-tmpl/prog/js/tables/bookings.js b/koha-tmpl/intranet-tmpl/prog/js/tables/bookings.js
index 46776432bfd..7461ce94885 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/tables/bookings.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/tables/bookings.js
@@ -4,8 +4,21 @@ var bookings_table;
 $(document).ready(function () {
     // Load bookings table on tab selection
     $("#bookings-tab").on("click", function () {
+        let filter_expired = true;
+        let additional_filters = {
+            patron_id: patron_borrowernumber,
+            end_date: function () {
+                if (filter_expired) {
+                    let today = new Date();
+                    return { ">=": today.toISOString() }
+                } else {
+                    return;
+                }
+            }
+        };
+
         if (!bookings_table) {
-            var today = new Date();
+
             var bookings_table_url = "/api/v1/bookings";
             bookings_table = $("#bookings_table").kohaTable(
                 {
@@ -87,13 +100,23 @@ $(document).ready(function () {
                         },
                     ],
                 },
-                table_settings_bookings_table,
-                0,
-                {
-                    patron_id: patron_borrowernumber,
-                    end_date: { ">=": today.toISOString() },
-                }
+                table_settings_bookings_table, 0, additional_filters
             );
         }
     });
+
+    var txtActivefilter = _("Show expired");
+    var txtInactivefilter = _("Hide expired");
+    $("#expired_filter").on("click", function () {
+        if ($(this).hasClass('filtered')) {
+            filter_expired = false;
+            $(this).html('<i class="fa fa-filter"></i> ' + txtInactivefilter);
+        } else {
+            filter_expired = true;
+            $(this).html('<i class="fa fa-bars"></i> ' + txtActivefilter);
+        }
+        bookings_table.DataTable().draw();
+        $(this).toggleClass('filtered');
+    });
+
 });
-- 
2.39.2