From 97620ae5f1f5dce3c7962954042be450c95dc8f2 Mon Sep 17 00:00:00 2001
From: Emmi Takkinen <emmi.takkinen@koha-suomi.fi>
Date: Mon, 25 Mar 2024 09:56:25 +0200
Subject: [PATCH] Bug 36135: Add several new features

This patch adds following new features to batch
modify holds tool.

- Do not automatically select login library as search param
- Add biblios title and items barcode to found holds table
- Add count of found holds at top of the table
- Add ability to do not change pick up library
- Display those hold rows with some found status in different
color

Sponsored-by: Koha-Suomi Oy
---
 .../en/modules/tools/batch_modify_holds.tt    | 28 ++++++++++++-------
 tools/batch_modify_holds.pl                   |  9 ++++--
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_modify_holds.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_modify_holds.tt
index 6b96176498..d9a6ddef49 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_modify_holds.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_modify_holds.tt
@@ -61,7 +61,7 @@
                                     <li>
                                         <label for="branchcodes">Libraries:</label>
                                         <select name="branchcodes" id="branchcodes" multiple="multiple">
-                                            [% PROCESS options_for_libraries libraries => Branches.all() %]
+                                            [% PROCESS options_for_libraries libraries => Branches.all(selected => "") %]
                                         </select>
                                     </li>
                                     <li>
@@ -108,6 +108,7 @@
                         <form action="/cgi-bin/koha/tools/batch_modify_holds.pl" method="post" id="process">
                             [% INCLUDE 'csrf-token.inc' %]
                         [% IF holds.count %]
+                            <div><p>Found [% holds.count | html %][% IF holds.count > 1 %] holds [% ELSE %] hold [% END %]to modify.</p><div>
                             <div class="page-section">
                                 <div class="btn-toolbar selections-toolbar">
                                     <a id="selectall" href="#"><i class="fa fa-check"></i> Select all</a>
@@ -119,6 +120,8 @@
                                             <th>&nbsp;</th>
                                             <th>Expiration date</th>
                                             <th>Patron expiration date</th>
+                                            <th>Title</title>
+                                            <th>Barcode</th>
                                             <th>Status</th>
                                             <th>Hold pickup library</th>
                                             <th>Suspended</th>
@@ -128,10 +131,12 @@
                                     </thead>
                                     <tbody>
                                         [% FOREACH hold IN holds %]
-                                            <tr>
+                                            <tr [% IF hold.found %]class="highlighted-row"[% END %]>
                                                 <td><input type="checkbox" name="hold_id" value="[% hold.reserve_id | html %]"/></td>
                                                 <td>[% hold.expirationdate | $KohaDates %]</td>
                                                 <td>[% hold.patron_expiration_date | $KohaDates %]</td>
+                                                <td><a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% hold.item.biblio.biblionumber | uri %]">[% hold.item.biblio.title | html %]</a></td>
+                                                <td><a href="/cgi-bin/koha/catalogue/moredetail.pl?itemnumber=[% hold.itemnumber | uri %]&biblionumber=[% hold.item.biblio.biblionumber | uri %]&bi=[% hold.item.biblio.biblionumber | uri %]#item[% hold.itemnumber | uri %]">[% hold.item.barcode | html %]</a></td>
                                                 <td class="found_status" data-found="[% hold.found | html %]">
                                                     [% IF hold.found == "T" %]
                                                         In transit
@@ -169,8 +174,9 @@
                                             <input type="text" id="new_expiration_date" name="new_expiration_date" class="flatpickr" data-flatpickr-futuredate="true"/>
                                         </td>
                                         <td>
-                                            <select name="new_pickup_loc" id="new_pickup_loc">
-                                                [% PROCESS options_for_libraries libraries => Branches.pickup_locations({ selected => hold.branchcode }) %]
+                                            <select id="new_pickup_loc" name="new_pickup_loc">
+                                                <option value="" selected="selected"></option>
+                                                [% PROCESS options_for_libraries libraries => Branches.pickup_locations(selected = "") %]
                                             </select>
                                         </td>
                                         <td>
@@ -206,10 +212,6 @@
                             Holds have been modified!
                         </div>
                         <div class="page-section">
-                            <div class="btn-toolbar selections-toolbar">
-                                <a id="selectall" href="#"><i class="fa fa-check"></i> Select all</a>
-                                <a id="clearall" href="#"><i class="fa fa-remove"></i> Clear all</a>
-                            </div>
                             <table id="holds">
                                 <thead>
                                     <tr>
@@ -282,6 +284,7 @@
             $("#process").on('submit', function(e) {
                 var reserve_ids = $("input[type=checkbox][name='hold_id']:checked");
 
+                var new_pickup_loc = $("#new_pickup_loc").val();
                 var new_suspend_status = $("#new_suspend_status").val();
                 var new_suspend_date = $("#new_suspend_date").val();
 
@@ -290,11 +293,16 @@
                     alert(_("Please select at least one hold to process."));
                     return false;
                 }
-                if( new_suspend_status ){
+                if( new_pickup_loc || new_suspend_status ){
                     reserve_ids.each(function(){
                         if($(this).parents("tr").children(".found_status").data("found") != ""){
                             e.preventDefault();
-                            alert(_("One or more holds have found status and can't be suspended."));
+                            if( new_suspend_status ){
+                                alert(_("One or more holds have found status and can't be suspended."));
+                            }
+                            if( new_pickup_loc ){
+                                alert(_("One or more holds have found status and their pick up location can't be changed."));
+                            }
                             return false;
                         }
                     })
diff --git a/tools/batch_modify_holds.pl b/tools/batch_modify_holds.pl
index 839f682eca..c1438c3c61 100755
--- a/tools/batch_modify_holds.pl
+++ b/tools/batch_modify_holds.pl
@@ -116,7 +116,12 @@ elsif ( $op eq 'list' ) {
         }
     }
 
-    my $holds = Koha::Holds->search( $search_params );
+    my $holds = Koha::Holds->search(
+        $search_params,
+        {
+            join => ["item"]
+        }
+    );
 
     $template->param(
         holds => $holds,
@@ -142,7 +147,7 @@ if ( $op eq 'cud-modify' ) {
             $hold->expirationdate($new_expiration_date)->store;
         }
 
-        unless( $hold->branchcode eq $new_pickup_loc){
+        if($new_pickup_loc && ($hold->branchcode ne $new_pickup_loc)){
             $hold->branchcode($new_pickup_loc)->store;
         }
 
-- 
2.34.1