From 7f867deb357ea0dbedcd58899337578eb7c3b774 Mon Sep 17 00:00:00 2001
From: Andrii Nugged <nugged@gmail.com>
Date: Mon, 3 Jun 2024 03:50:24 +0300
Subject: [PATCH] Bug 37014: Fix after-modal-POST to transmit "not_returned"
 message

The bug report addresses an issue where the message "Item was not checked in" erroneously appears during the second POST after a manual or modal check-in process.

1. In circ/returns.pl:
   - Introduced a new hash `%rinot_returned` to track items not returned across pages.
   - Updated the logic to capture the "not returned" status from the query parameters.
   - Adjusted the input processing loop to include `not_returned` status.
   - Modified the section handling barcode check-ins to appropriately initialize and update the `not_returned` status.

2. returns.tt:
   - Added hidden input fields in the template to include `not_returned` status in the form submissions.

The changes ensure that the "not_returned" status is correctly tracked and displayed, preventing the erroneous message from appearing on subsequent POST requests.

1. Perform a manual check-in of an item, but make some warning modal appear: for ex., transfer: check-in the item in not home library.
2. Press OK on the modal. There will be a POST transition again to redraw the checked-in items list.
3. Verify that the "Item was not checked in" message appears erroneously near the item's row.
4. Apply the patch.
5. Repeat steps 1-3 and check that there will be no erroneous "Item was not checked in" message.
---
 circ/returns.pl                                         | 8 +++++++-
 koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt | 7 +++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/circ/returns.pl b/circ/returns.pl
index 3fd20c6591..79c14b982d 100755
--- a/circ/returns.pl
+++ b/circ/returns.pl
@@ -103,6 +103,7 @@ my $returned_counter = C4::Context->preference('numReturnedItemsToShow') || 8;
 my %returneditems;
 my %riduedate;
 my %riborrowernumber;
+my %rinot_returned;
 my @inputloop;
 foreach ( $query->param ) {
     my $counter;
@@ -120,6 +121,7 @@ foreach ( $query->param ) {
     my $barcode        = $query->param("ri-$counter");
     my $duedate        = $query->param("dd-$counter");
     my $borrowernumber = $query->param("bn-$counter");
+    my $not_returned   = $query->param("nr-$counter");
     $counter++;
 
     # decode barcode    ## Didn't we already decode them before passing them back last time??
@@ -130,12 +132,14 @@ foreach ( $query->param ) {
     $returneditems{$counter}    = $barcode;
     $riduedate{$counter}        = $duedate;
     $riborrowernumber{$counter} = $borrowernumber;
+    $rinot_returned{$counter}   = $not_returned;
 
     #######################
     $input{counter}        = $counter;
     $input{barcode}        = $barcode;
     $input{duedate}        = $duedate;
     $input{borrowernumber} = $borrowernumber;
+    $input{not_returned}   = $not_returned;
     push( @inputloop, \%input );
 }
 
@@ -385,6 +389,7 @@ if ($barcode) {
         $returneditems{0}      = $barcode;
         $riborrowernumber{0}   = $borrower->{'borrowernumber'};
         $riduedate{0}          = $duedate;
+        $rinot_returned{0}     = 0;
         $input{borrowernumber} = $borrower->{'borrowernumber'};
         $input{duedate}        = $duedate;
         unless ( $dropboxmode ) {
@@ -428,6 +433,7 @@ if ($barcode) {
         }
         $input{duedate}      = $duedate;
         $input{not_returned} = 1;
+        $rinot_returned{0}   = 1;
         $returneditems{0}    = $barcode;
         $riduedate{0}        = $duedate;
         push( @inputloop, \%input );
@@ -807,7 +813,7 @@ foreach ( sort { $a <=> $b } keys %returneditems ) {
                            # we could handle that better displaying a message in the template
 
 
-        $ri{not_returned} = 1 unless $returned;
+        $ri{not_returned} = $rinot_returned{$_};
         my $biblio = $item->biblio;
         # FIXME pass $item to the template and we are done here...
         $ri{itembiblionumber}    = $biblio->biblionumber;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
index 357eebb3ba..bb02f1b6f2 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt
@@ -430,6 +430,7 @@
                                                     <input type="hidden" name="ri-[% inputloo.counter | html %]" value="[% inputloo.barcode | html %]" />
                                                     <input type="hidden" name="dd-[% inputloo.counter | html %]" value="[% inputloo.duedate | html %]" />
                                                     <input type="hidden" name="bn-[% inputloo.counter | html %]" value="[% inputloo.borrowernumber | html %]" />
+                                                    <input type="hidden" name="nr-[% inputloo.counter | html %]" value="[% inputloo.not_returned | html %]" />
                                                 [% END %]
                                                 <button type="submit" class="approve" accesskey="y"><i class="fa fa-check"></i> Yes, checkin (Y)</button>
                                             </form>
@@ -511,6 +512,7 @@
                                             <input type="hidden" name="ri-[% inputloo.counter | html %]" value="[% inputloo.barcode | html %]" />
                                             <input type="hidden" name="dd-[% inputloo.counter | html %]" value="[% inputloo.duedate | html %]" />
                                             <input type="hidden" name="bn-[% inputloo.counter | html %]" value="[% inputloo.borrowernumber | html %]" />
+                                            <input type="hidden" name="nr-[% inputloo.counter | html %]" value="[% inputloo.not_returned | html %]" />
                                             [% END %]
                                             [% IF item.onloan %]
                                             <button type="submit" class="btn btn-default"><i class="fa fa-check"></i> Confirm checkin and mark missing items as lost</button>
@@ -583,6 +585,7 @@
                                                     <input type="hidden" name="ri-[% inputloo.counter | html %]" value="[% inputloo.barcode | html %]" />
                                                     <input type="hidden" name="dd-[% inputloo.counter | html %]" value="[% inputloo.duedate | html %]" />
                                                     <input type="hidden" name="bn-[% inputloo.counter | html %]" value="[% inputloo.borrowernumber | html %]" />
+                                                    <input type="hidden" name="nr-[% inputloo.counter | html %]" value="[% inputloo.not_returned | html %]" />
                                                 [% END %]
 
                                                 <div class="modal-header">
@@ -670,6 +673,7 @@
                                                         <input type="hidden" name="ri-[% inputloo.counter | html %]" value="[% inputloo.barcode | html %]" />
                                                         <input type="hidden" name="dd-[% inputloo.counter | html %]" value="[% inputloo.duedate | html %]" />
                                                         <input type="hidden" name="bn-[% inputloo.counter | html %]" value="[% inputloo.borrowernumber | html %]" />
+                                                        <input type="hidden" name="nr-[% inputloo.counter | html %]" value="[% inputloo.not_returned | html %]" />
                                                     [% END %]
 
                                                     <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
@@ -777,6 +781,7 @@
                                                         <input type="hidden" name="ri-[% inputloo.counter | html %]" value="[% inputloo.barcode | html %]" />
                                                         <input type="hidden" name="dd-[% inputloo.counter | html %]" value="[% inputloo.duedate | html %]" />
                                                         <input type="hidden" name="bn-[% inputloo.counter | html %]" value="[% inputloo.borrowernumber | html %]" />
+                                                        <input type="hidden" name="nr-[% inputloo.counter | html %]" value="[% inputloo.not_returned | html %]" />
                                                     [% END %]
                                                 </div> <!-- /.modal-footer -->
                                             </form> <!-- /#mainform -->
@@ -860,6 +865,7 @@
                                                         <input type="hidden" name="ri-[% inputloo.counter | html %]" value="[% inputloo.barcode | html %]" />
                                                         <input type="hidden" name="dd-[% inputloo.counter | html %]" value="[% inputloo.duedate | html %]" />
                                                         <input type="hidden" name="bn-[% inputloo.counter | html %]" value="[% inputloo.borrowernumber | html %]" />
+                                                        <input type="hidden" name="nr-[% inputloo.counter | html %]" value="[% inputloo.not_returned | html %]" />
                                                     [% END %]
 
                                                     <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
@@ -1122,6 +1128,7 @@
                                                     <input type="hidden" name="ri-[% inputloo.counter | html %]" value="[% inputloo.barcode | html %]" />
                                                     <input type="hidden" name="dd-[% inputloo.counter | html %]" value="[% inputloo.duedate | html %]" />
                                                     <input type="hidden" name="bn-[% inputloo.counter | html %]" value="[% inputloo.borrowernumber | html %]" />
+                                                    <input type="hidden" name="nr-[% inputloo.counter | html %]" value="[% inputloo.not_returned | html %]" />
                                                 [% END %]
                                             </div>
                                     </div>
-- 
2.45.2