From c86497d472ae377311991c031343a7c008af5b9d Mon Sep 17 00:00:00 2001
From: Sam Lau <samalau@gmail.com>
Date: Tue, 11 Jun 2024 16:39:05 +0000
Subject: [PATCH] Bug 37047: Make Patron Bookings table visible from Patrons
 Checkout page

This patch makes it so that the patron bookings table loads properly from teh checkout page. Previously, it was only loading correctly from the details page.

To test:
1. Create an item bookable.
2. Book an item for a patron.
3. Go to the patron's checkout page.
4. See they have a Booking (1) in their tab.
5. Click this tab, but nothing appears.
6. Apply patch, restart_all
7. See they still have a Booking (1) in their tab.
8. Click this tab, table loads as expected.

Signed-off-by: Pedro Amorim <pedro.amorim@ptfs-europe.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 .../prog/en/modules/circ/circulation.tt       | 104 ++++++++++++++++++
 1 file changed, 104 insertions(+)

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 c124dcb6cfb..76859fe0980 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -986,6 +986,7 @@
         </div>
     </div>
 [% END %]
+[% INCLUDE modals/cancel_booking.inc %]
 
 [% MACRO jsinclude BLOCK %]
     [% INCLUDE 'datatables.inc' %]
@@ -1026,10 +1027,113 @@
     [% END %]
     [% Asset.js("js/holds.js") | $raw %]
     [% INCLUDE 'calendar.inc' %]
+    [% Asset.js("js/cancel_booking_modal.js") | $raw %]
+    [% INCLUDE 'js-biblio-format.inc' %]
     <script>
         table_settings_issues_table = [% TablesSettings.GetTableSettings( 'circ', 'circulation', 'issues-table', 'json' ) | $raw %]
         table_settings_relatives_issues_table = [% TablesSettings.GetTableSettings( 'circ', 'circulation', 'relatives-issues-table', 'json' ) | $raw %]
         table_settings_holds_table = [% TablesSettings.GetTableSettings( 'circ', 'circulation', 'holds-table', 'json' ) | $raw %]
+        table_settings_bookings_table = [% TablesSettings.GetTableSettings( 'members', 'moremember', 'bookings-table', 'json' ) | $raw %]
+
+        var bookings_table;
+        $(document).ready(function() {
+            $("#info_digests").tooltip();
+
+            $("#finesholdsissues a[data-toggle='tab']").on("shown.bs.tab", function(e){
+                var oTable = $('div.dataTables_wrapper > table', $(e.target.hash) ).dataTable();
+                if ( oTable.length > 0 ) {
+                    oTable.fnAdjustColumnSizing();
+                }
+            });
+
+            $("#view_restrictions").on("click",function(){
+                $('#reldebarments-tab').click();
+            });
+
+            $("#view_guarantees_finesandcharges").on("click",function(){
+                $('#guarantees_finesandcharges-tab').click();
+            });
+
+            // Bookings
+            // Load bookings table on tab selection
+            $("#bookings-tab").on( "click", function(){
+                if ( !bookings_table ) {
+                    var today = new Date();
+                    var bookings_table_url = "/api/v1/bookings";
+                    bookings_table = $('#bookings_table').kohaTable({
+                        "ajax": {
+                            "url": bookings_table_url
+                        },
+                        "embed": [
+                            "biblio",
+                            "item",
+                            "patron"
+                        ],
+                        "columns": [{
+                            "data": "booking_id",
+                            "title": _("Booking ID"),
+                        },
+                        {
+                            "data": "biblio.title",
+                            "title": _("Title"),
+                            "searchable": true,
+                            "orderable": true,
+                            "render": function(data,type,row,meta) {
+                                return $biblio_to_html(row.biblio, {
+                                    link: 'bookings'
+                                });
+                            }
+                        },
+                        {
+                            "data": "item.external_id",
+                            "title": _("Item"),
+                            "searchable": true,
+                            "orderable": true,
+                            "defaultContent": _("Any item"),
+                            "render": function(data,type,row,meta) {
+                                if ( row.item ) {
+                                    return row.item.external_id + " (" + row.booking_id + ")";
+                                } else {
+                                    return null;
+                                }
+                            }
+                        },
+                        {
+                            "data": "start_date",
+                            "title": _("Start date"),
+                            "searchable": true,
+                            "orderable": true,
+                            "render": function(data, type, row, meta) {
+                                return $date(row.start_date);
+                            }
+                        },
+                        {
+                            "data": "end_date",
+                            "title": _("End date"),
+                            "searchable": true,
+                            "orderable": true,
+                            "render": function(data, type, row, meta) {
+                                return $date(row.end_date);
+                            }
+                        },
+                        {
+                            "data": "",
+                            "title": _("Actions"),
+                            "class": "actions",
+                            "searchable": false,
+                            "orderable": false,
+                            "render": function(data, type, row, meta) {
+                                let result = "";
+                                [% IF CAN_user_circulate_manage_bookings %]
+                                result += '<button type="button" class="btn btn-default btn-xs cancel-action" data-toggle="modal" data-target="#cancelBookingModal" data-booking="'+row.booking_id+'"><i class="fa fa-trash" aria-hidden="true"></i> '+_("Cancel")+'</button>';
+                                [% END %]
+                                return result;
+                            }
+                        }]
+                    }, table_settings_bookings_table, 0, { patron_id: "[% patron.borrowernumber | html %]", end_date: { ">=": today.toISOString() } });
+                };
+            });
+         });
 
         [% IF borrowernumber and patron %]
             if( Cookies.get("holdfor") != [% patron.borrowernumber | html %]){
-- 
2.45.2