From 31fd8900ccc8634d3bd1165a055d76889156e45c Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Thu, 2 May 2013 08:38:36 +0000
Subject: [PATCH] Bug 8918: Move $rank and $found calculation into AddReserve

and update scripts that call it.
---
 C4/ILSDI/Services.pm    |   26 ++++----------------------
 C4/Reserves.pm          |   24 ++++++++++++++++++++++--
 opac/opac-reserve.pl    |   14 ++------------
 reserve/placerequest.pl |   21 ++++-----------------
 4 files changed, 32 insertions(+), 53 deletions(-)

diff --git a/C4/ILSDI/Services.pm b/C4/ILSDI/Services.pm
index 93facd4..af8dd7c 100644
--- a/C4/ILSDI/Services.pm
+++ b/C4/ILSDI/Services.pm
@@ -26,7 +26,7 @@ use C4::Circulation;
 use C4::Branch;
 use C4::Accounts;
 use C4::Biblio;
-use C4::Reserves qw(AddReserve CancelReserve GetReservesFromBiblionumber GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved GetReserveNextRank);
+use C4::Reserves qw(AddReserve CancelReserve GetReservesFromBiblionumber GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved);
 use C4::Context;
 use C4::AuthoritiesMarc;
 use C4::ILSDI::Utility;
@@ -623,11 +623,9 @@ sub HoldTitle {
         $branch = $$borrower{branchcode};
     }
 
-    my $rank = GetReserveNextRank($biblionumber);
-
     # Add the reserve
     #          $branch, $borrowernumber, $biblionumber, $constraint, $bibitems,  $priority, $notes, $title, $checkitem,  $found
-    AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $rank, undef, $title, undef, undef );
+    AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, undef, undef, $title, undef, undef );
 
     # Hashref building
     my $out;
@@ -689,9 +687,8 @@ sub HoldItem {
     my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
     return { code => 'NotHoldable' } unless $canbookbereserved and $canitembereserved;
 
-    my $branch;
-
     # Pickup branch management
+    my $branch;
     if ( $cgi->param('pickup_location') ) {
         $branch = $cgi->param('pickup_location');
         my $branches = GetBranches();
@@ -700,24 +697,9 @@ sub HoldItem {
         $branch = $$borrower{branchcode};
     }
 
-    my $rank;
-    my $found;
-
-    # Get rank and found
-    if (C4::Context->preference('ReservesNeedReturns')) {
-        $rank = GetReserveNextRank($biblionumber);
-    } else {
-        $rank = '0';
-        if ($item->{'holdingbranch'} eq $branch) {
-            $found = 'W';
-        } else {
-            $found = 'T';
-        }
-    }
-
     # Add the reserve
     # $branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$resdate,$expdate,$notes,$title,$checkitem,$found
-    AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, $rank, '', '', '', $title, $itemnumber, $found );
+    AddReserve( $branch, $borrowernumber, $biblionumber, 'a', undef, undef, '', '', '', $title, $itemnumber );
 
     # Hashref building
     my $out;
diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index 286a604..22871da 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -164,9 +164,29 @@ sub AddReserve {
     } else {
         undef $expdate; # make reserves.expirationdate default to null rather than '0000-00-00'
     }
+
+    # Calculate priority if not given
+    if (not defined $priority) {
+        if ($checkitem and not C4::Context->preference('ReservesNeedReturns')) {
+            $priority = 0;
+        } else {
+            $priority = GetReserveNextRank($biblionumber);
+        }
+    }
+
+    # Calculate found if not given
+    if ($priority == 0 and not defined $found) {
+        my $item = GetItem($checkitem);
+        if ($item->{holdingbranch} eq $branch) {
+            $found = 'W';
+        } else {
+            $found = 'T';
+        }
+    }
+
     if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
-	# Make room in reserves for this before those of a later reserve date
-	$priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
+        # Make room in reserves for this before those of a later reserve date
+        $priority = _ShiftPriorityByDateAndPriority( $biblionumber, $resdate, $priority );
     }
     my $waitingdate;
 
diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl
index 4bba9a6..76fb7ba 100755
--- a/opac/opac-reserve.pl
+++ b/opac/opac-reserve.pl
@@ -223,7 +223,6 @@ if ( $query->param('place_reserve') ) {
         }
 
         my $biblioData = $biblioDataHash{$biblioNum};
-        my $found;
 
         # Check for user supplied reserve date
         my $startdate;
@@ -235,17 +234,8 @@ if ( $query->param('place_reserve') ) {
 
         my $expiration_date = $query->param("expiration_date_$biblioNum");
 
-      # If a specific item was selected and the pickup branch is the same as the
-      # holdingbranch, force the value $rank and $found.
-        my $rank = $biblioData->{rank};
         if ( $itemNum ne '' ) {
             $canreserve = 1 if CanItemBeReserved( $borrowernumber, $itemNum );
-            $rank = '0' unless C4::Context->preference('ReservesNeedReturns');
-            my $item = GetItem($itemNum);
-            if ( $item->{'holdingbranch'} eq $branch ) {
-                $found = 'W'
-                  unless C4::Context->preference('ReservesNeedReturns');
-            }
         }
         else {
             $canreserve = 1 if CanBookBeReserved( $borrowernumber, $biblioNum );
@@ -266,10 +256,10 @@ if ( $query->param('place_reserve') ) {
             AddReserve(
                 $branch,      $borrowernumber,
                 $biblioNum,   'a',
-                [$biblioNum], $rank,
+                [$biblioNum], undef,
                 $startdate,   $expiration_date,
                 $notes,       $biblioData->{title},
-                $itemNum,     $found
+                $itemNum,     undef
             );
             ++$reserve_cnt;
         }
diff --git a/reserve/placerequest.pl b/reserve/placerequest.pl
index 3fe459c..bd1ce13 100755
--- a/reserve/placerequest.pl
+++ b/reserve/placerequest.pl
@@ -68,19 +68,6 @@ foreach my $bibnum (@biblionumbers) {
     $bibinfos{$bibnum} = \%bibinfo;
 }
 
-my $found;
-
-# if we have an item selectionned, and the pickup branch is the same as the holdingbranch
-# of the document, we force the value $rank and $found .
-if ($checkitem ne ''){
-    $rank[0] = '0' unless C4::Context->preference('ReservesNeedReturns');
-    my $item = $checkitem;
-    $item = GetItem($item);
-    if ( $item->{'holdingbranch'} eq $branch ){
-        $found = 'W' unless C4::Context->preference('ReservesNeedReturns');
-    }
-}
-
 if ($type eq 'str8' && $borrower){
 
     foreach my $biblionumber (keys %bibinfos) {
@@ -110,17 +97,17 @@ if ($type eq 'str8' && $borrower){
         if ($multi_hold) {
             my $bibinfo = $bibinfos{$biblionumber};
             AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,'a',[$biblionumber],
-                       $bibinfo->{rank},$startdate,$expirationdate,$notes,$bibinfo->{title},$checkitem,$found);
+                       $bibinfo->{rank},$startdate,$expirationdate,$notes,$bibinfo->{title},$checkitem, undef);
         } else {
             if ($input->param('request') eq 'any'){
                 # place a request on 1st available
-                AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,'a',\@realbi,$rank[0],$startdate,$expirationdate,$notes,$title,$checkitem,$found);
+                AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,'a',\@realbi,undef,$startdate,$expirationdate,$notes,$title,$checkitem,undef);
             } elsif ($reqbib[0] ne ''){
                 # FIXME : elsif probably never reached, (see top of the script)
                 # place a request on a given item
-                AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,'o',\@reqbib,$rank[0],$startdate,$expirationdate,$notes,$title,$checkitem, $found);
+                AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,'o',\@reqbib,undef,$startdate,$expirationdate,$notes,$title,$checkitem,undef);
             } else {
-                AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,'a',\@realbi,$rank[0],$startdate,$expirationdate,$notes,$title,$checkitem, $found);
+                AddReserve($branch,$borrower->{'borrowernumber'},$biblionumber,'a',\@realbi,undef,$startdate,$expirationdate,$notes,$title,$checkitem,undef);
             }
         }
     }
-- 
1.7.10.4