From 86117fe1c868d43740bb3dd13ea2a5d75cc06e02 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Mon, 19 Nov 2012 13:38:37 -0500
Subject: [PATCH] [SIGNED-OFF] Bug 8451 - Confusing and problematic double
 prompt for processing transfers
Content-Type: text/plain; charset="utf-8"

This patch will cause changing the priority of a reserve from 'in transit'
to a priority number to delete the transfer associated with it, if there
is one.

Signed-off-by: Owen Leonard <oleonard@myacpl.org>

Tested with this plan:

1. Place a hold on an item for delivery to another branch.
2. Check in the item and confirm the transfer.
3. Edit the hold, changing the status from in-transit to a priority number.
4. Check in the item again.

Before the patch I would see two prompts at step 4. After the patch I
see one.
---
 C4/Reserves.pm                                     |   17 +++++++++++++++--
 .../prog/en/modules/reserve/request.tt             |    1 +
 reserve/modrequest.pl                              |    3 ++-
 reserve/request.pl                                 |    1 +
 4 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index a380bf0..a730a56 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -273,7 +273,8 @@ sub GetReservesFromBiblionumber {
                 expirationdate,
                 lowestPriority,
                 suspend,
-                suspend_until
+                suspend_until,
+                reserve_id
         FROM     reserves
         WHERE biblionumber = ? ";
     unless ( $all_dates ) {
@@ -1056,7 +1057,7 @@ itemnumber and supplying itemnumber.
 
 sub ModReserve {
     #subroutine to update a reserve
-    my ( $rank, $biblio, $borrower, $branch , $itemnumber, $suspend_until) = @_;
+    my ( $rank, $biblio, $borrower, $branch , $itemnumber, $suspend_until, $reserve_id ) = @_;
      return if $rank eq "W";
      return if $rank eq "n";
     my $dbh = C4::Context->dbh;
@@ -1089,6 +1090,13 @@ sub ModReserve {
         
     }
     elsif ($rank =~ /^\d+/ and $rank > 0) {
+        my $reserve;
+        if ( $reserve_id ) {
+            my $sth = $dbh->prepare("SELECT * FROM reserves WHERE reserve_id = ?");
+            $sth->execute( $reserve_id );
+            $reserve = $sth->fetchrow_hashref();
+        }
+
         my $query = "
             UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL, waitingdate = NULL
             WHERE biblionumber   = ?
@@ -1098,6 +1106,11 @@ sub ModReserve {
         $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
         $sth->finish;
 
+        ## If we are re-prioritizing a previously 'in transit' hold, we need to remove any item transfer that may have been created for it.
+        if ( $reserve->{'found'} eq 'T' ) {
+            $dbh->do("DELETE FROM branchtransfers WHERE itemnumber = ? AND tobranch = ? AND datearrived IS NULL", undef, ( $itemnumber, $reserve->{'branchcode'} ) );
+        }
+
         if ( defined( $suspend_until ) ) {
             if ( $suspend_until ) {
                 $suspend_until = C4::Dates->new( $suspend_until )->output("iso");
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
index 0ad5d23..06e3385 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
@@ -553,6 +553,7 @@ function checkMultiHold() {
   [% FOREACH reserveloo IN biblioloo.reserveloop %]
   [% UNLESS ( loop.odd ) %]<tr class="highlight">[% ELSE %]<tr>[% END %]
         <td>
+          <input type="hidden" name="reserve_id" value="[% reserveloo.reserve_id %]" />
           <input type="hidden" name="borrowernumber" value="[% reserveloo.borrowernumber %]" />
           <input type="hidden" name="biblionumber" value="[% reserveloo.biblionumber %]" />
           <select name="rank-request">
diff --git a/reserve/modrequest.pl b/reserve/modrequest.pl
index 03ef064..0b48a1f 100755
--- a/reserve/modrequest.pl
+++ b/reserve/modrequest.pl
@@ -42,6 +42,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 );
 
 my @rank=$query->param('rank-request');
+my @reserve_id = $query->param('reserve_id');
 my @biblionumber=$query->param('biblionumber');
 my @borrower=$query->param('borrowernumber');
 my @branch=$query->param('pickup');
@@ -67,7 +68,7 @@ if ($CancelBorrowerNumber) {
 else {
     for (my $i=0;$i<$count;$i++){
         undef $itemnumber[$i] unless $itemnumber[$i] ne '';
-        ModReserve($rank[$i],$biblionumber[$i],$borrower[$i],$branch[$i],$itemnumber[$i],$suspend_until[$i]); #from C4::Reserves
+        ModReserve($rank[$i],$biblionumber[$i],$borrower[$i],$branch[$i],$itemnumber[$i],$suspend_until[$i],$reserve_id[$i]); #from C4::Reserves
     }
 }
 my $from=$query->param('from');
diff --git a/reserve/request.pl b/reserve/request.pl
index c34a614..8de4e4b 100755
--- a/reserve/request.pl
+++ b/reserve/request.pl
@@ -556,6 +556,7 @@ foreach my $biblionumber (@biblionumbers) {
         $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
             unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
         $reserve{'date'}           = format_date( $res->{'reservedate'} );
+        $reserve{'reserve_id'}     = $res->{'reserve_id'};
         $reserve{'borrowernumber'} = $res->{'borrowernumber'};
         $reserve{'biblionumber'}   = $res->{'biblionumber'};
         $reserve{'borrowernumber'} = $res->{'borrowernumber'};
-- 
1.7.9.5